Advertisement
Guest User

Untitled

a guest
Jun 19th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. @app.route("/sell", methods=["GET", "POST"])
  2. @login_required
  3. def sell():
  4. """Sell shares of stock"""
  5. if request.method == "GET":
  6.  
  7. # Dropdown menu options for the list of stocks owned
  8. holdings = db.execute("SELECT symbol FROM holdings WHERE user_id=:user_id", user_id=session["user_id"])
  9.  
  10. return render_template("sell.html", holdings=holdings)
  11.  
  12. else:
  13.  
  14. # Stock sold
  15. soldSymbol = request.form.get("sell")
  16.  
  17. # Check if user selected a stock
  18. if not soldSymbol:
  19. return apology("Please select a stock.")
  20.  
  21. # Lookup symbol
  22. symbol = lookup(soldSymbol)
  23.  
  24. # Get the current number of shares owned
  25. currentHoldings = db.execute("SELECT amount FROM holdings WHERE user_id=:user_id AND symbol=:symbol", user_id=session["user_id"], symbol=symbol["symbol"])
  26. currentAmount = int(currentHoldings[0]["amount"])
  27.  
  28. # Shares sold
  29. shares = request.form.get("shares")
  30. if not shares:
  31. return apology("Please select a number of stocks to sell.")
  32.  
  33. # If user sold too many shares
  34. if shares > currentAmount:
  35. return apology("You have sold too many shares.")
  36.  
  37. # Price of one share
  38. sharePrice = symbol["price"]
  39.  
  40. # Total value sold, number of shares sold * price of one share
  41. value = shares * sharePrice
  42.  
  43. # Current balance of user
  44. userCash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
  45.  
  46. # New balance of user after transaction
  47. userCash = float(userCash[0]['cash'])
  48. newCash = userCash + value
  49.  
  50. # Log into transactions
  51. # Date and time
  52. now = datetime.datetime.now()
  53. date_time = now.strftime("%d/%m/%Y %H:%M:%S")
  54.  
  55. # Insert transaction details into the transaction table
  56. transaction = db.execute("INSERT INTO transactions (user_id, name, symbol, price, amount, cost, transaction_type, date_time) VALUES (:user_id, :name, :symbol, :price, :amount, :cost, :transaction_type, :date_time)", user_id=session["user_id"], name=symbol["name"], symbol=symbol["symbol"], price=sharePrice, amount=shares, cost=value, transaction_type="sell", date_time=date_time)
  57.  
  58. # Update the user's balance
  59. update = db.execute("UPDATE users SET cash=:newCash WHERE id=:user_id", newCash=newCash, user_id=session["user_id"])
  60.  
  61. # Update the user's holdings
  62. holding = db.execute("SELECT symbol FROM holdings WHERE symbol=:symbol AND user_id=:user_id", symbol=symbol["symbol"], user_id=session["user_id"])
  63.  
  64. # Get the current number of shares owned
  65. currentHoldings = db.execute("SELECT amount FROM holdings WHERE user_id=:user_id AND symbol=:symbol", user_id=session["user_id"], symbol=symbol["symbol"])
  66. finalAmount = currentAmount - shares
  67. updateHoldings = db.execute("UPDATE holdings SET amount=:finalAmount WHERE user_id=:user_id AND symbol=:symbol", user_id=session["user_id"], symbol=symbol["symbol"], finalAmount=finalAmount)
  68.  
  69. # Delete row if number of shares left is 0
  70. if finalAmount == 0:
  71. deleteHolding = db.execute("DELETE FROM holdings WHERE user_id=:user_id AND symbol=:symbol", user_id=session["user_id"], symbol=symbol["symbol"])
  72. return redirect("/")
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement