DragonOsman

typename test

Mar 24th, 2017
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. from cs50 import SQL
  2. from flask import Flask, flash, redirect, render_template, request, session, url_for
  3. from helpers import *
  4.  
  5. db = SQL("sqlite:///finance.db")
  6. app = Flask(__name__)
  7.  
  8. # ensure that entered stock symbol is valid
  9. stock = lookup("NFLX")
  10. if not stock:
  11.     apology("invalid symbol!")
  12.        
  13. # check if user has shares of that stock
  14. stock_query = db.execute("SELECT * FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=1, ssymbol=stock["symbol"])
  15. for i in range(len(stock_query)):
  16.     if stock_query[i]["stock_symbol"] != "NFLX":
  17.         apology("You don't have any shares of that stock!")
  18.        
  19. # ensure that user has enough shares of the stock
  20. total_shares = 0;
  21. for i in range(len(stock_query)):
  22.     total_shares += stock_query[i]["no_of_shares"]
  23.        
  24. if total_shares < 1:
  25.     apology("You don't have enough shares of that stock for this!")
  26. elif total_shares == int(request.form.got("shares")):
  27.     stock_query = db.execute("DELETE FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=1, ssymbol=stock["symbol"])
  28.        
  29. # check if subtracting that number of shares will leave the user with no shares of that stock left
  30. share_query = db.execute("SELECT no_of_shares FROM transactions WHERE user_id=:uid AND stock_symbol=:ssymbol", uid=1, ssymbol=stock["symbol"])
  31. for i in range(len(stock_query)):
  32.     for j in range(len(share_query)):
  33.         if total_shares - int(request.form.get("shares")) > 0:
  34.             db.execute("UPDATE transactions SET no_of_shares=:shares WHERE user_id=:uid AND stock_symbol=:ssymbol",
  35.             shares=total_shares - 1, uid=1, ssymbol="NFLX")
Advertisement
Add Comment
Please, Sign In to add comment