Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @app.route("/sell", methods=["GET", "POST"])
- @login_required
- def sell():
- """Sell shares of stock"""
- if request.method == "GET":
- # Dropdown menu options for the list of stocks owned
- holdings = db.execute("SELECT symbol FROM holdings WHERE user_id=:user_id", user_id=session["user_id"])
- return render_template("sell.html", holdings=holdings)
- else:
- # Stock sold
- soldSymbol = request.form.get("sell")
- # Check if user selected a stock
- if not soldSymbol:
- return apology("Please select a stock.")
- # Lookup symbol
- symbol = lookup(soldSymbol)
- # Get the current number of shares owned
- currentHoldings = db.execute("SELECT amount FROM holdings WHERE user_id=:user_id AND symbol=:symbol", user_id=session["user_id"], symbol=symbol["symbol"])
- currentAmount = int(currentHoldings[0]["amount"])
- # Shares sold
- shares = request.form.get("shares")
- if not shares:
- return apology("Please select a number of stocks to sell.")
- # If user sold too many shares
- if shares > currentAmount:
- return apology("You have sold too many shares.")
- # Price of one share
- sharePrice = symbol["price"]
- # Total value sold, number of shares sold * price of one share
- value = shares * sharePrice
- # Current balance of user
- userCash = db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=session["user_id"])
- # New balance of user after transaction
- userCash = float(userCash[0]['cash'])
- newCash = userCash + value
- # Log into transactions
- # Date and time
- now = datetime.datetime.now()
- date_time = now.strftime("%d/%m/%Y %H:%M:%S")
- # Insert transaction details into the transaction table
- 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)
- # Update the user's balance
- update = db.execute("UPDATE users SET cash=:newCash WHERE id=:user_id", newCash=newCash, user_id=session["user_id"])
- # Update the user's holdings
- holding = db.execute("SELECT symbol FROM holdings WHERE symbol=:symbol AND user_id=:user_id", symbol=symbol["symbol"], user_id=session["user_id"])
- # Get the current number of shares owned
- currentHoldings = db.execute("SELECT amount FROM holdings WHERE user_id=:user_id AND symbol=:symbol", user_id=session["user_id"], symbol=symbol["symbol"])
- finalAmount = currentAmount - shares
- 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)
- # Delete row if number of shares left is 0
- if finalAmount == 0:
- deleteHolding = db.execute("DELETE FROM holdings WHERE user_id=:user_id AND symbol=:symbol", user_id=session["user_id"], symbol=symbol["symbol"])
- return redirect("/")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement