-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
412 lines (304 loc) · 13.9 KB
/
server.py
File metadata and controls
412 lines (304 loc) · 13.9 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
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
from flask import Flask, request, render_template, redirect, flash, session, jsonify
from model import db, User, Activity, Occurrence, connect_to_db, sign_in_user
from flask_debugtoolbar import DebugToolbarExtension
from datetime import datetime
import os
import pytz
import bcrypt
app = Flask(__name__)
app.secret_key = os.environ["SECRET_KEY"]
@app.before_request
def check_signed_in():
"""Check that user is logged in before loading pages which should only be
accessible when logged in. If not, redirect to sign-in page."""
public_routes = { "/", "/signup", "/signin", "/static/style.css", "/static/landing.css", "/static/heart.jpg" }
if request.path not in public_routes and not session.get("user_id"):
flash("You need to be logged in to view this page. Please log in.")
return redirect("/signin")
@app.route("/")
def show_landing_page():
"""Return landing page."""
return render_template("landing.html")
@app.route("/signup", methods=["GET"])
def display_signup_form():
"""Return form for signing up for an account."""
return render_template("signup.html", hide=True)
@app.route("/signup", methods=["POST"])
def signup_user():
"""Process signup form, adding user to database."""
username = request.form.get("username")
password = request.form.get("password")
email = request.form.get("email")
phone_number = request.form.get("phone-number")
age = request.form.get("age")
# Check database for pre-existing account by checking for a user with the
# provided email address
if User.query.filter(User.email == email).all():
flash("Looks like you've already registered. If you mistyped, please try again.")
return redirect("/signup")
else:
# Generate salt and hash password to store hashed password in database
password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
# Age and phone number are optional
if age and phone_number:
new_user = User(user_handle=username,
password=password,
email=email,
phone_number=phone_number,
age=age)
elif age:
new_user = User(user_handle=username,
password=password,
email=email,
age=age)
elif phone_number:
new_user = User(user_handle=username,
password=password,
email=email,
phone_number=phone_number)
else:
new_user = User(user_handle=username,
password=password,
email=email)
db.session.add(new_user)
db.session.commit()
# Immediately sign in new user; otherwise, redirecting to /setup would
# fail the @app.before_request sign-in check, and user would be
# redirected to /signin instead
sign_in_user(email)
return redirect("/setup")
@app.route("/setup", methods=["GET"])
def request_activity_types():
"""Display form for user to choose the activities they want to track."""
return render_template("setup.html")
@app.route("/setup", methods=["POST"])
def create_activity_types():
"""Process setup form, adding user-defined activity types to database."""
activity_1 = request.form.get("activity_1")
activity_2 = request.form.get("activity_2")
activity_3 = request.form.get("activity_3")
activity_4 = request.form.get("activity_4")
activity_5 = request.form.get("activity_5")
activity_6 = request.form.get("activity_6")
activity_7 = request.form.get("activity_7")
activity_8 = request.form.get("activity_8")
activity_9 = request.form.get("activity_9")
activity_10 = request.form.get("activity_10")
activities = [activity_1, activity_2, activity_3, activity_4, activity_5,
activity_6, activity_7, activity_8, activity_9, activity_10]
new_activities = []
for activity in activities:
# User can enter as few as one; if None, it will not be added to the
# database
if activity:
new_activities.append(activity)
# Handle submission of form with no values
if new_activities == []:
flash("""You need to enter at least one activity to continue.
Please make a selection and try again.""")
return redirect("/setup")
# For every activity user provides, add activity to the database
else:
for activity in new_activities:
new_activity = Activity(activity_type=activity,
user_id=session["user_id"])
db.session.add(new_activity)
db.session.commit()
flash("Great! Looks like you're ready to start tracking!")
return redirect("/main")
@app.route("/signin", methods=["GET"])
def display_signin_form():
"""Display form for logging into existing account."""
return render_template("signin.html", hide=True)
@app.route("/signin", methods=["POST"])
def signin_user():
"""Handle sign-in."""
provided_password = request.form.get("provided-password")
email = request.form.get("email")
# Check that a user with the provided email address is already in database
if User.query.filter(User.email == email).all():
# If so, check that the provided password produces the same hash as
# what's stored in the database for the user with the provided email
# address when the salt (stored in the hash) is applied to it
stored = User.query.filter(User.email == email).one().password
provided_password = provided_password.encode('utf8')
provided_after_salt = bcrypt.hashpw(provided_password, stored)
if stored == provided_after_salt:
# If so, get the user's user_id and user_handle and store them on
# the session
sign_in_user(email)
flash("Thanks for logging in!")
return redirect("/main")
else:
flash("Sorry, we didn't find an account with the email and password\
you provided. Please try again.")
return render_template("signin.html")
else:
flash("Sorry, we didn't find an account with the email and password you\
provided. Please try again.")
return render_template("signin.html")
@app.route("/main", methods=["GET"])
def show_main_page():
"""Load main page."""
# Get activities for dropdown menu for choosing one to plan
activities = Activity.query.filter(Activity.user_id == session['user_id']
).all()
# Get user's occurrences without end times & before ratings to display as
# links so user can click to complete them
user = User.query.filter(User.user_id == session['user_id']).one()
planned_occurrences = user.get_planned_occurrences()
# Get list of user's completed occurrences for rendering charts
completed_occurrences = user.get_completed_occurrences()
# Charts should only be rendered for activities with at least one completed
# occurrence
used_activities = set([])
for occurrences in completed_occurrences:
used_activities.add(occurrences.activity)
used_activities = list(used_activities)
# Charts should be displayed in a consistent order; this alphabetizes them
used_activities.sort(key=lambda x: x.activity_type)
return render_template("/main.html",
activities=activities,
planned_occurrences=planned_occurrences,
completed_occurrences=completed_occurrences,
used_activities=used_activities)
@app.route("/plan_activity", methods=["POST"])
def handle_activity_choice():
"""Handle form for planning an activity."""
activity_id = request.form.get("activity-choice")
return redirect("/record_before/" + activity_id)
@app.route("/record_before/<activity_id>", methods=["GET"])
def display_before_form(activity_id):
"""Display form for creating a new occurrence."""
# Get activity_type to display as a heading
activity = Activity.query.filter(Activity.activity_id == activity_id).one()
activity_type = activity.activity_type
# Get current date and time so that user can quickly select these using now
# button if desired
pacific = pytz.timezone('US/Pacific')
now = datetime.now(tz=pacific)
now_date = datetime.strftime(now, "%Y-%m-%d")
now_time = datetime.strftime(now, "%I:%M %p")
return render_template("/record_before.html",
activity_id=activity_id,
now_date=now_date,
now_time=now_time,
activity_type=activity_type)
@app.route("/record_before/<activity_id>", methods=["POST"])
def get_before_values(activity_id):
"""Process form, creating a new occurrence and saving it to the database."""
before_rating = request.form.get("before-rating")
start_hour = request.form.get("planned-time")
start_date = request.form.get("planned-date")
unformatted_time = start_date + " " + start_hour
start_time = datetime.strptime(unformatted_time, "%Y-%m-%d %I:%M %p")
new_occurrence = Occurrence(activity_id=activity_id,
start_time=start_time,
before_rating=before_rating)
db.session.add(new_occurrence)
db.session.commit()
flash("Entries successfully saved.")
return redirect("/main")
@app.route("/record_after/<occurrence_id>", methods=["GET"])
def display_after_form(occurrence_id):
"""Display form for completing record of a previously created occurrence."""
# Get activity_type to display as a heading
occurrence = Occurrence.query.filter(
Occurrence.occurrence_id == occurrence_id
).one()
activity_name = occurrence.activity.activity_type
# Get current date and time so that user can quickly select these using now
# button if desired
pacific = pytz.timezone('US/Pacific')
now = datetime.now(tz=pacific)
now_date = datetime.strftime(now, "%Y-%m-%d")
now_time = datetime.strftime(now, "%I:%M %p")
return render_template("/record_after.html",
occurrence_id=occurrence_id,
now_date=now_date,
now_time=now_time,
activity_name=activity_name)
@app.route("/record_after/<occurrence_id>", methods=["POST"])
def get_after_values(occurrence_id):
"""Process form for completing record of a previously created occurrence."""
after_rating = request.form.get("after-rating")
end_hour = request.form.get("end-time")
end_date = request.form.get("end-date")
unformatted_time = end_date + " " + end_hour
end_time = datetime.strptime(unformatted_time, "%Y-%m-%d %I:%M %p")
completed_occurrence = Occurrence.query.filter(
Occurrence.occurrence_id == occurrence_id
).one()
completed_occurrence.end_time = end_time
completed_occurrence.after_rating = after_rating
db.session.commit()
flash("Changes saved.")
return redirect("/main")
@app.route("/chart/<activity_id>.json")
def make_lines_chart_json(activity_id):
"""Return json with the data needed to render charts for all activities with
completed occurrences."""
completed_occurrences = db.session.query(Occurrence).join(Activity).filter(
Activity.user_id == session['user_id'],
Occurrence.end_time.isnot(None),
Occurrence.after_rating.isnot(None),
Occurrence.start_time.isnot(None),
Occurrence.before_rating.isnot(None),
Activity.activity_id == activity_id).order_by(
Occurrence.start_time).all()
before_ratings = [occurrence.before_rating
for occurrence in completed_occurrences]
after_ratings = [occurrence.after_rating
for occurrence in completed_occurrences]
unformatted_start_times = [occurrence.start_time
for occurrence in completed_occurrences]
start_times = [datetime.strftime(unformatted, "%a, %b %d")
for unformatted in unformatted_start_times]
return jsonify({"before": before_ratings,
"after": after_ratings,
"starts": start_times,
"activityId": activity_id})
@app.route("/profile", methods=["GET"])
def display_update_page():
"""Display page where user can update registration data."""
return render_template("profile.html")
@app.route("/json/user", methods=["GET"])
def get_profile_data():
"""Return json for updating user profile information."""
user = User.query.get(session['user_id'])
results = {}
results['email'] = user.email
results['username'] = user.user_handle
results['phoneNumber'] = user.phone_number
results['age'] = user.age
return jsonify(results)
@app.route("/profile", methods=["POST"])
def process_profile_update():
"""Process form for changing user data."""
username = request.form.get("username")
email = request.form.get("email")
phone_number = request.form.get("phoneNumber")
age = request.form.get("age")
user = User.query.get(session['user_id'])
user.user_handle = username
user.email = email
user.phone_number = phone_number
user.age = age
db.session.commit()
# Update relevant session data immediately
session['user_handle'] = user.user_handle
flash("Changes saved.")
return redirect("/main")
@app.route("/signout", methods=["GET"])
def signout_user():
"""Sign user out and redirect to landing page."""
del session['user_id']
del session['user_handle']
return redirect("/")
# connect_to_db(app)
if __name__ == "__main__":
app.debug = False
app.jinja_env.auto_reload = app.debug
connect_to_db(app)
DebugToolbarExtension(app)
app.run(host="0.0.0.0")