# SELL
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock"""
if request.method == "POST":
# ensure ifproper notsymbol
stock = lookup(request.form.get("symbol"):)
if not stock:
return apology("Missing symbol"Symbol")
# ensure password was submitted
elif not request.form.get("shares"):
return apology("Missing shares")
#else:
Check how much cash and what stocks usershares have= request.form.get("shares")
# query database for user's cash
cash = db.execute("SELECT cash FROM users WHERE id=id = :id", id = session["user_id"])
cash = cash[0]["cash"]
# select the symbol shares id=session["user_id"])of that user
stocks user_shares = db.execute("SELECT shares, symbol FROM portfolio WHERE id=:id",
id = :id AND symbol=:symbol", \
id=session["user_id"])
# update each symbol prices and total
# for stock inid=session["user_id"], stocks:symbol=stock["symbol"])
symbol = request.form.get("symbol")
stockstocks = lookupdb.execute(symbol)
"SELECT shares, symbol = stock["symbol"]
FROM portfolio sharesWHERE =id=:id", int(stock["shares"]id=session["user_id"])
price = stock["price"]
# check totalif =enough shares *to price
sell
db.execute("UPDATE portfolio SET price=:price, total=:total WHEREif id=:iduser_shares[0]["shares"] AND< symbol=shares:symbol",
price=usd(stock["price"]),return total=usdapology(total),"Too id=session["user_id"],many symbol=symbolshares")
# Calculate how many shares# ofupdate this stockof usera hassell
share_qty = db.execute("SELECT shares FROM"INSERT totalINTO WHEREhistory symbol=:(symbol AND id=:id",
shares, price, id) \
VALUES(:symbol, :shares, symbol=symbol:price, id=session["user_id"]:id)", \
total_share = share_qty[0]["shares"]
# Check if user has enough shares of the stock
symbol=stock["symbol"], shares=-shares, \
if total_share < shares:
return apology("You don't have enough stocks")
price=usd(stock["price"]), else:id=session["user_id"])
# Calcalute how much cashupdate user nowcash has(increase)
new_cash = floatdb.execute(cash[0]["cash"])"UPDATE +users total
SET cash = cash + :cash_after WHERE id #= Log:id", in\
updated cash
db.execute("UPDATE users SET cash=:new_cash WHERE id=:id",
id=session["user_id"], \
cash_after=stock["price"] new_cash=new_cash,* id=session["user_id"]float(shares))
# Insertdecrement intothe hisotryshares tablecount
to record the transaction
shares_total = user_shares[0]["shares"] - shares
db.execute("INSERT INTO history (symbol, shares, price, id)# VALUESif (:symbol,after :shares,decrement :price,is :id)"zero,
delete shares from portfolio
if shares_total == 0:
symbol=symbol,db.execute("DELETE shares=shares,FROM price=price,portfolio id=session["user_id"])
\
# Updating portfolio table reflecting the sell of shares
ifWHERE total_shareid=:id ==AND sharessymbol=:
symbol", \
db.execute("DELETE FROM portfolio WHERE symbol=:symbol AND id=:id",
id=session["user_id"], \
symbol=symbol, id=session["user_id"]symbol=stock["symbol"])
else:
# otherwise, update portfolio shares count
new_total = total_share - shareselse:
db.execute("UPDATE portfolio SET shares=:new_shareshares \
WHERE id=:id"id AND symbol=:symbol", \
shares=shares_total, id=session["user_id"], \
new_share=new_total, id=session["user_id"] symbol=stock["symbol"])
# Flashflash soldbought alert
flash('Sold!')
# Redirect user to indexhome page
return redirect("/")
else:
# If request method GET
else:
return render_template("sell.html", cash=cash[0]["cash"]cash = cash[0]["cash"], stocks=stocks)