Skip to main content
6 of 8
deleted 148 characters in body

CS50 finance: Sell function.(Dropdown list of user shares NOT displayed via sell.html)

Purpose of Sell: to make sure the shares bought can be sold by selecting it through a dropdown list displaying symbols of share bought. I am trying to replicate the cs50 finance default page.

enter image description here

I have 3 tables in my SQL database: - user (default), portfolio, history

Below is my sell function from application.py. I am using for-loop to make sure that bought shares in portfolio can be selected in sell.html (like in index.html)

# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
    # ensure proper symbol
    stock = lookup(request.form.get("symbol"))
    if not stock:
        return apology("Missing Symbol")

    # ensure password was submitted
    elif not request.form.get("shares"):
        return apology("Missing shares")

    else:
        shares = request.form.get("shares")

        # query database for user's cash
        cash = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
        cash = cash[0]["cash"]


        # select the symbol shares of that user
        user_shares = db.execute("SELECT shares FROM portfolio WHERE id = :id AND symbol=:symbol", \
                                 id=session["user_id"], symbol=stock["symbol"])

        stocks = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id", id=session["user_id"])


        # check if enough shares to sell
        if user_shares[0]["shares"] < shares:
            return apology("Too many shares")

        # update  of a sell
        db.execute("INSERT INTO history (symbol, shares, price, id) \
                    VALUES(:symbol, :shares, :price, :id)", \
                    symbol=stock["symbol"], shares=-shares, \
                    price=usd(stock["price"]), id=session["user_id"])

        # update user cash (increase)
        db.execute("UPDATE users SET cash = cash + :cash_after WHERE id = :id", \
                    id=session["user_id"], \
                    cash_after=stock["price"] * float(shares))

        # decrement the shares count
        shares_total = user_shares[0]["shares"] - shares

        # if after decrement is zero, delete shares from portfolio
        if shares_total == 0:
            db.execute("DELETE FROM portfolio \
                        WHERE id=:id AND symbol=:symbol", \
                        id=session["user_id"], \
                        symbol=stock["symbol"])
        # otherwise, update portfolio shares count
        else:
            db.execute("UPDATE portfolio SET shares=:shares \
                    WHERE id=:id AND symbol=:symbol", \
                    shares=shares_total, id=session["user_id"], \
                    symbol=stock["symbol"])

        # flash bought alert
        flash('Sold!')

        # Redirect user to home page
        return redirect("/")

else:
    # If request method GET
    return render_template("sell.html", cash = cash[0]["cash"], stocks=stocks)

............................................................................

I want to use the backend python code above with my frontend sell.html code which is as follows:

{% extends "layout.html" %}

{% block title %}
    Sell
{% endblock %}

{% block main %}
    <form action="/sell" method="post">
        <div class="form-group">
            <select  class="form-control" name="symbol" placeholder="Symbol">
                {% for stock in stocks %}
                <option value ="{{stock.symbol}}" > {{ stock.symbol}} </option>
                {% endfor %}
            </select>
        </div>
        <div class="form-group">
            <input autocomplete="off" class="form-control" min="0" name="shares" placeholder="Shares" type="number">
        </div>
        <button class="btn btn-primary" type="submit">Sell</button>
    </form>
{% endblock %}

Issue: Unable to get the output or the dropdown list of bought share in my portfolio.