after I run check 50, everything shows green except these:
I am trying to fix the error for buy. After I run flask and tried to make purchase, it shows 404 however my portfolio table get updated. So I am thinking the error is in linking buy code to my index code so my buy function will show in index.html. but I dont know how. Below are my code for index.html. Any suggestions would be appreciated.
{% extends "layout.html" %}
{% block title %}
Portfolio
{% endblock %}
{% block main %}
<table class="table table-striped" style="width:100%">
<thread>
<th>Symbol</th>
<th>Name</th>
<th>Shares</th>
<th>Price</th>
<th>Total</th>
</thread>
<tfoot>
<tr>
<td colspan="4"></td>
<td> <b> ${{'%0.2f'|format(total_balance)}} </b> </td>
</tr>
</tfoot>
<tbody>
{% for row in rows_pt %}
<tr>
<td>{{row.symbol}}</td>
<td>{{row.name}}</td>
<td>{{row.shares}}</td>
<td>${{row.price}}</td>
<td>${{'%0.2f'|format(portfolio.total|float)}}</td>
</tr>
{% endfor %}
<tr>
<td colspan="4">CASH</td>
<td>${{'%0.2f'|format(current_balance|float)}}</td>
</tr>
</tbody>
</table>
{% endblock %}
Below are my code for index and buy
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks"""
rows_pt = db.execute("SELECT * FROM portfolio WHERE id = :id",id = session["user_id"])
if rows_pt == []:
cash_b = db.execute("SELECT cash FROM users WHERE id = :id",id = session["user_id"])
cash = cash_b[0]["cash"]
return render_template("index.html", current_balance=cash, total_balance = cash)
else:
new_rows_pt = []
for row in rows_pt:
new_rows_dict = {}
quote = lookup(row["symbol"])
new_rows_dict["symbol"] = quote["symbol"]
new_rows_dict["name"] = quote["name"]
new_rows_dict["shares"] = row["shares"]
new_rows_dict["price"] = quote["price"]
new_rows_dict["total"] = row["shares"] * quote["price"]
new_rows_pt.append(new_rows_dict)
cash_b = db.execute("SELECT cash FROM users WHERE id = :id",id = session["user_id"])
cash = cash_b[0]["cash"]
total_balance = cash
for row in new_rows_pt:
total_balance += row["total"]
return render_template("index.html", rows_pt = new_rows_pt, current_balance = cash, total_balance = cash)
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock"""
if request.method == "POST":
if not request.form.get("symbol"):
return apology("must provide symbol")
elif not request.form.get("shares") or not request.form.get("shares").isdigit() or int(request.form.get("shares")) <= 0:
return apology("must provide positive number of shares you want to purchase")
else:
quote = lookup(request.form.get("symbol").upper())
if not quote:
return apology("invalid stock name")
cash = db.execute("SELECT cash FROM users WHERE id= :id", id=session["user_id"])
if not cash or float(cash[0]["cash"]) < quote["price"] * int(request.form.get("shares")):
return apology("insufficient cash")
db.execute("UPDATE users SET cash = cash - :price WHERE id = :id", price = quote["price"] * int(request.form.get("shares")), id = session["user_id"])
updated_cash = db.execute("SELECT cash FROM users WHERE id= :id", id=session["user_id"])
total = updated_cash[0]["cash"] + quote["price"] * int(request.form.get("shares"))
rows = db.execute("SELECT * FROM portfolio WHERE id = :id AND symbol = :symbol",symbol = request.form.get("symbol").upper(), id = session["user_id"])
if len(rows) == 0:
db.execute("INSERT INTO portfolio (id, symbol, name, shares, price, total) VALUES (:id, :symbol, :name, :shares, :price, :total)", id= session["user_id"], symbol = request.form.get("symbol").upper(), name = quote["name"], shares= int(request.form.get("shares")), price = usd(quote["price"]), total = total)
else:
db.execute("UPDATE portfolio SET shares = shares + :shares WHERE symbol = :symbol", shares= int(request.form.get("shares")), symbol = request.form.get("symbol").upper())
db.execute("INSERT INTO history (id, symbol, shares, price) VALUES (:id, :symbol, :shares, :price)", id = session["user_id"], symbol = request.form.get("symbol").upper(), shares = int(request.form.get("shares")), price =quote["price"])
return redirect("/index")
else:
return render_template("buy.html")