Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cs50 import SQL
- from flask import Flask, flash, redirect, render_template, request, session, url_for
- from helpers import *
- db = SQL("sqlite:///finance.db")
- app = Flask(__name__)
- # ensure that entered stock symbol is valid
- stock = lookup("NFLX")
- if not stock:
- apology("invalid symbol!")
- # check if user has shares of that stock
- stock_query = db.execute("SELECT * FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=1, ssymbol=stock["symbol"])
- for i in range(len(stock_query)):
- if stock_query[i]["stock_symbol"] != "NFLX":
- apology("You don't have any shares of that stock!")
- # ensure that user has enough shares of the stock
- total_shares = 0;
- for i in range(len(stock_query)):
- total_shares += stock_query[i]["no_of_shares"]
- if total_shares < 1:
- apology("You don't have enough shares of that stock for this!")
- elif total_shares == int(request.form.got("shares")):
- stock_query = db.execute("DELETE FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=1, ssymbol=stock["symbol"])
- # check if subtracting that number of shares will leave the user with no shares of that stock left
- share_query = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=1, ssymbol=stock["symbol"])
- for i in range(len(stock_query)):
- for j in range(len(share_query)):
- if total_shares - int(request.form.get("shares")) > 0:
- db.execute("UPDATE transactions SET no_of_shares=:shares WHERE user_id=:uid AND stock_symbol=:ssymbol",
- shares=total_shares - 1, uid=1, ssymbol="NFLX")
Advertisement
Add Comment
Please, Sign In to add comment