/
controller.py
753 lines (644 loc) · 27.6 KB
/
controller.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
"""
This file will be to create the app routes and framework of the app.
"""
import accounts
import json
import model
import requests
import seed
import utils
from flask import Flask, render_template, redirect, request, flash, g
from flask import session as f_session
from flask.ext.wtf import Form
from model import session as m_session
from passlib.hash import pbkdf2_sha512
from wtforms import TextField, IntegerField, PasswordField, validators
from wtforms.validators import DataRequired
app = Flask(__name__)
app.secret_key = open("flask_tokens.txt").read()
@app.before_request
def before_request():
if "email" in f_session:
g.status = "Log Out"
g.logged_in = True
g.email = f_session["email"]
g.user = m_session.query(model.User).filter_by(email=g.email).first()
if g.user.income is not None and g.user.company_401k is not None \
and g.user.company_match is not None and \
g.user.match_percent is not None and \
g.user.match_salary is not None and \
g.user.risk_profile_id is not None:
g.inputs = True
else:
g.inputs = False
else:
g.status = "Log In"
g.logged_in = False
@app.route("/")
def home_page():
return render_template("home.html")
class LoginForm(Form):
email = TextField("Email", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
@app.route("/login")
def show_login():
if g.logged_in is True:
flash("You are already logged in.")
return redirect("/")
else:
return render_template("login.html", form=LoginForm())
@app.route("/login", methods=["POST"])
def process_login():
form = LoginForm(request.form)
if form.validate_on_submit():
email = request.form["email"]
password = request.form["password"]
user = m_session.query(model.User).filter_by(email=email).first()
if user is not None:
hashed_password = user.password
verify_password = pbkdf2_sha512.verify(password, hashed_password)
if email == user.email and verify_password is True:
f_session["email"] = email
flash("Login successful.")
return redirect("/profile")
else:
flash("Incorrect username or password. Try again.")
return redirect("/login")
else:
flash("Please create an account first.")
return redirect("/create")
else:
flash("Please enter a valid email and password.")
return redirect("/login")
class CreateForm(Form):
email = TextField("Email", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
@app.route("/create")
def create_acct():
if g.logged_in is True:
flash("You are already logged in.")
return redirect("/")
else:
return render_template("create_acct.html", form=CreateForm())
@app.route("/create", methods=["POST"])
def process_acct():
form = CreateForm(request.form)
print "$$$$", form.validate_on_submit()
if form.validate_on_submit():
email = request.form["email"]
password = request.form["password"]
hashed_password = pbkdf2_sha512.encrypt(
password, salt=b'64', rounds=100000, salt_size=16)
# Checks that user isn't creating a duplicate account
user = m_session.query(model.User).filter_by(email=email).first()
if user is not None:
flash("That account already exists. Please log in.")
return redirect("/login")
else:
new_user_acct = model.User(email=email, password=hashed_password)
m_session.add(new_user_acct)
m_session.commit()
flash("Your account has been succesfully added.")
f_session["email"] = email
return redirect("/input/banking")
else:
flash("Please enter a valid email and password.")
return redirect("/create")
class BankLoginForm(Form):
user_name = TextField("Username", validators=[DataRequired()])
user_password = PasswordField("Password", validators=[DataRequired()])
@app.route("/banklogin")
def login_bank():
if g.logged_in is True:
return render_template("banking.html", form=BankLoginForm())
else:
return redirect("/login")
@app.route("/banklogin", methods=["POST"])
def access_bank():
"""
Allows login to banking institutions using Intuit API and
Python library aggcat. Calls functions in accounts.py.
Assumes that all account assets will be checking accounts.
"""
form = BankLoginForm(request.form)
if form.validate_on_submit():
institution = str(request.form["institution"])
username = request.form["user_name"]
password = request.form["user_password"]
user_fields = accounts.get_credential_fields(
accounts.create_client(), institution)
credentials = {}
credentials[user_fields["username"]] = username
credentials[user_fields["password"]] = password
try:
account = accounts.discover_add_account(
accounts.create_client(), institution, credentials)
account_data = account.content
# Checks the HTTP error code if account needs further
# authentication
if account.status_code in [200, 201]:
checking_balance = account_data.balance_amount
# Checks that user's assets are getting updated each time
# they change their input, and not getting added to the
# database.
user_assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).first()
if user_assets is not None:
update_assets = m_session.query(
model.UserBanking).filter_by(user_id=g.user.id).update(
{model.UserBanking.checking_amt: checking_balance})
else:
new_account = model.UserBanking(
user_id=g.user.id, inputted_assets=0,
checking_amt=checking_balance, savings_amt=0,
IRA_amt=0, comp401k_amt=0, investment_amt=0)
m_session.add(new_account)
m_session.commit()
flash("%s account XXXX%s with $%s has been added to your \
assets." % (account_data.account_nickname,
account_data.account_number[-4:],
account_data.balance_amount))
return redirect("/input/assets")
else:
return redirect("/banklogin/challenge")
except:
flash("There was an error accessing your account. Please try \
again.")
return redirect("/banklogin")
else:
flash("Please enter a valid email and password.")
return redirect("/banklogin")
@app.route("/banklogin/challenge")
def input_challenge():
if g.logged_in is True:
return render_template("banking_challenge.html")
else:
return redirect("/login")
@app.route("/banklogin/challenge", methods=["POST"])
def process_challenge():
"""
Authenticates access to banking institutions if there is a challenge
response with HTTP code 401.
"""
try:
institution = str(request.form["institution"])
username = request.form["user_name"]
password = request.form["user_password"]
# Responses must be in a list for XML to parse
responses = request.form[[challenge]]
user_fields = accounts.get_credential_fields(
accounts.create_client(), institution)
credentials = {}
credentials[user_fields["username"]] = username
credentials[user_fields["password"]] = password
account = accounts.discover_and_add_accounts(
accounts.create_client(), institution, credentials)
# Access "account" dictionary to pull the session and node id
challenge_session_id = account.headers["challengesessionid"]
challenge_node_id = account.headers["challengenodeid"]
confirmed_account = accounts.confirm_challenge(
create_client(), institution, challenge_session_id,
challenge_node_id, responses)
print accounts.content.account_nickname, \
accounts.content.account_number
checking_balance = confirmed_account.balance_amount
user_assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).first()
if user_assets is not None:
update_assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).update(
{model.UserBanking.checking_amt: checking_balance})
else:
new_account = model.UserBanking(
user_id=g.user.id, checking_amt=checking_balance)
m_session.add(new_account)
m_session.commit()
flash("%s account XXXX%s with $%s has been added to your assets."
% (confirmed_account.content.account_nickname,
confirmed_account.content.account_number[-4:],
confirmed_account.content.balance_amount))
return redirect("/input/assets")
except:
flash("There was an error authenticating your account. Please \
try again.")
return redirect("/banklogin/challenge")
@app.route("/input/banking")
def add_bank():
"""
This allows the user to add their external bank accounts.
"""
if g.logged_in is True:
return render_template("input_banking.html")
else:
return redirect("/login")
class AssetsForm(Form):
assets = IntegerField("How much money do you have?",
validators=[DataRequired()])
@app.route("/input/assets")
def input_assets():
"""
This allows the user to enter and edit their financial assets.
"""
if g.logged_in is True:
if g.inputs is True:
assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).first().inputted_assets
else:
assets = 0
return render_template("input_assets.html", form=AssetsForm(),
assets=assets)
else:
return redirect("/login")
@app.route("/input/assets", methods=["POST"])
def save_assets():
"""
Pulls assets from user input (as a post request), save to
database, and routes to next question (/results will perform
the calculations).
"""
form = AssetsForm(request.form)
if form.validate_on_submit():
assets = float(request.form["assets"])
# Checks that user's assets are getting updated each time they change
# their input, and not getting added to the database.
user_assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).first()
if user_assets is not None:
update_assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).update(
{model.UserBanking.inputted_assets: assets})
else:
new_account = model.UserBanking(
user_id=g.user.id, inputted_assets=assets, checking_amt=0,
savings_amt=0, IRA_amt=0, comp401k_amt=0, investment_amt=0)
m_session.add(new_account)
m_session.commit()
return redirect("/input/income")
else:
flash("Please enter an integer. No commas or symbols.")
return redirect("/input/assets")
class IncomeForm(Form):
income = IntegerField(
"What's your annual income?", validators=[DataRequired()])
@app.route("/input/income")
def input_income():
"""
This allows the user to enter and edit their income.
"""
if g.logged_in is True:
if g.inputs is True:
income = m_session.query(model.User).filter_by(
id=g.user.id).first().income
else:
income = 0
return render_template("input_income.html", form=IncomeForm(),
income=income)
else:
return redirect("/login")
@app.route("/input/income", methods=["POST"])
def save_income():
"""
Pulls income from user input (as a post request), save to
database, and routes to next question (/results will perform
the calculations).
"""
form = IncomeForm(request.form)
if form.validate_on_submit():
income = float(request.form["income"])
# Find user id using f_session and then update the database with the
# user's financial inputs
update_user = m_session.query(model.User).filter_by(
id=g.user.id).update({model.User.income: income})
m_session.commit()
return redirect("/input/comp_401k")
else:
flash("Please enter an integer. No commas or symbols.")
return redirect("/input/income")
@app.route("/input/comp_401k")
def input_comp_401k():
"""
This allows the user to enter and edit if their company has a 401k.
"""
if g.logged_in is True:
if g.inputs is True:
comp_401k = m_session.query(model.User).filter_by(
id=g.user.id).first().company_401k
else:
comp_401k = 0
return render_template(
"input_comp_401k.html", comp_401k=comp_401k)
else:
return redirect("/login")
@app.route("/input/comp_401k", methods=["POST"])
def save_comp_401k():
"""
Pulls company 401k from user input (as a post request), save to
database, and routes to next question (/results will perform
the calculations).
"""
comp_401k = request.form["401k"]
# Find user id using f_session and then update the database with the
# user's financial inputs
update_user = m_session.query(model.User).filter_by(
id=g.user.id).update({model.User.company_401k: comp_401k})
m_session.commit()
return redirect("/input/match_401k")
@app.route("/input/match_401k")
def input_match_401k():
"""
This allows the user to enter and edit if their company has a 401k
match.
"""
if g.logged_in is True:
# If user selects that they do not have a company 401k, skip
# all 401k-related questions.
comp_401k = m_session.query(model.User).filter_by(
id=g.user.id).first().company_401k
if comp_401k == "Yes":
if g.inputs is True:
match_401k = m_session.query(model.User).filter_by(
id=g.user.id).first().company_match
else:
match_401k = 0
return render_template(
"input_match_401k.html", match_401k=match_401k)
else:
match_401k = "No"
match_percent = match_salary = 0
update_user = m_session.query(model.User).filter_by(
id=g.user.id).update({model.User.company_match: match_401k,
model.User.match_percent: match_percent,
model.User.match_salary: match_salary})
m_session.commit()
return redirect("/input/risk_tolerance")
else:
return redirect("/login")
@app.route("/input/match_401k", methods=["POST"])
def save_match_401k():
"""
Pulls company 401k match from user input (as a post request), save
to database, and routes to next question (/results will perform
the calculations).
"""
match_401k = request.form["match"]
# Find user id using f_session and then update the database with the
# user's financial inputs
update_user = m_session.query(model.User).filter_by(
id=g.user.id).update({model.User.company_match: match_401k})
m_session.commit()
return redirect("/input/match_terms")
@app.route("/input/match_terms")
def input_match_terms():
"""
This allows the user to enter and edit the match percent and max
salary percent match of their 401k match.
"""
if g.logged_in is True:
# If user selects that they do not have a 401k match, skip
# all 401k match-related questions.
match_401k = m_session.query(model.User).filter_by(
id=g.user.id).first().company_match
if match_401k == "Yes":
if g.inputs is True:
match_percent = m_session.query(model.User).filter_by(
id=g.user.id).first().match_percent
match_salary = m_session.query(model.User).filter_by(
id=g.user.id).first().match_salary
else:
match_percent = match_salary = 0
return render_template(
"input_match_terms.html", match_percent=match_percent,
match_salary=match_salary)
else:
match_percent = match_salary = 0
update_user = m_session.query(model.User).filter_by(
id=g.user.id).update({model.User.match_percent: match_percent,
model.User.match_salary: match_salary})
m_session.commit()
return redirect("/input/risk_tolerance")
else:
return redirect("/login")
@app.route("/input/match_terms", methods=["POST"])
def save_match_terms():
"""
Pulls match percent and max salary percent match from user input
(as a post request), save to database, and routes to next question
(/results will perform the calculations).
"""
match_percent = float(request.form["match_percent"])
match_salary = float(request.form["salary_percent"])
# Find user id using f_session and then update the database with the
# user's financial inputs
update_user = m_session.query(model.User).filter_by(
id=g.user.id).update({model.User.match_percent: match_percent,
model.User.match_salary: match_salary})
m_session.commit()
return redirect("/input/risk_tolerance")
@app.route("/input/risk_tolerance")
def input_risk_tolerance():
"""
This allows the user to enter and edit their risk tolerance.
"""
if g.logged_in is True:
if g.inputs is True:
risk_tolerance_id = m_session.query(model.User).filter_by(
id=g.user.id).first().risk_profile_id
risk_tolerance = m_session.query(model.RiskProfile).filter_by(
id=risk_tolerance_id).first().name
else:
risk_tolerance = 0
return render_template(
"input_risk_tolerance.html", risk_tolerance=risk_tolerance)
else:
return redirect("/login")
@app.route("/input/risk_tolerance", methods=["POST"])
def save_risk_tolerance():
"""
Pulls risk tolerance from user input (as a post request),
save to database, and routes to next question (/results will perform
the calculations).
"""
risk_tolerance = request.form["risk_tolerance"]
risk_profile_id = m_session.query(model.RiskProfile).filter_by(
name=risk_tolerance).one().id
# Find user id using f_session and then update the database with the
# user's financial inputs
update_user = m_session.query(model.User).filter_by(
id=g.user.id).update({model.User.risk_profile_id: risk_profile_id})
m_session.commit()
return redirect("/results")
@app.route("/results")
def show_existing_results():
"""
This route accesses saved data from the database and
calculates the results.
"""
if g.logged_in is True:
if g.inputs is True:
user_assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).one()
total_assets = user_assets.inputted_assets + \
user_assets.checking_amt + user_assets.savings_amt + \
user_assets.IRA_amt + user_assets.comp401k_amt + \
user_assets.investment_amt
income = g.user.income
comp_401k = g.user.company_401k
match_401k = g.user.company_match
match_percent = g.user.match_percent
match_salary = g.user.match_salary
results = utils.calc_financial_results(
total_assets, income, comp_401k, match_401k, match_percent,
match_salary)
max_results = utils.calc_max_financials(
income, comp_401k, match_401k, match_percent, match_salary)
return render_template(
"results.html",
checking=utils.format_currency(results["checking"]),
savings=utils.format_currency(results["savings"]),
match=utils.format_currency(results["match"]),
ira=utils.format_currency(results["ira"]),
ret401k=utils.format_currency(results["ret401k"]),
investment=utils.format_currency(results["investment"]),
results=json.dumps(results),
max_results=json.dumps(max_results))
else:
flash("Our financial data on you is incomplete. \
Please input now.")
return redirect("/input/banking")
else:
return redirect("/login")
@app.route("/profile")
def show_existing_inputs():
"""
This shows the user's current saved inputs and allows them
to either move on or edit it.
"""
if g.logged_in is True:
if g.inputs is True:
user_assets = m_session.query(model.UserBanking).filter_by(
user_id=g.user.id).first()
total_assets = user_assets.inputted_assets + \
user_assets.checking_amt + user_assets.savings_amt + \
user_assets.IRA_amt + user_assets.comp401k_amt + \
user_assets.investment_amt
risk_prof = m_session.query(model.RiskProfile).filter_by(
id=g.user.risk_profile_id).first()
return render_template(
"profile_inputs.html",
assets=utils.format_currency(total_assets),
income=utils.format_currency(g.user.income),
company_401k=g.user.company_401k,
company_match=g.user.company_match,
match_percent=utils.format_percentage(g.user.match_percent),
match_salary=utils.format_percentage(g.user.match_salary),
risk_profile=risk_prof.name)
else:
flash("Our financial data on you is incomplete. \
Please input now.")
return redirect("/input/banking")
else:
return redirect("/login")
@app.route("/investments", methods=["GET", "POST"])
def show_investments():
"""
This shows the user's investment summary based on their risk profile
input.
GET request - summary includes a pie graph showing portfolio
allocation and line graph showing both total portfolio performance
and individual fund performance.
POST request - Allows the user to enter in a ticker as a comparison
point. Checks ticker against Stocks-GOOG.csv file to find ticker url,
make QUANDL API call, seed database, and plot data on line graph.
"""
if g.logged_in is True:
if g.inputs is True:
# Risk_prof is <Risk Profile ID=2 Name=Moderate>
risk_prof = m_session.query(model.RiskProfile).filter_by(
id=g.user.risk_profile_id).first()
chart_ticker_data = utils.generate_allocation_piechart(risk_prof)
dates = utils.generate_performance_linegraph(risk_prof)[0]
total_performance = utils.generate_performance_linegraph(
risk_prof)[1]
prof_ticker_data = utils.save_prof_tickers(risk_prof)
ticker_query_1 = utils.generate_individual_ticker_linegraph(
prof_ticker_data[0][0])
ticker_query_2 = utils.generate_individual_ticker_linegraph(
prof_ticker_data[0][1])
ticker_query_3 = utils.generate_individual_ticker_linegraph(
prof_ticker_data[0][2])
ticker_query_4 = utils.generate_individual_ticker_linegraph(
prof_ticker_data[0][3])
ticker_query_5 = utils.generate_individual_ticker_linegraph(
prof_ticker_data[0][4])
if request.method == "GET":
return render_template(
"investments.html",
risk_prof=risk_prof.name,
dates=json.dumps(dates),
total_performance=json.dumps(total_performance),
chart_ticker_data=json.dumps(chart_ticker_data),
prof_ticker_data=json.dumps(prof_ticker_data),
ticker_query_1=json.dumps(ticker_query_1),
ticker_query_2=json.dumps(ticker_query_2),
ticker_query_3=json.dumps(ticker_query_3),
ticker_query_4=json.dumps(ticker_query_4),
ticker_query_5=json.dumps(ticker_query_5))
else:
compare_ticker = request.form["compare_ticker"]
compare_ticker = compare_ticker.upper()
# Makes API call to seed database
filename = "seed_data/Stocks-GOOG.csv"
ticker_identifier_list = seed.find_ticker(
[compare_ticker], filename)
# Checks if ticker is in "Stocks-GOOG.csv"
if ticker_identifier_list != []:
check_ticker = m_session.query(model.Ticker).filter_by(
symbol=compare_ticker).first()
# Checks if ticker data already exists in the database.
# If it doesn't, make API call and seed database.
if check_ticker is None:
ticker_url_list = seed.build_ticker_url(
ticker_identifier_list)
seed.load_ticker_data(ticker_url_list, m_session)
utils.calc_percent_change_compare(
compare_ticker, m_session)
# Generates data in list to plot on line graph
compare_ticker_id = model.session.query(
model.Ticker).filter_by(
symbol=compare_ticker).first().id
compare_ticker_query = \
utils.generate_individual_ticker_linegraph(
compare_ticker_id)
return render_template(
"investments_compare.html",
risk_prof=risk_prof.name,
dates=json.dumps(dates),
total_performance=json.dumps(total_performance),
chart_ticker_data=json.dumps(chart_ticker_data),
prof_ticker_data=json.dumps(prof_ticker_data),
ticker_query_1=json.dumps(ticker_query_1),
ticker_query_2=json.dumps(ticker_query_2),
ticker_query_3=json.dumps(ticker_query_3),
ticker_query_4=json.dumps(ticker_query_4),
ticker_query_5=json.dumps(ticker_query_5),
compare_ticker_name=json.dumps([compare_ticker]),
compare_ticker_query=json.dumps(compare_ticker_query))
else:
flash("Sorry, that ticker does not exist in our \
database.")
return redirect("/investments")
else:
flash("Our financial data on you is incomplete. \
Please input now.")
return redirect("/input/banking")
else:
return redirect("/login")
@app.route("/logout")
def process_logout():
f_session.clear()
return redirect("/")
@app.route("/about")
def show_about():
return render_template("about.html")
if __name__ == "__main__":
app.run(debug=True)