-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.py
387 lines (289 loc) · 15.2 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#importaciones de funciones para ayudarnos
from library50 import cs50
import os
import sqlalchemy
from flask import Flask, flash, redirect, render_template, request, session, url_for
from flask_session import Session
from passlib.apps import custom_app_context as pwd_context
from tempfile import mkdtemp
from datetime import datetime
from pytz import timezone
#the * imports all the thins in helpers without specifing what
from helpers import *
# configure application, esto es para inicializar el programa
app = Flask(__name__)
# ensure responses aren't cached. so they go to the server to get fresh data and no to the cache memory
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# custom filter
app.jinja_env.filters["usd"] = usd
# configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# configure CS50 Library to use SQLite database
#db = SQL("sqlite:///finance.db")
db = SQL("postgres://bgpaphycyrjlid:7a88d94c94f9413c65eb77c227716ee66f48789f0d60e038493ba215a29c5ca4@ec2-54-83-58-222.compute-1.amazonaws.com:5432/daqaposu16bo6g")
#in order to acces to the route the user must be logeg in. that's why we use @login_required
@app.route("/")
@login_required
def index():
#save current user
currentUser1 = db.execute("SELECT * FROM users WHERE id = :idUser", idUser=session["user_id"])
#check if the history/portfolio table is empty
result = db.execute("SELECT COUNT(*) FROM history WHERE username = :username", username= currentUser1[0]["username"])
if result[0]["COUNT(*)"] == 0:
return render_template("index0stock.html")
#show user portfolio: show to user stocks owned, quanty of shares, current price of each, total value of each, balance
else:
#get from the database the symbols of the current user agroping the same symbols and countig them. save in a list called:
historyTable = db.execute("SELECT symbol, SUM(quanty) AS 'totalShares' , [symbol] FROM history WHERE username = :username GROUP BY symbol", username= currentUser1[0]["username"])
#print(historyTable)
#create empty list called positions and int variable to iterate:
positions = list()
n = 0
totalPrice = 0
#loop over the table and append the lookup function, and other values to a dict(called lookupSymbol) first and then to the positions list
for i in historyTable:
#look up the price and name of each symbol
lookupSymbol = lookup(historyTable[n]['symbol'])
#get the number of shares of each symbol
lookupSymbol['shares'] = historyTable[n]['totalShares']
#create a dict key with the TOTAL value of each share
lookupSymbol['total'] = lookupSymbol['price'] * lookupSymbol['shares']
#append current dict to the list positions
positions.append (lookupSymbol)
#move to the next share
n = n + 1
totalPrice = totalPrice + lookupSymbol['total']
totalCash = currentUser1[0]["cash"]
#print(positions)
total = currentUser1[0]["cash"] + totalPrice
return render_template("index.html", index= positions, totalPrice= totalPrice, totalCash= totalCash, total= total)
#return render_template("index.html", index= try2, name= symbolName['name'])
#get and post es para enviar y recibir informacion entre el cliente y servidor
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock."""
if request.method == "POST":
if request.form.get("symbol"):
#symbol of the stock to buy
symbolInput = lookup(request.form.get("symbol"))
#ensure symbol exist
if symbolInput == None:
return apology("Symbol doesn't exist")
#save the input of the shares number that user wants to buy in a variable and as a int
sharesQuanty = int(request.form.get("shares"))
#ensure that stocks to buy are 1 or grater
if sharesQuanty <= 0:
return apology("Must buy 1 or more shares")
#save price in a variable and as a float
priceShare = float(symbolInput['price'])
#save curren time
naive_dt = datetime.now(timezone('America/New_York'))
#save id of current user
idUser=session["user_id"]
#can user afford to buy? save info of current user
currentUser = db.execute("SELECT * FROM users WHERE id = :idUser", idUser=session["user_id"])
#make sure user has enought cash to buy the shares
totalPrice = priceShare * sharesQuanty
if (totalPrice > currentUser[0]["cash"]):
return apology("You don't have enought money!")
else:
#save name of user, symbol of shares, quantity and at what price he bought it
insert = db.execute("INSERT INTO history (username, symbol, quanty, price, date) VALUES (:username, :symbol, :quanty, :price, :date)", username = currentUser[0]["username"], symbol = symbolInput["symbol"], quanty = sharesQuanty, price = totalPrice, date = naive_dt)
#update user cash after the purchase
db.execute("UPDATE users SET cash = cash - :totalPrice WHERE id = :idUser", totalPrice=totalPrice, idUser=session["user_id"])
#redirect to index
return redirect(url_for("index"))
return render_template("buy.html")
@app.route("/history")
@login_required
def history():
"""Show history of transactions."""
#save current user
currentUser1 = db.execute("SELECT * FROM users WHERE id = :idUser", idUser=session["user_id"])
#query to check if table is empty or not
result1 = db.execute("SELECT COUNT(*) FROM history WHERE username = :username", username= currentUser1[0]["username"])
if result1[0]["COUNT(*)"] == 0:
return apology("empty table")
else:
#if is not empty save all the user history table in result.
result = db.execute("SELECT symbol, quanty, price, date FROM history WHERE username = :username", username= currentUser1[0]["username"])
print(result)
return render_template("history.html", history= result)
#pass all the variables needed to html
return render_template("history.html")
#login is already done (use them as a example of how to implement the others ones)
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in."""
# forget any user_id
session.clear()
# if user reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# ensure username was submitted
if not request.form.get("username"):
return apology("must provide username") #calling apology function
# ensure password was submitted
elif not request.form.get("password"):
return apology("must provide password")
# query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
# ensure username exists and password is correct
if len(rows) != 1 or not pwd_context.verify(request.form.get("password"), rows[0]["hash"]):
return apology("invalid username and/or password")
# remember which user has logged in
session["user_id"] = rows[0]["id"]
#SAVE USERNAME
session["user_name"] = rows[0]["username"]
# redirect user to home page
return redirect(url_for("index"))
# else if user reached route via GET (as by clicking a link or via redirect)
else:
return render_template("login.html")
#logout is already done
@app.route("/logout")
def logout():
"""Log user out."""
# forget any user_id
session.clear()
# redirect user to login form
return redirect(url_for("login"))
#display form, retriveve stock quote, display stock quote
@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
"""Get stock quote."""
#return render_template("quote.html")
if request.method == "POST":
#look in yahoo finance for requested symbol
if request.form.get("symbol"):
#return the dictnionary found. (name, price, symbol)
symbolInput = lookup(request.form.get("symbol"))
#print example accesing only one key in the dict
#print (symbolInput['name'])
#ensure symbol is not none
if symbolInput != None:
return render_template("stock.html", name= symbolInput['name'], price= symbolInput['price'], symbol= symbolInput['symbol'])
#return to quote html with message symbol dosent exist if symbolInput = None
#return apology("SKULKER")
return render_template("quote.html")
#display a form so the user can register a account. check if password is valid and add the user to database
#log them in
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user."""
if request.method == "POST":
# ensure username was submitted
if not request.form.get("username"):
return failure("must provide username") #calling apology function
if request.form.get("password") != request.form.get("password2"):
return failure("must provide username")
# we dont store de password that they type in to add security, we store a hash
hash = pwd_context.hash(request.form.get("password"))
#save data in table
result = db.execute("INSERT INTO users (username, hash) VALUES (:username, :hash)", username = request.form.get("username"), hash = hash)
#check for failure becouse of same username
if not result:
return badusername("Username already exist")
else:
#log them in automatically if they register
#session["user_id"] = rows[0]["id"]
# query database for username
rows = db.execute("SELECT * FROM users WHERE username = :username", username=request.form.get("username"))
session["user_id"] = rows[0]["id"]
#redirect user to homepage
return redirect(url_for("index"))
return render_template("register.html")
#return render_template("login.html")
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock."""
#save current user
currentUser1 = db.execute("SELECT * FROM users WHERE id = :idUser", idUser=session["user_id"])
#check if the history/portfolio table is empty
result = db.execute("SELECT COUNT(*) FROM history WHERE username = :username", username= currentUser1[0]["username"])
if result[0]["COUNT(*)"] == 0:
return render_template("index0stock.html")
else:
historyTable = db.execute("SELECT symbol, SUM(quanty) AS 'totalShares' , [symbol] FROM history WHERE username = :username GROUP BY symbol", username= currentUser1[0]["username"])
#print(historyTable)
#create empty list called positions and int variable to iterate:
positions = list()
n = 0
totalPrice = 0
#loop over the table and append the lookup function, and other values to a dict(called lookupSymbol) first and then to the positions list
for i in historyTable:
#look up the price and name of each symbol
lookupSymbol = lookup(historyTable[n]['symbol'])
#get the number of shares of each symbol
lookupSymbol['shares'] = historyTable[n]['totalShares']
#create a dict key with the TOTAL value of each share
lookupSymbol['total'] = lookupSymbol['price'] * lookupSymbol['shares']
#append current dict to the list positions
positions.append (lookupSymbol)
#move to the next share
n = n + 1
totalPrice = totalPrice + lookupSymbol['total']
#print(totalPrice)
if request.method == "POST":
#save the input of the shares number that user wants to buy in a variable and as a int
symbolName = request.form.get("symbol")
numberToSell = int(request.form.get("shares"))
#sell only if the input is 1 or more shares
if numberToSell <= 0:
return apology("Sell one or more shares")
if request.form["symbol"] == "" or request.form["shares"] == "":
return apology("Symbol or number of shares are incorrect")
#search for symbol in user list. (check if the user has at least 1 share)
if any(d['symbol'] == symbolName.upper() for d in positions):
shares_index = next(index for (index, d) in enumerate(positions) if d["symbol"] == symbolName.upper())
#check if el numero que queremos vender es igual o menor a lo que tenemos
if numberToSell <= positions[shares_index]['shares']:
print(positions)
print(positions[shares_index]['price'])
updateCash = positions[shares_index]['price'] * numberToSell
print(updateCash)
totalPrice = updateCash
#generate a negative purchase, query
insert = db.execute("INSERT INTO history (username, symbol, quanty, price, date) VALUES (:username, :symbol, :quanty, :price, :date)", username = currentUser1[0]["username"], symbol = symbolName.upper(), quanty = (numberToSell * -1), price = (totalPrice * -1), date = datetime.now())
#update user cash after selling the shares
#db.execute("UPDATE users SET cash = cash + :sellPrice WHERE id = :idUser", sellPrice= updateCash, idUser=session["user_id"])
db.execute("UPDATE users SET cash = cash + :totalPrice WHERE id = :idUser", totalPrice=totalPrice, idUser=session["user_id"])
return redirect(url_for("index"))
else:
return apology("You don't have that many shares")
return render_template("sell.html", index= positions, totalPrice= totalPrice)
class SQL(object):
def __init__(self, url):
try:
self.engine = sqlalchemy.create_engine(url)
except Exception as e:
raise RuntimeError(e)
def execute(self, text, *multiparams, **params):
try:
statement = sqlalchemy.text(text).bindparams(*multiparams, **params)
result = self.engine.execute(str(statement.compile(compile_kwargs={"literal_binds": True})))
# SELECT
if result.returns_rows:
rows = result.fetchall()
return [dict(row) for row in rows]
# INSERT
elif result.lastrowid is not None:
return result.lastrowid
# DELETE, UPDATE
else:
return result.rowcount
except sqlalchemy.exc.IntegrityError:
return None
except Exception as e:
raise RuntimeError(e)