-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
142 lines (104 loc) · 4.42 KB
/
helpers.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
import os
import requests
import urllib.parse
from cs50 import SQL
from flask import redirect, render_template, request, session
from functools import wraps
from datetime import datetime
db = SQL("sqlite:///finance.db")
def apology(message, code=400):
"""Render message as an apology to user."""
def escape(s):
"""
Escape special characters.
https://github.com/jacebrowning/memegen#special-characters
"""
for old, new in [("-", "--"), (" ", "-"), ("_", "__"), ("?", "~q"),
("%", "~p"), ("#", "~h"), ("/", "~s"), ("\"", "''")]:
s = s.replace(old, new)
return s
return render_template("apology.html", top=code, bottom=escape(message)), code
def login_required(f):
"""
Decorate routes to require login.
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
def lookup(symbol):
"""Look up quote for symbol."""
# Contact API
try:
api_key = os.environ.get("API_KEY")
url = f"https://cloud.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}"
response = requests.get(url)
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
quote = response.json()
return {
"name": quote["companyName"],
"price": float(quote["latestPrice"]),
"symbol": quote["symbol"]
}
except (KeyError, TypeError, ValueError):
return None
def usd(value):
"""Format value as USD."""
return f"${value:,.2f}"
def getPortfolio(userid):
testPortfolio = db.execute("SELECT symbol, shares FROM portfolio WHERE id = ?", userid)
return testPortfolio
def getCash(userid):
cash = db.execute("SELECT cash FROM users WHERE id = ?", userid)[0]["cash"]
return cash
def setCash(cash, total_price_setCash, userid):
total_cash = round(cash + total_price_setCash, 2)
db.execute("UPDATE users SET cash = ? WHERE id = ?", total_cash, userid)
def adjustPortfolio(userid, symbol, shares_wanted):
# Get date for purchase history
date_today_verbose = datetime.now()
date_today = date_today_verbose.strftime("%d/%m/%Y/ %H:%M:%S")
# Get stock price
stock_info = lookup(symbol)
price = stock_info["price"]
cash = getCash(userid)
if shares_wanted >= 1:
total_price = round(price * shares_wanted, 2)
total_price_setCash = round(price * shares_wanted * -1, 2)
if total_price > cash:
return apology("not enough cash", 403)
else:
total_price = round(price * shares_wanted * -1, 2)
total_price_setCash = round(price * shares_wanted * -1, 2)
# Insert into puchase history table on fincance database
db.execute("INSERT INTO purchase_history (id, date, symbol, shares, price, total_price) VALUES (?, ?, ?, ?, ?, ?)", userid, date_today, symbol, shares_wanted, price, total_price)
try:
shares_owned = db.execute("SELECT shares FROM portfolio WHERE symbol = ?", symbol)[0]["shares"]
except (KeyError, TypeError, ValueError, IndexError):
db.execute("INSERT INTO portfolio (id, symbol, shares) VALUES (?, ?, ?)", userid, symbol, shares_wanted)
setCash(cash, total_price_setCash, userid)
return goHome()
if shares_owned <= 0:
db.execute("DELETE FROM portfolio WHERE symbol = ?", symbol)
if shares_owned + shares_wanted == 0:
db.execute("DELETE FROM portfolio WHERE symbol = ?", symbol)
setCash(cash, total_price_setCash, userid)
elif shares_owned + shares_wanted < 0:
return apology("not enough shares", 403)
elif shares_owned + shares_wanted > 0:
shares = shares_owned + shares_wanted
db.execute("UPDATE portfolio SET shares = ? WHERE symbol = ? AND id = ?", shares, symbol, userid)
setCash(cash, total_price_setCash, userid)
return goHome()
return goHome()
def goHome():
cash = getCash(session["user_id"])
portfolio = getPortfolio(session["user_id"])
return render_template("index.html", cash=cash, portfolio=portfolio)