Guest User

Untitled

a guest
Sep 4th, 2017
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @app.route("/buy", methods=["GET", "POST"])
  2. @login_required
  3. def buy():
  4. if request.method == "POST":
  5. if not request.form.get("symbol"):
  6. return apology("Please enter the symbol of the stock you want to purchase")
  7.  
  8. elif not request.form.get("share"):
  9. return apology("You need to enter number of shares you want to purchase")
  10.  
  11. else:
  12. stock = request.form.get("symbol")
  13. shares = int(request.form.get("share"))
  14.  
  15. purchase = lookup(stock)
  16.  
  17. if not purchase:
  18. return apology("You need to enter a valid stock symbol. Please check again.")
  19.  
  20. row = db.execute("SELECT cash FROM users WHERE id = :id", id = session["user_id"])
  21. total = purchase["price"] * shares
  22. money = row[0]["cash"]
  23.  
  24. if total > money:
  25. return apology("You don't have enough money to do that.")
  26. else:
  27. order = db.execute("UPDATE users SET cash = cash - 'total' WHERE id = :id", id = session["user_id"])
  28. return order
  29. # else if user reached route via GET (as by clicking a link or via redirect)
  30. else:
  31. return render_template("buy.html")
RAW Paste Data