-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
119 lines (94 loc) · 2.96 KB
/
Copy pathhelpers.py
File metadata and controls
119 lines (94 loc) · 2.96 KB
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
import os
import requests
import urllib.parse
from flask import redirect, render_template, request, session
from functools import wraps
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.
http://flask.pocoo.org/docs/1.0/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")
response = requests.get(
f"https://cloud-sse.iexapis.com/stable/stock/{urllib.parse.quote_plus(symbol)}/quote?token={api_key}"
)
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 get_all_symbols():
"""Get all stock symbols."""
# Contact API
try:
api_key = os.environ.get("API_KEY")
response = requests.get(
f"https://cloud-sse.iexapis.com/stable/ref-data/symbols?token={api_key}"
)
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
symbols = []
stock_data = response.json()
for stock in stock_data:
symbols.append(stock["symbol"])
return symbols
except (KeyError, TypeError, ValueError):
return None
def usd(value):
"""Format value as USD."""
return f"${value:,.2f}"
def validate_transaction(symbol=None, shares=None):
# validate the transaction and get the current stock data
# ensure the user has entered a stock symbol
if not symbol:
return ("must provide a stock symbol", 418)
# ensure the user has submitted a number of shares
if not shares:
return ("must provide an amount of shares", 418)
# validate that user is entering a number of shares
# the form itself should also protect against this
try:
shares = int(shares)
except ValueError:
return ("must provide a number of shares", 418)