Advertisement
DragonOsman

buy route pset7 Finance

Mar 11th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. @app.route("/buy", methods=["GET", "POST"])
  2. @login_required
  3. def buy():
  4.     """Buy shares of stock."""
  5.    
  6.     if request.method == "POST":
  7.         # ensure a symbol was submitted
  8.         if not request.form.get("symbol"):
  9.             return apology("symbol required")
  10.        
  11.         # check if symbol is valid
  12.         stock = lookup(request.form.get("symbol"))
  13.         if not stock:
  14.             return apology("symbol does not exist")
  15.        
  16.         # ensure that number of shares entered is not negative
  17.         user_input_shares = int(request.form.get("shares"))
  18.         if user_input_shares < 0:
  19.             return apology("number of shares cannot be negative!")
  20.        
  21.         # check how much cash user has
  22.         cash = db.execute("SELECT cash FROM users WHERE id=:uid", uid=session["user_id"])
  23.         if not cash:
  24.             return apology("something went wrong when looking up cash amount")
  25.        
  26.         # calculate total price of stock
  27.         total = stock["price"] * user_input_shares
  28.         if total > cash[0]["cash"]:
  29.             return apology("You don't have enough money!")
  30.            
  31.         # update user's information and total amount of cash in database and send user to Portfolio
  32.         total_cash = cash[0]["cash"] - total
  33.         db.execute("UPDATE users SET cash=:cash WHERE id=:uid", cash=total_cash, uid=session["user_id"])
  34.        
  35.         transactions_info = db.execute("SELECT * FROM transactions WHERE user_id=:uid", uid=session["user_id"])
  36.         total_shares = 0
  37.         if len(transactions_info) != 0:
  38.             for i in range(len(transactions_info)):
  39.                 if transactions_info[i]["stock_symbol"] == request.form.get("symbol") and transactions_info[i]["no_of_shares"] > 0:
  40.                     total_shares = db.execute("UPDATE transactions SET no_of_shares=:shares WHERE user_id=:uid AND stock_symbol=:ssymbol AND time=:t",
  41.                     shares=transactions_info["no_of_shares"] + user_input_shares, uid=session["user_id"], stock_symbol=request.form.get("symbol"),
  42.                     t=time.time())
  43.                 elif transactions_info[i]["stock_symbol"] != request.form.get("symbol"):
  44.                     db.execute("INSERT INTO transactions (stock_name, stock_price, stock_symbol, time, no_of_shares, user_id) VALUES(:sname, :sprice, :ssymbol, :t, :shares, :uid)",
  45.         sname=stock["name"], sprice=stock["price"], ssymbol=stock["symbol"], t=time.time(), shares=user_input_shares, uid=session["user_id"])
  46.        
  47.         cash = db.execute("SELECT cash FROM users WHERE id=:uid", uid=session["user_id"])
  48.        
  49.         grand_total = 0
  50.         total = 0
  51.         for i in range(len(transactions_info)):
  52.             grand_total = (transactions_info[i]["no_of_shares"] * transactions_info[i]["stock_price"]) + cash
  53.             if transactions_info[i]["stock_symbol"] == stock["symbol"]:
  54.                 total = transactions_info["stock_price"] * transactions_info[i]["no_of_shares"]
  55.            
  56.         return render_template("index.html", transactions_info=transactions_info, total_shares=total_shares,
  57.         grand_total=grand_total, cash=cash, total=total)
  58.     elif request.method == "GET":
  59.         return render_template("buy.html")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement