-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
226 lines (164 loc) · 6.62 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
import os
from datetime import datetime
import time
from flask import Flask, render_template, request, session, redirect, url_for, flash
from flask_login import current_user, login_user, logout_user, LoginManager, login_required
from flask_wtf import Form
from wtforms import StringField, PasswordField, SubmitField, FloatField, BooleanField
from wtforms.validators import DataRequired, Email, Length, EqualTo, ValidationError
from werkzeug.urls import url_parse
#Added imports for postgres hsoting
from flask_sqlalchemy import SQLAlchemy
from graph import graphMaker
app = Flask(__name__)
#Added initializations for postgres hosting
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
login = LoginManager(app)
login.login_view = 'signin'
@login.user_loader
def user_loader(id):
return User.query.get(id)
from models import *
#from forms import SignupForm
class SignupForm(Form):
first_name = StringField('First name', validators=[DataRequired("Please enter your first name.")])
last_name = StringField('Last name', validators=[DataRequired("Please enter your last name.")])
email = StringField('Email', validators=[DataRequired("Please enter your email address."), Email("Please enter your email address.")])
password = PasswordField('Password', validators=[DataRequired("Please enter a password."), Length(min=6, message="Passwords must be 6 characters or more.")])
initial_investment = FloatField('Initial Investment')
submit = SubmitField("Sign Up")
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError('Please use a different email address')
class LoginForm(Form):
email = StringField('Email', validators=[DataRequired("Please enter your email address."), Email("Please enter your email address.")])
password = PasswordField('Password', validators=[DataRequired("Please enter a password.")])
remember_me = BooleanField('Remember Me')
submit = SubmitField("Sign in")
class PersonalInfo(Form):
initial_investment = FloatField('Initial Investment', validators=[DataRequired("Please enter an amount. ")])
risk = BooleanField('Risk')
submit = SubmitField("Save")
#app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:trader@localhost/db'
#db.init_app(app)
app.secret_key = "development-key"
@app.route("/")
def index():
graphUrl = graphMaker()
return render_template("index.html", graphUrl=graphUrl)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/dashboard")
@login_required
def dashboard():
print('in dashboard')
user = current_user
#user = User.query.filter_by(email=session['email']).first()
if user is not None:
initial_investment = user.initial_investment
balance = user.balance
change = balance-initial_investment
percent = change/initial_investment
pos = percent >= 0
print('numbers')
print(initial_investment, balance, change)
graphUrl = graphMaker()
return render_template("dashboard.html", initial_investment=round(initial_investment, 2), balance=round(balance, 2), change=round(change, 2), risk=user.risk, graphUrl=graphUrl, percent = round(percent, 3), pos = pos)
else:
return redirect(url_for('index'))
#return render_template("dashboard.html")
@app.route("/personal-info", methods=["GET", "POST"])
def personal_info():
form = PersonalInfo()
if request.method == "POST":
if form.validate() == False:
return render_template('personal-info.html', form=form)
else:
current_user.initial_investment = form.initial_investment.data
current_user.balance = form.initial_investment.data
current_user.risk = form.risk.data
print ('!!!!!!! ', form.risk.data)
db.session.commit()
return redirect(url_for('dashboard'))
elif request.method == "GET":
return render_template('personal-info.html', form=form)
@app.route("/login", methods=["GET", "POST"])
def signin():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
email = form.email.data
password = form.password.data
user = User.query.filter_by(email=email).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('signin'))
login_user(user, remember=form.remember_me.data)
flash('Welcome back, ' + current_user.firstname)
return redirect(url_for('dashboard'))
return render_template('login.html', form=form)
'''
if request.method == "POST":
if form.validate() == False:
return render_template("login.html", form=form)
else:
email = form.email.data
password = form.password.data
#print(email, '\n', password)
user = User.query.filter_by(email=email).first()
#print(user, '\n', user.check_password(password))
if user is not None and user.check_password(password):
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index')
return redirect(next_page)
else:
flash('Invalid Username or Password')
return redirect(url_for('login'))
elif request.method == 'GET':'''
@app.route("/signup", methods=["GET", "POST"])
def signup():
if current_user.is_authenticated:
return redirect(url_for('dashboard'))
form = SignupForm()
if request.method == "POST":
if form.validate() == False:
return render_template('signup.html', form=form)
else:
newuser = User(form.first_name.data, form.last_name.data, form.email.data, form.password.data)
db.session.add(newuser)
db.session.commit()
login_user(newuser)
flash("Account created successfully!")
return redirect(url_for('personal_info'))
elif request.method == "GET":
return render_template('signup.html', form=form)
@app.route("/withdraw_funds")
@login_required
def profile():
balance = current_user.balance
return render_template("withdraw-funds.html", balance=round(balance, 2))
@app.route("/goodbye")
@login_required
def goodbye():
name = current_user.firstname
db.session.delete(current_user)
db.session.commit()
logout_user()
flash('Funds transferred and account closed successfully!')
return render_template("goodbye.html", name=name)
@app.route("/logout")
@login_required
def logout():
name = current_user.firstname
logout_user()
flash('Successfully logged out')
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(debug=True)