0

Check50 does not seem to think that the values in my table in index.html are updating correctly. After an afternoon of testing and searching through here and stackoverflow for similar issues, I cannot seem to find a good reason why my code would be failing this one test.

The code:

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
    """Sell shares of stock"""
    if request.method == "POST":
        symbol = request.form.get("symbol").lower()
        try:
            amount = int(request.form.get("shares"))
        except ValueError:
            return apology("shares must be a positive integer", 400)
        price = lookup(symbol).get("price")
        total_value = int(amount) * price
        cash = db.execute("SELECT cash FROM users WHERE id=?", session['user_id'])[0]["cash"]

        stocks = db.execute("SELECT * FROM transactions WHERE symbol=? AND id=?", symbol, session['user_id'])
        if len(stocks) == 0:
            return apology("you do not own any of this stock!", 400)
        elif total_shares(symbol) < int(amount):
            return apology("you do not own enough of this stock to sell", 400)
        elif int(amount) < 0:
            return apology("invalid amount", 400)
        else:
            db.execute("UPDATE users SET cash = ? WHERE id=?", cash + total_value, session['user_id'])
            db.execute("INSERT INTO transactions (id, symbol, volume, price, type) VALUES(?, ?, ?, ?, ?)",
                        session['user_id'], symbol, int(amount), price, "sell")
        return redirect("/")
    else:
        stocks = db.execute("SELECT DISTINCT symbol FROM transactions WHERE id=?", session['user_id'])
        return render_template("sell.html", stocks=stocks)

The HTML:

{% extends "layout.html" %}

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

{% block main %}
    <h1>Sell Some Stock!</h1>

    <form action="/sell" method="post">
        <div class="form-group">
            <label for="symbol">Select a stock to sell:</label>
            <select name="symbol">
                {% for stock in stocks %}
                    <option name="symbol">{{ stock.symbol.upper() }}</option>
                {% endfor %}
            </select>
            <input class="form-control" name="shares" placeholder="Number of Shares" type="text">
        </div>
        <button class="btn btn-primary" type="submit">Sell</button>
    </form>
{% endblock %}

My database schema:

CREATE TABLE IF NOT EXISTS 'transactions' 
('id' integer DEFAULT NULL, 'symbol' text DEFAULT NULL, 
'volume' integer DEFAULT NULL, 'price' numeric DEFAULT NULL, 
'type' text DEFAULT NULL, 'date' date DEFAULT CURRENT_DATE, 
'time' time DEFAULT CURRENT_TIME);

Again, not entirely sure why this one test is failing. I have tested and retested and all of my values are accounted for in the table that is returned at "/" after a successful "sale". Thanks in advance to anyone who can try and help me unmelt my brain. Please let me know if there are any additional details I might need to provide to diagnose what's going on.

You must log in to answer this question.

Browse other questions tagged .