-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapp.py
426 lines (322 loc) · 13.4 KB
/
app.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
"""
https://cs50.harvard.edu/x/2023/psets/9/finance/
"""
import re
import os
from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session
from werkzeug.security import generate_password_hash, check_password_hash
from helpers import apology, login_required, lookup, usd, get_time, check_password
from flask_session import Session
# Configure application
app = Flask(__name__)
# Custom filter
app.jinja_env.filters["usd"] = usd
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///finance.db")
# Make sure API key is set
if not os.environ.get("API_KEY"):
raise RuntimeError("API_KEY not set")
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
@login_required
def index():
"""Show portfolio of stocks."""
user_id = session["user_id"]
portfolio = db.execute("SELECT * FROM portfolios WHERE user_id = ?", user_id)
cash_left = db.execute("SELECT cash FROM users WHERE id = ?", user_id)
# Getting the amount of cash the user has left to spend
if cash_left and "cash" in cash_left[0]:
cash_left = float(cash_left[0]["cash"])
else:
cash_left = 0.0
total_amount = cash_left
# Updating the current price and the overall stock value for each stock to be displayed in real time
try:
for stock in portfolio:
symbol = stock["symbol"]
stock_info = lookup(symbol)
current_price = float(stock_info["price"])
stock_value = current_price * stock["shares"]
stock.update({"current_price": current_price, "stock_value": stock_value})
total_amount += float(stock["stock_value"])
except (ValueError, LookupError):
return apology("Failed to update stock prices!")
return render_template(
"index.html",
portfolio=portfolio,
cash_left=cash_left,
total_amount=total_amount,
)
@app.route("/buy", methods=["GET", "POST"])
@login_required
def buy():
"""Buy shares of stock."""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
user_id = session["user_id"]
symbol = request.form.get("symbol")
stock = lookup(symbol)
shares = request.form.get("shares")
# Checking for symbol to be valid
if not symbol or not stock:
return apology("Symbol is not valid!")
# Checking for shares number to be positive
if not shares.isdigit():
return apology("Number of shares must be a positive digit!")
transaction_value = int(shares) * stock["price"]
user_cash = db.execute("SELECT cash FROM users WHERE id = ?", user_id)
user_cash = user_cash[0]["cash"]
# Making sure user has enough cash to buy the shares
if user_cash < transaction_value + 1:
return apology("Not enough money!", 401)
# Perform the aquisition and update database
update_user_cash = user_cash - transaction_value
db.execute("UPDATE users SET cash = ? WHERE id = ?", update_user_cash, user_id)
# Format balance
balance = f"${update_user_cash:,.2f} (-${transaction_value:,.2f})"
# Add transaction to portfolio database
db.execute(
"INSERT INTO portfolios (user_id, name, symbol, shares, paid_price, current_price, date, stock_value) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
user_id,
stock["name"],
symbol,
shares,
stock["price"],
stock["price"],
get_time(),
stock["price"],
)
# Add transaction to history database
db.execute(
"INSERT INTO history (user_id, name, symbol, shares, action, balance, date) VALUES (?, ?, ?, ?, ?, ?, ?)",
user_id,
stock["name"],
symbol,
shares,
"BOUGHT",
balance,
get_time(),
)
flash(f"Successfully bought {shares} shares of {symbol}!")
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("buy.html")
@app.route("/history")
@login_required
def history():
"""Show history of transactions."""
user_id = session["user_id"]
portfolio = db.execute("SELECT * FROM history WHERE user_id = ?", user_id)
return render_template("history.html", portfolio=portfolio)
@app.route("/login", methods=["GET", "POST"])
def login():
"""Log user in."""
# Forget any user_id
session.clear()
# 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!", 403)
# Ensure password was submitted
if not request.form.get("password"):
return apology("Must provide password!", 403)
# Query database for username
rows = db.execute(
"SELECT * FROM users WHERE username = ?", request.form.get("username")
)
# Ensure username exists and password is correct
if len(rows) != 1 or not check_password_hash(
rows[0]["hash"], request.form.get("password")
):
return apology("invalid username and/or password", 403)
# Remember which user has logged in
session["user_id"] = rows[0]["id"]
# Redirect user to home page
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("login.html")
@app.route("/logout")
def logout():
"""Log user out."""
# Forget any user_id
session.clear()
# Redirect user to login form
return redirect("/")
@app.route("/quote", methods=["GET", "POST"])
@login_required
def quote():
"""Get stock quote."""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
# Search details about the stock
stock = lookup(str(request.form.get("symbol")))
# Check for empty field
if not stock:
return apology("Invalid symbol!")
# Format price
stock["price"] = usd(stock["price"])
return render_template("quoted.html", stock=stock)
# User reached route via GET (as by clicking a link or via redirect)
return render_template("quote.html")
@app.route("/register", methods=["GET", "POST"])
def register():
"""Register user."""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
# Check for empty fields
if any(not field for field in [username, password, confirmation]):
return apology("Fields cannot be empty!")
# Ensure username is at least 4 characters long
if len(username) < 4:
return apology("Username must be at least 4 characters long!", 403)
# Ensure username consists only of characters and digits
if not username.isalnum():
return apology("Username must contain only characters and digits!", 403)
# Ensure password is stronger (has characters, digits, symbols)
if len(password) < 8:
return apology("Password must be at least 8 characters long!", 403)
if (
not re.search("[a-zA-Z]", password)
or not re.search("[0-9]", password)
or not re.search("[!@#$%^&*()]", password)
):
return apology("Password must contain characters, digits and symbols!", 403)
# Check for password to be the same
if password != confirmation:
return apology("Passwords do not match!", 400)
# Make sure the name isn't registered already or the field is empty
if len(db.execute("SELECT * FROM users WHERE username = ?", username)) > 0:
return apology("Username already taken!", 400)
# Hash password
hashed_password = generate_password_hash(password)
# Add username & hashed password in the database
db.execute(
"INSERT INTO users (username, hash) VALUES (?, ?)",
username,
hashed_password,
)
# Remember which user has logged in
rows = db.execute("SELECT * FROM users WHERE username = ?", username)
session["user_id"] = rows[0]["id"]
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("register.html")
@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
"""Sell shares of stock."""
user_id = session["user_id"]
portfolio = db.execute("SELECT * FROM portfolios WHERE user_id = ?", user_id)
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
symbol = request.form.get("symbol")
stock = lookup(symbol)
shares = int(request.form.get("shares"))
owned_stock = db.execute(
"SELECT shares FROM portfolios WHERE user_id = ? AND symbol = ?",
user_id,
symbol,
)
# Check if user owns shares of the stock
if not owned_stock:
return apology(f"You don't own any shares of {symbol}!")
# Check if user has enough shares to sell
current_shares = sum([stock["shares"] for stock in owned_stock])
if current_shares < shares:
return apology("You don't have enough shares to sell!")
# Retrieve user's balance
cash = db.execute("SELECT cash FROM users WHERE id = ?", user_id)
cash = cash[0]["cash"]
# Deposit value of sold shares
current_price = stock["price"]
cash += shares * current_price
# Perform the sale
for info in owned_stock:
# Update database if user sells less shares than the total amount he owns
if info["shares"] > shares:
db.execute(
"UPDATE portfolios SET shares = ? WHERE user_id = ? AND symbol = ?",
info["shares"] - shares,
user_id,
symbol,
)
# Delete stock from portfolio if all shares were sold
else:
db.execute(
"DELETE FROM portfolios WHERE user_id = ? AND symbol = ?",
user_id,
symbol,
)
# Format balance
balance = f"${cash:,.2f} (+${(shares * current_price):,.2f})"
# Update user's cash balance
db.execute("UPDATE users SET cash = ? WHERE id = ?", cash, user_id)
# Add transaction to history database
db.execute(
"INSERT INTO history (user_id, name, symbol, shares, action, balance, date) VALUES (?, ?, ?, ?, ?, ?, ?)",
user_id,
stock["name"],
symbol,
shares,
"SOLD",
balance,
get_time(),
)
flash(f"Successfully sold {shares} shares of {symbol}!")
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("sell.html", portfolio=portfolio)
@app.route("/deposit", methods=["GET", "POST"])
@login_required
def deposit():
"""Deposit funds to account."""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
user_id = session["user_id"]
amount = int(request.form.get("sum"))
account = db.execute("SELECT * FROM users WHERE id = ?", user_id)
# Ensure password is correct
check_password(account[0]["hash"], request.form.get("password"))
# Add funds to account
cash = account[0]["cash"] + amount
db.execute("UPDATE users SET cash = ? WHERE id = ?", cash, user_id)
flash(f"Successfully added ${amount} to your balance!")
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("deposit.html")
@app.route("/withdraw", methods=["GET", "POST"])
@login_required
def withdraw():
"""Withdraw funds from account."""
# User reached route via POST (as by submitting a form via POST)
if request.method == "POST":
user_id = session["user_id"]
amount = int(request.form.get("sum"))
account = db.execute("SELECT * FROM users WHERE id = ?", user_id)
# Ensure password is correct
check_password(account[0]["hash"], request.form.get("password"))
# Ensure user cannot withdraw more than left cash
if amount > account[0]["cash"]:
return apology("Cannot withdraw more than cash left!")
# Withdraw funds from account
cash = account[0]["cash"] - amount
db.execute("UPDATE users SET cash = ? WHERE id = ?", cash, user_id)
flash(f"Successfully withdrew ${amount} from your balance!")
return redirect("/")
# User reached route via GET (as by clicking a link or via redirect)
return render_template("withdraw.html")