def buy(symbol): """Buy shares of stock""" print(symbol) userCash = user_request.request_cash(session["user_id"], cursor) quote = quote_request.get_quote_info(symbol) maximum_share = round(userCash / quote["price"]) return render_template("buy.html", quote=quote, cash=userCash, maximum_share=maximum_share, user_name=session["user_name"])
def index(): """Show portfolio of stocks""" cash = user_request.request_cash(session["user_id"], cursor=cursor) # get user quote info list quote_info = user_request.request_list_quote(session["user_id"], cursor=cursor) # get price from market quote_info = quote_request.get_quote_list_info(quote_info) total = cash for quote in quote_info: total = total + quote["share"] * quote["price"] total = round(total, 2) return render_template('index.html', user_name=session["user_name"], quote_info=quote_info, cash=cash, total=total)
def buy_post(): if request.method == "POST": symbol = request.form.get("symbol") price = request.form.get("price") buy_share = request.form.get("buy_share") print(symbol) print(price) print(buy_share) cash = user_request.request_cash(session["user_id"], cursor=cursor) if float(cash) < float(price) * float(buy_share): return apology("Not enough cash to commit the transaction") else: transaction.commitBuy(user_id=session["user_id"], symbol=symbol, share=int(buy_share), price=float(price), cash=cash, cursor=cursor) return redirect("/")
def sell(symbol): """Sell shares of stock""" if not symbol: return redirect("/") elif request.method == "GET": quote = quote_request.get_quote_info(symbol) available_share = user_request.request_available_share( session["user_id"], symbol, cursor) cash = user_request.request_cash(session["user_id"], cursor) if quote == None: return redirect("/") elif available_share == 0: return apology("You have no share to sell!" ) #Will update this to message thing return render_template("sell.html", user_name=session["user_name"], quote=quote, available_share=available_share, cash=cash)
def sell_post(): if request.method == "GET": return redirect("/") else: sell_share = request.form.get("sell_share") symbol = request.form.get("symbol") price = request.form.get("price") #To-do: do transaction here available_share = user_request.request_available_share( session["user_id"], symbol, cursor) cash = user_request.request_cash(session["user_id"], cursor) if int(sell_share) > available_share: return apology("Not enough share to sell") else: transaction.commitSell(user_id=session["user_id"], symbol=symbol, share=float(sell_share), price=float(price), cash=cash, cursor=cursor) return redirect("/")