def quote(): """Get stock quote.""" if request.method == "POST": #verify symbol entry present if not request.form.get("symbol"): return apology("please enter ticker symbol", 403) #lookup the symbol else: sym=(request.form.get("symbol")) sym=sym.upper() if lookup(sym) != None: result = lookup(sym) #log quote logname = getlogname(db) db.execute("INSERT INTO :logname (username, datetime, action,symbol,price) VALUES(:username, DATETIME('now'), 'quote', :symbol, :price)", logname=logname, username=getuser(db), symbol = result['symbol'], price = result['price']) #get current cash to pass to result page for user consideration currentcash=getcash(db) return render_template("quote_result.html", name = result['name'], price = result['price'], symbol = result['symbol'], cash=currentcash) #if lookup(sym) is bad, means ticker was invalid else: return apology("invalid ticker symbol", 403) else: return render_template("quote.html")
def buy_from_quote(): """Show quote & qty purchase option""" #Get relevant quote information from db quote=lastquote(db) if request.method == "POST": #verify qty field was filled out properly if not request.form.get("qty"): return apology("must provide qty to purchase", 403) elif not request.form.get("qty").isdigit(): return apology("must provide valid qty to purchase", 403) #execute the purchase else: sym=quote[0]['symbol'] qty=int(request.form.get("qty")) bought=buyshares(sym,qty,db) if bought == True: return redirect("/buy_success") elif bought == 999: return apology("not enough ca$h", 403) else: return apology("unknown", 403) else: #show relevant info cash=getcash(db) return render_template("buy_from_quote.html",symbol=quote[0]['symbol'],price=quote[0]['price'], cash=cash)
def sell(): """Sell shares of stocks""" if request.method == "POST": #verify user provided ticker symbol if not request.form.get("symbol"): return apology("symbol needed",403) #verify user provided quantity to sell elif not request.form.get("qty"): return apology("qty needed",403) #tests passed: let's sell! else: #cleanup variables sym=request.form.get("symbol") sym=sym.upper() qty=int(request.form.get("qty")) #sellshares function executes the sale sold=sellshares(sym,qty,db) if sold == True: return redirect("/") else: return apology("ain't got enough") else: #variables to display holdings & cash while considering sale holdings=valueheld(db) currentcash=getcash(db) return render_template("sell.html", data=holdings,cash=currentcash)
def buy(): """Buy shares of stock""" if request.method == "POST": #verify symbold & qty fields were filled out properly if not request.form.get("symbol"): return apology("must provide ticker symbol to purchase", 403) elif not request.form.get("qty"): return apology("must provide qty to purchase", 403) elif not request.form.get("qty").isdigit(): return apology("must provide valid qty to purchase", 403) else: #clean up vars sym = request.form.get("symbol") sym = sym.upper() qty = int(request.form.get("qty")) #verify ticker symbol is real if lookup(sym) == None: return apology("invalid ticker symbol", 403) #execute the sale else: bought=buyshares(sym,qty,db) if bought == True: return redirect("/buy_success") elif bought == 999: return apology("not enough ca$h", 403) else: return apology("unknown", 403) else: #variables to show current cash & holdings cash=getcash(db) holdings=valueheld(db) return render_template("buy.html", data=holdings, cash=cash)
def index(): """Show portfolio of stocks""" #find name of holdings DB holdings=valueheld(db) currentcash=getcash(db) return render_template("index.html", data=holdings,cash=currentcash)