I've been trying to solve this issue for hours and every time I run the code I get the error:
****:( sell handles valid sale
Cause expected to find "56.00" in page, but it wasn't found
Log sending POST request to /login sending POST request to /sell checking that "56.00" is in page****
I've looked at and tried the solutions recommended to those with similar issues but none of it has worked so far.
Thank you to anyone who is willing to help in advance.
The code:
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock"""
owned_symbols = db.execute("""SELECT symbol, sum(shares) as sum_of_shares
FROM transactions
WHERE user_id = ?
GROUP BY user_id, symbol
HAVING sum_of_shares > 0;""", session["user_id"])
if request.method == "POST":
if not (symbol := request.form.get("symbol")):
return apology("MISSING SYMBOL")
if not (shares := request.form.get("shares")):
return apology("MISSING SHARES")
# Define price
price = lookup(symbol)
# Check share is numeric data type
try:
shares = int(shares)
except ValueError:
return apology("INVALID SHARES")
# Check shares is positive number
if not (shares > 0):
return apology("INVALID SHARES")
symbols_dict = {d['symbol']: d['sum_of_shares'] for d in owned_symbols}
if symbols_dict[symbol] < shares:
return apology("TOO MANY SHARES")
query = lookup(symbol)
# Get user currently owned cash
rows = db.execute("SELECT * FROM users WHERE id = ?", session["user_id"])
# Execute a transaction
db.execute("INSERT INTO transactions (user_id, symbol, shares, price) VALUES (?, ?, ?, ?);",
session["user_id"], symbol.upper(), shares, price["price"])
# Update user owned cash
db.execute("UPDATE users SET cash = ? WHERE id = ?;",
(rows[0]['cash'] + (query['price'] * shares)), session["user_id"])
flash("Transaction successful")
return redirect("/")
else:
return render_template("sell.html", symbols=owned_symbols)
sell.html:
{% extends "layout.html" %}
{% block title %}
Sell
{% endblock %}
{% block main %}
<form action="/sell" method="post">
<div class= "form-group">
<select class="form-control" name="symbol">
<option disabled selected value="">Symbol</option>
{% for symbol in symbols %}
<option value="{{symbol}}">{{symbol}}</option>
{% endfor %}
</select>
</div>
<div class= "form-group">
<input autocomplete="off" placeholder="Shares" min="1" class="form-control"name="shares" type="number">
</div>
<button class="btn btn-primary" type="submit">Sell</button>
</form>
{% endblock %}