Please help me implement the /sell function in my flask app. Here's my sell.html: {%…
- User
Please help me implement the /sell function in my flask app. Here's my sell.html: {% extends "layout.html" %}
{% block title %}
Sell
{% endblock %}
{% block body %}
{% endblock %}
Here's app.py: import os
from cs50 import SQL from flask import Flask, flash, redirect, render_template, request, session from flask_session import Session from werkzeug.security import check_password_hash, generate_password_hash
from helpers import apology, login_required, lookup, usd
Configure application
app = Flask(name)
Custom filter
app.jinja_env.filters["usd"] = usd
Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem" Session(app)
Configure CS50 Library to use SQLite database
db = SQL("sqlite:///finance.db")
@app.after_request def after_request(response): """Ensure responses aren't cached""" response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" response.headers["Expires"] = 0 response.headers["Pragma"] = "no-cache" return response
@app.route("/") @login_required def index(): # Assume db.execute() returns a dict or a list of dicts user_cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session['user_id'])[0]['cash'] purchases = db.execute(""" SELECT symbol, SUM(shares) as shares, AVG(price) as price FROM purchases WHERE user_id = :id GROUP BY symbol HAVING SUM(shares) > 0; """, id=session['user_id']) # Fixed indent of for loop and calculate total for purchase in purchases: purchase['total'] = purchase['shares'] * purchase['price']
# Moved the summation of total_assets out of the for loop total_assets = sum(purchase['total'] for purchase in purchases) + user_cash return render_template("index.html", cash=user_cash, purchases=purchases, total_assets=total_assets)@app.route("/buy", methods=["GET", "POST"]) @login_required def buy(): if request.method == "GET": return render_template("buy.html") elif request.method == "POST": symbol = request.form.get('symbolb').upper() # Get symbol and ensure it’s uppercase try: quantity = int(request.form.get('quantity')) except ValueError: return apology("Invalid quantity", 400)
if quantity <= 0: return apology("Quantity must be more than 0", 400) stock = lookup(symbol) if stock is None: return apology("Invalid stock symbol", 400) current_price = stock['price'] user_cash = db.execute("SELECT cash FROM users WHERE id = :id", id=session['user_id'])[0]['cash'] total_cost = current_price * quantity if total_cost > user_cash: return apology("Not enough cash to complete purchase", 403) db.execute("UPDATE users SET cash = cash - :cost WHERE id = :id", cost=total_cost, id=session['user_id']) # Insert the new purchase db.execute("INSERT INTO purchases (user_id, symbol, shares, price) VALUES (:user_id, :symbol, :shares, :price)", user_id=session['user_id'], symbol=symbol, shares=quantity, price=current_price) flash("Purchase successful!") return redirect("/") - Assistant
- User
What would sell.html look like
- Assistant
To accommodate the changes mentioned previously, your
sell.htmltemplate needs a dropdown populated with the stocks the user owns. Here’s a modified version ofsell.htmlthat includes a<select>element for the user to choose which stock symbol to sell.