-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
738 lines (524 loc) · 28.9 KB
/
server.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
from statistical_functions import calculate_coefficient_of_determination
from jinja2 import StrictUndefined
from math import (sqrt, modf)
from scipy import stats
import numpy as np
from passlib.hash import pbkdf2_sha256
from flask import (Flask, render_template, redirect, request, flash,
session, copy_current_request_context, json)
from flask_debugtoolbar import DebugToolbarExtension
from datetime import date, timedelta
from model import User, Daily_Input, Custom_Variable_Info, Custom_Variable_Daily_Entry, connect_to_db, db
app = Flask(__name__)
app.secret_key = "ZYX"
regression_info = {}
next_day_regression_info = {}
@app.route('/')
def show_homepage():
"""show home page"""
if 'current_user' in session:
return redirect('/my_stats')
return render_template("homepage.html")
@app.route("/register", methods=["POST"])
def register_process():
"""Registration Form."""
email_input = request.form['register_email']
pw_input = request.form['register_pw']
name = request.form['name']
sql = """SELECT *
FROM users
WHERE "ID" = :email
"""
cursor = db.session.execute(sql, {'email': email_input})
result = cursor.fetchone()
print result
if result:
flash('The email {} is already attached to an account.'.format(email_input))
return redirect('/')
else:
saved_password = pbkdf2_sha256.hash(pw_input)
sql = """INSERT INTO users ("ID", password, name)
VALUES (:ID, :password, :name)"""
db.session.execute(sql, {'ID': email_input,
'password': pw_input,
'name': name,
})
db.session.commit()
session['current_user'] = email_input
return redirect('/registration_confirmation')
@app.route("/registration_confirmation")
def confirm_registration():
if 'current_user' not in session:
return redirect('/')
user_email = session['current_user']
print user_email
user = User.query.filter(User.ID == user_email).one()
print user
name = user.name
password = user.password
first_entry = user.first_entry_at
if not first_entry:
first_entry = Daily_Input.query.filter(Daily_Input.user_id == user_email).order_by('date').first()
first_entry = first_entry.strftime('%B %d, %Y')
return render_template("registration_confirmation.html", name=name, first_entry=first_entry)
@app.route("/login", methods=["POST"])
def login():
email_input = request.form['email_input']
pw_input = request.form['pw_input']
if User.query.filter(User.ID == email_input).all() == []:
flash('That email is not in our database. Please check your spelling, or use the form below to register', 'error')
return redirect("/")
elif User.query.filter(User.ID == email_input, User.password == pw_input).all() != []:
user = User.query.filter(User.ID == email_input).one()
session['current_user'] = user.ID
return redirect("/my_stats")
else:
user = User.query.filter(User.ID==email_input).one()
if pbkdf2_sha256.verify(pw_input, user.password):
session['current_user'] = user.ID
return redirect("/my_stats")
else:
flash('Invalid password. Please try again.', 'error')
return redirect("/")
@app.route("/my_stats")
def show_user_stats():
if 'current_user' not in session:
flash('Please log in first')
return redirect('/')
current_user = session['current_user']
user_stats = Daily_Input.query.filter_by(user_id=current_user).order_by('date').all()
users_variables = Custom_Variable_Info.query.filter(Custom_Variable_Info.user_id==current_user).all()
print users_variables
if users_variables:
records_dict = make_custom_v_and_wellness_dict(users_variables)
print records_dict
custom_variables_with_next_day_wellness = get_custom_v_next_day_effects(users_variables, current_user)
else:
custom_variables_with_next_day_wellness = None
if len(user_stats) < 1:
flash('You have not entered any data yet. Please enter data in order to view behavioral graphs.')
return redirect("/record_daily_input")
user = User.query.filter(User.ID == current_user).one()
name = user.name
sleep = []
screentime = []
exercise = []
well_being_rating = []
for info in user_stats:
sleep += [info.sleep]
screentime += [info.screen_time]
exercise += [info.exercise]
well_being_rating += [info.well_being_rating]
sleep_r = []
screentime_r = []
exercise_r = []
regression_lines = [sleep_r, screentime_r, exercise_r]
independent_variables = [sleep, screentime, exercise]
sleep_points, screen_points, exercise_points = find_next_day_effects(independent_variables, well_being_rating)
next_day_plot_points = [sleep_points, screen_points, exercise_points]
next_day_regression_info = create_regression_dict(next_day_plot_points, sleep_points, screen_points, exercise_points)
# goes through each item in each independent variable list and pairs it with its corresponding
# wellness score. The item and the wellness score become a sublist, which is appended to a
# list in regression lines. Because this loop simulatenously goes through regression_lines
# and independent_variables, sleep_r uses data from sleep, screentime_r uses data from screentime, etc.
for j in range(len(independent_variables)) and range(len(regression_lines)):
for i in range(len(independent_variables[j])) and range(len(well_being_rating)):
regression_lines[j].append([independent_variables[j][i], well_being_rating[i]])
if records_dict:
same_day_custom_v_r_squared = get_custom_r_squared(records_dict)
next_day_custom_v_r_squared = get_custom_r_squared(custom_variables_with_next_day_wellness)
if same_day_custom_v_r_squared:
same_day_custom_variable_insight, same_day_cv, sd_slope = write_insight_for_custom_variable(same_day_custom_v_r_squared, "same day")
if next_day_custom_v_r_squared:
next_day_custom_variable_insight, next_day_cv, nd_slp = write_insight_for_custom_variable(next_day_custom_v_r_squared, "next day", same_day_cv, sd_slope)
else:
same_day_custom_variable_insight = None
next_day_custom_variable_insight = None
for lst in independent_variables:
slope, intercept, r_value, p_value, std_err = stats.linregress(lst, well_being_rating)
sub_dict = {'slope': slope, 'intercept': intercept, 'r_value': r_value, 'p_value': p_value, 'std_err': std_err}
if lst == sleep:
regression_info["sleep"] = sub_dict
elif lst == screentime:
regression_info["screentime"] = sub_dict
elif lst == exercise:
regression_info["exercise"] = sub_dict
for key in list(regression_info.keys()):
for lst in independent_variables:
if key == "sleep" and lst == sleep:
add_regression_info_to_dict(regression_info, key, lst, len(lst))
if key == "screentime" and lst == screentime:
add_regression_info_to_dict(regression_info, key, lst, len(lst))
if key == "exercise" and lst == exercise:
add_regression_info_to_dict(regression_info, key, lst, len(lst))
for key in list(next_day_regression_info.keys()):
for lst in next_day_plot_points:
if key == "sleep" and lst == sleep_points:
add_regression_info_to_dict(next_day_regression_info, key, lst, len(lst))
if key == "screen" and lst == screen_points:
add_regression_info_to_dict(next_day_regression_info, key, lst, len(lst))
if key == "exercise" and lst == exercise_points:
add_regression_info_to_dict(next_day_regression_info, key, lst, len(lst))
ordered_ars = determine_relevence_of_behavior(regression_info)
most_relevent_activity = "behavior"
for behavior in regression_info:
if regression_info[behavior]["adjusted_r_squared"] == ordered_ars[-1]:
most_relevent_activity = behavior
biggest_next_day_impact = "behavior"
next_day_sorted_ars = determine_relevence_of_behavior(next_day_regression_info)
for behavior in next_day_regression_info:
if next_day_regression_info[behavior]["adjusted_r_squared"] == next_day_sorted_ars[-1]:
biggest_next_day_impact = behavior
next_day_insight = "stuff"
if next_day_regression_info[biggest_next_day_impact]["slope"] < 0:
next_day_insight = "the less {} you get, the better you tend to feel the next day.".format(biggest_next_day_impact)
elif next_day_regression_info[biggest_next_day_impact]["slope"] > 0:
next_day_insight = "the more {} you get, the better you tend to feel the next day.".format(biggest_next_day_impact)
custom_variables = get_users_custom_v_info(current_user)
custom_variables_list = []
for i in custom_variables.keys():
custom_variables_list.append(i)
same_day_insight = "stuff"
if regression_info[most_relevent_activity]["slope"] < 0:
same_day_insight = "the less {} you get, the better you tend to feel that day.".format(most_relevent_activity)
elif regression_info[most_relevent_activity]["slope"] > 0:
same_day_insight = "the more {} you get, the better you tend to feel that day.".format(most_relevent_activity)
same_day_message = "Out of all the activities you are tracking, {} is the most relevent to your sense of well-being that day. {}".format(most_relevent_activity, same_day_insight)
return render_template("my_stats.html", sleep=sleep_r, same_day_custom_variable_insight=same_day_custom_variable_insight, next_day_custom_variable_insight=next_day_custom_variable_insight, custom_variables_with_next_day_wellness=custom_variables_with_next_day_wellness, screentime=screentime_r, exercise=exercise_r, name=name, independent_variables=independent_variables, regression_info=regression_info, ordered_ars=ordered_ars, sleep_points=sleep_points, screen_points=screen_points, exercise_points=exercise_points, biggest_next_day_impact=biggest_next_day_impact, next_day_insight=next_day_insight, same_day_message=same_day_message, custom_variables_list=custom_variables_list)
def get_custom_r_squared(dictionary):
r_dictionary = {}
for v in dictionary.keys():
if len(dictionary[v]) >= 2:
behavior = []
wellness = []
for lst in dictionary[v]:
behavior.append(lst[0])
wellness.append(lst[1])
slope, intercept, r_value, p_value, std_err = stats.linregress(behavior, wellness)
r_squared = r_value**2
r_dictionary[v] = {}
n = len(behavior)
if n > 2:
adjusted_r_squared = 1 - (((1 - r_squared) * (n-1))/(n-1-1))
r_dictionary[v]['adjusted_r_squared'] = adjusted_r_squared
r_dictionary[v]['r_squared'] = r_squared
r_dictionary[v]['slope'] = slope
if not r_dictionary.values():
return None
return r_dictionary
def write_insight_for_custom_variable(dictionary, string, same_day=None, slp=None):
"""look up adjusted r squared and slope to determine most relevent custom variable"""
highest_influence = None
number = None
for key in dictionary:
if dictionary[key].get('adjusted_r_squared', 0) > number:
highest_influence = key
if dictionary[highest_influence]['slope'] > 0:
slope = "more"
sign = 1
else:
slope = "less"
sign = -1
if string == "same day":
message = "Out of all the custom behaviors you created, getting {} {} has the most impact on your {} sense of well being.".format(slope, highest_influence, string)
elif highest_influence == same_day and slp != sign:
message = "However, the {} {} you get, the better you feel the next day.".format(slope, highest_influence)
elif highest_influence ==same_day and slp == sign:
message = "Additionally, getting {} {} tends to correlate with you feeling better the next day.".format(slope, highest_influence)
else:
message = "The {} {} you get, the better you feel the next day.".format(slope, highest_influence)
return message, highest_influence, sign
def make_custom_v_and_wellness_dict(users_variables):
"""Pairs custom variable amount with corresponding wellness score and returns dictionary"""
records_dict = {}
for item in users_variables:
behavior_and_well_being_lst = []
records = Custom_Variable_Daily_Entry.query.filter(Custom_Variable_Daily_Entry.variable_info==item.variable_id).all()
if len(records) < 2:
print "\n None \n"
break
for entry in records:
day = []
day.append(entry.custom_variable_amount)
default_entry = Daily_Input.query.filter(Daily_Input.input_id==entry.daily_default_v_input_id).one()
day.append(default_entry.well_being_rating)
behavior_and_well_being_lst.append(day)
records_dict[str(item.variable_name)] = behavior_and_well_being_lst
return records_dict
def get_custom_v_next_day_effects(users_variables, current_user):
"""Pairs custom variable amount with next day wellness score and returns dictionary"""
records_dict = {}
for item in users_variables:
behavior_and_well_being_lst = []
records = Custom_Variable_Daily_Entry.query.filter(Custom_Variable_Daily_Entry.variable_info==item.variable_id).all()
for entry in records:
day = []
day.append(entry.custom_variable_amount)
default_entry = Daily_Input.query.filter(Daily_Input.input_id==entry.daily_default_v_input_id).one()
default_entry.date
next_day = default_entry.date + timedelta(days=1)
next_day_wellness_score = Daily_Input.query.filter(Daily_Input.date==next_day, Daily_Input.user_id==current_user).all()
if not next_day_wellness_score:
pass
for i in next_day_wellness_score:
day.append(i.well_being_rating)
if len(day) == 2:
behavior_and_well_being_lst.append(day)
records_dict[str(item.variable_name)] = behavior_and_well_being_lst
return records_dict
# key refers to keys in regression_info dictionary. These keys share the same name as the lists in the
# independent variable list, because the dictionary info is based on these lists
def add_regression_info_to_dict(dictionary, key, lst, n):
"""Calculate adjusted r squared for each data set of dependent + independent variables, and add it to the data set dict."""
# n is the sample size
# adjusted_r_squared formula used can be found here: http://mtweb.mtsu.edu/stats/dictionary/formula.htm
dictionary[key]['n'] = len(lst)
r_value = dictionary[key]['r_value']
# I hardcoded k to 1 because for the first chart, each scatter plot and regression line is for 1
# independent variable (k is supposed to be number of independent variables).
adjusted_r_squared = 1 - (((1 - r_value**2) * (n-1))/(n-1-1))
dictionary[key]['adjusted_r_squared'] = adjusted_r_squared
def determine_relevence_of_behavior(dictionary):
"""Pass in the dict with all the regression info and see which behaviors are most relevent to the user.
Return print statements that provide correlative insights."""
highest_influences = []
for behavior in dictionary:
r = dictionary[behavior]["adjusted_r_squared"]
highest_influences.append(r)
return sorted(highest_influences)
def find_next_day_effects(indep_v_list, wellness_scores):
sleep_points = []
screen_points = []
exercise_points = []
plot_points_for_next_day_effects = [sleep_points, screen_points, exercise_points]
for j in range(len(indep_v_list)) and range(len(plot_points_for_next_day_effects)):
for i in range(len(indep_v_list[j])) and range(len(wellness_scores)-1):
plot_points_for_next_day_effects[j].append([indep_v_list[j][i], wellness_scores[i+1]])
return plot_points_for_next_day_effects[0], plot_points_for_next_day_effects[1], plot_points_for_next_day_effects[2]
def create_regression_dict(lst, sublist_1, sublist_2, sublist_3):
for item in lst:
slope, intercept, r_value, p_value, std_err = stats.linregress(item)
sub_dict = {'slope': slope, 'intercept': intercept, 'r_value': r_value, 'p_value': p_value, 'std_err': std_err}
if item == sublist_1:
next_day_regression_info["sleep"] = sub_dict
elif lst == sublist_2:
next_day_regression_info["screen"] = sub_dict
elif lst == sublist_3:
next_day_regression_info["exercise"] = sub_dict
return next_day_regression_info
@app.route("/record_daily_input")
def record_input():
if 'current_user' not in session:
return redirect('/')
current_user = session['current_user']
user = User.query.filter(User.ID == current_user).one()
name = user.name
custom_variables = get_users_custom_v_info(current_user)
size = len(custom_variables)
return render_template("record_daily_input.html", name=name, custom_variables=custom_variables, size=size)
def get_users_custom_v_info(current_user):
user = User.query.filter(User.ID == current_user).one()
name = user.name
user_custom_variables = Custom_Variable_Info.query.filter(Custom_Variable_Info.user_id==current_user).all()
custom_variables = {}
for i in range(len(user_custom_variables)):
custom_variables[str(user_custom_variables[i].variable_name).rstrip()] = {}
custom_variables[str(user_custom_variables[i].variable_name).rstrip()]["name"] = str(user_custom_variables[i].variable_name).rstrip()
custom_variables[str(user_custom_variables[i].variable_name).rstrip()]["unit"] = str(user_custom_variables[i].variable_units).rstrip()
return custom_variables
@app.route("/add_info", methods=["POST"])
def add_info():
current_user = session['current_user']
yesterday = date.today() - timedelta(1)
if Daily_Input.query.filter(Daily_Input.date==yesterday, Daily_Input.user_id == current_user).all() != []:
flash('Sorry, you have already entered information for this day. If you believe this information was entered incorrectly, you can change it below.')
return redirect('/see_all_records')
sleep_h = request.form.get('sleep_h')
sleep_m = request.form.get('sleep_m')
exercise_h = request.form.get('exercise_h')
exercise_m = request.form.get('exercise_m')
screentime_h = request.form.get('screentime_h')
screentime_m = request.form.get('screentime_m')
wellness_score = request.form.get('wellness_score')
sleep_t = round((float(sleep_m)/60.0) + float(sleep_h), 2)
exercise_t = round((float(exercise_m)/60.0) + float(exercise_h), 2)
screentime_t = round((float(screentime_m)/60.0) + float(screentime_h), 2)
wellness_score = int(wellness_score)
new_day_log = Daily_Input(date=yesterday, user_id=current_user, sleep=sleep_t, exercise=exercise_t, screen_time=screentime_t, well_being_rating=wellness_score)
db.session.add(new_day_log)
db.session.commit()
#gets the entry we just made
default_v_entry = Daily_Input.query.filter(Daily_Input.date==yesterday, Daily_Input.user_id == current_user).one()
#gets the input id so we can use it as the foreign key for the entry below
default_fk = default_v_entry.input_id
custom_variables = get_users_custom_v_info(current_user)
for i in custom_variables.keys():
name = custom_variables[i]["name"]
variable_info = Custom_Variable_Info.query.filter(Custom_Variable_Info.user_id==current_user, Custom_Variable_Info.variable_name==name).one()
v_info = variable_info.variable_id
amount = request.form.get(name)
new_custom_entry = Custom_Variable_Daily_Entry(variable_info=v_info, daily_default_v_input_id=default_fk, custom_variable_amount=amount)
db.session.add(new_custom_entry)
db.session.commit()
return redirect('/my_stats')
@app.route("/change_row_in_daily_inputs", methods=["POST"])
def change_records():
current_user = session['current_user']
date = request.form.get('date')
sleep_h = request.form.get('sleep_hours')
sleep_m = request.form.get('sleep_minutes')
exercise_h = request.form.get('exercise_hours')
exercise_m = request.form.get('exercise_minutes')
screentime_h = request.form.get('screentime_hours')
screentime_m = request.form.get('screentime_minutes')
wellness_score = request.form.get('wellness_score')
entry_number = request.form.get('change_entry_number')
entry_number = int(entry_number) - 1
all_info = Daily_Input.query.filter(Daily_Input.user_id==current_user).order_by(Daily_Input.date.desc()).all()
entry_to_change = all_info[entry_number]
custom_v_entry = Custom_Variable_Daily_Entry.query.filter(Custom_Variable_Daily_Entry.daily_default_v_input_id==entry_to_change.input_id).all()
for i in custom_v_entry:
print i
db.session.delete(i)
db.session.commit()
db.session.delete(entry_to_change)
db.session.commit()
sleep_t = round((float(sleep_m)/60.0) + float(sleep_h), 2)
exercise_t = round((float(exercise_m)/60.0) + float(exercise_h), 2)
screentime_t = round((float(screentime_m)/60.0) + float(screentime_h), 2)
new_day_log = Daily_Input(date=date, user_id=current_user, sleep=sleep_t, exercise=exercise_t, screen_time=screentime_t, well_being_rating=wellness_score)
db.session.add(new_day_log)
db.session.commit()
get_default_entry_id = Daily_Input.query.filter(Daily_Input.user_id==current_user, Daily_Input.date==date).one()
custom_variables = get_users_custom_v_info(current_user)
for variable in custom_variables.keys():
custom_v = request.form.get(variable)
usersvars = Custom_Variable_Info.query.filter(Custom_Variable_Info.user_id==current_user).all()
custom_v_info = Custom_Variable_Info.query.filter(Custom_Variable_Info.variable_name==variable, Custom_Variable_Info.user_id==current_user).one()
custom_v_id = custom_v_info.variable_id
entry_id = get_default_entry_id.input_id
new_custom_entry = Custom_Variable_Daily_Entry(variable_info=custom_v_id, daily_default_v_input_id=entry_id, custom_variable_amount=custom_v)
db.session.add(new_custom_entry)
db.session.commit()
flash('entry successfully changed!')
return redirect('/see_all_records')
@app.route("/see_all_records")
def see_all_records():
if 'current_user' not in session:
return redirect('/')
current_user = session['current_user']
user = User.query.filter(User.ID == current_user).one()
name = user.name
all_info = Daily_Input.query.filter(Daily_Input.user_id==current_user).order_by(Daily_Input.date.desc()).all()
all_entries = []
for entry in all_info:
input_dictionary = {}
input_dictionary['date'] = "{}-{}-{}".format(entry.date.month, entry.date.day, entry.date.year)
input_dictionary['sleep'] = "{} hrs {} mins".format(hours_and_minutes(entry.sleep)[0], hours_and_minutes(entry.sleep)[1])
input_dictionary['exercise'] = "{} hrs {} mins".format(hours_and_minutes(entry.exercise)[0], hours_and_minutes(entry.exercise)[1])
input_dictionary['screentime'] = "{} hrs {} mins".format(hours_and_minutes(entry.screen_time)[0], hours_and_minutes(entry.screen_time)[1])
input_dictionary['well_being_rating'] = entry.well_being_rating
all_entries.append(input_dictionary)
custom_variables = get_users_custom_v_info(current_user)
matching_entries = create_matching_entries_dict(all_info, custom_variables)
last_30_days = []
for i in range(31):
last_30_days.append(date.today() - timedelta(i))
length = len(all_entries)
question = len(matching_entries)
how_many_customs = len(custom_variables)
return render_template("all_entries.html", all_entries=all_entries, current_user=current_user, length=length, last_30_days=last_30_days, name=name, custom_variables=custom_variables, matching_entries=matching_entries, how_many_customs=how_many_customs)
def create_matching_entries_dict(all_info, custom_variables):
matching_entries = {}
for entry in all_info:
# gets each date of all entries into string format
default_entry = "{}-{}-{}".format(entry.date.month, entry.date.day, entry.date.year)
#sets each variable and its info as a value to a key that is a date
matching_entries[default_entry] = {}
for item in custom_variables.keys():
matching_entries[default_entry][item] = 0
input_ids_to_check = entry.input_id
date_to_check = default_entry
matching_entries = find_custom_variable_amounts(input_ids_to_check, date_to_check, matching_entries)
return matching_entries
def find_custom_variable_amounts(input_ids_to_check, date_to_check, matching_entries):
# gets all the variable entries with a given input id. does this for each entry
cv_query = Custom_Variable_Daily_Entry.query.filter(Custom_Variable_Daily_Entry.daily_default_v_input_id==input_ids_to_check).all()
for item in cv_query:
# get the information associated with that variable
custom_v = Custom_Variable_Info.query.filter(Custom_Variable_Info.variable_id==item.variable_info).one()
#its name
name = custom_v.variable_name
for entry_day in matching_entries:
# if date of input is the same as matching_entries key
if entry_day == date_to_check:
# how much was recorded for this entry
amount = item.custom_variable_amount
matching_entries[date_to_check][name] = matching_entries[date_to_check].get('name', 0) + amount
else:
pass
return matching_entries
def hours_and_minutes(x):
minutes, hours = modf(x)
minutes *= 60
return int(hours), int(minutes)
@app.route("/create_custom_variable")
def create_custom_variable():
current_user = session['current_user']
user = User.query.filter(User.ID == current_user).one()
name = user.name
return render_template("create_custom_variable.html", name=name)
@app.route("/add_new_variable", methods=["POST"])
def add_new_variable():
variable_name = request.form.get('variable_name')
variable_name = str(variable_name)
variable_name = variable_name.rstrip()
variable_name = variable_name.replace('/', '')
variable_name = variable_name.replace(';', '')
unit_type = request.form.get('unit_type')
if unit_type:
unit_type = str(unit_type)
unit_type = unit_type.replace('/', '')
unit_type = unit_type.replace(';', '')
else:
unit_type = "B"
current_user = session['current_user']
new_custom_variable = Custom_Variable_Info(user_id=current_user, variable_name=variable_name, variable_units=unit_type)
db.session.add(new_custom_variable)
db.session.commit()
return redirect('/see_all_records')
@app.route("/delete_custom_variable")
def delete_custom_variable():
current_user = session['current_user']
user_create_variables = Custom_Variable_Info.query.filter(Custom_Variable_Info.user_id == current_user).all()
users_variables = []
for behavior in user_create_variables:
users_variables.append(behavior.variable_name)
return render_template('delete_custom_variable.html', users_variables=users_variables)
@app.route("/delete_variable", methods=["POST"])
def delete_variable():
current_user = session['current_user']
variable_name = request.form.get('variable_name')
email = session['current_user']
variable_to_delete = Custom_Variable_Info.query.filter(Custom_Variable_Info.user_id == current_user, Custom_Variable_Info.variable_name == variable_name).one()
entries = Custom_Variable_Daily_Entry.query.filter(Custom_Variable_Daily_Entry.variable_info == variable_to_delete.variable_id).all()
for entry in entries:
db.session.delete(entry)
db.session.commit()
db.session.delete(variable_to_delete)
db.session.commit()
return redirect('/see_all_records')
@app.route("/logout")
def logout():
del session['current_user']
return redirect ("/")
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
app.debug = True
# make sure templates, etc. are not cached in debug mode
app.jinja_env.auto_reload = app.debug
connect_to_db(app)
# Use the DebugToolbar
DebugToolbarExtension(app)
app.run(port=5000, host='0.0.0.0')