-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
387 lines (318 loc) Β· 15.2 KB
/
auth.py
File metadata and controls
387 lines (318 loc) Β· 15.2 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
import os
from queue import Empty
import time
from flask import Blueprint, render_template, redirect, url_for, request, flash, current_app, abort, logging, globals
from werkzeug.security import generate_password_hash, check_password_hash
from werkzeug.utils import secure_filename
import sys
from models import User, UserForm, Medication, Prescription
from flask_login import login_user, logout_user, login_required, current_user
from __init__ import db, text_to_speech_1
from datetime import datetime
import calendar
from datetime import date
import json
from pytz import timezone
MAX_CONTENT_LENGTH = 1024 * 1024
UPLOAD_EXTENSIONS = ['.jpg', '.png', '.gif', '.PNG', '.JPG', '.JPEG']
UPLOAD_PATH = 'static/userimages'
# create a Blueprint object that we name 'auth'
auth = Blueprint('auth', __name__)
@auth.route('/login', methods=['GET', 'POST']) # define login page path
def login(): # define login page fucntion
if request.method == 'GET': # if the request is a GET we return the login page
return render_template('login.html')
else: # if the request is POST the we check if the user exist and with te right password
email = request.form.get('email')
password = request.form.get('password')
remember = True if request.form.get('remember') else False
user = User.query.filter_by(email=email).first()
print(user)
# check if the user actually exists
# take the user-supplied password, hash it, and compare it to the hashed password in the database
if not user:
flash('Please sign up before!')
return redirect(url_for('auth.signup'))
elif not check_password_hash(user.password, password):
flash('Please check your login details and try again.')
# if the user doesn't exist or password is wrong, reload the page
return redirect(url_for('auth.login'))
# if the above check passes, then we know the user has the right credentials
login_user(user, remember=remember)
to_say = ("hello" + current_user.first_name)
#text_to_speech_1(to_say)
return userDash()
@auth.route('/signup', methods=['GET', 'POST']) # we define the sign up path
def signup(): # define the sign up function
if request.method == 'GET': # If the request is GET we return the sign up page and forms
return render_template('signup.html')
else: # if the request is POST, then we check if the email doesn't already exist and then we save data
email = request.form.get('email')
first_name = request.form.get('first_name')
last_name = request.form.get('last_name')
password = request.form.get('password')
dob = request.form.get('dob')
# image file
uploaded_file = request.files['image']
filename = secure_filename(uploaded_file.filename)
if filename != '':
file_ext = os.path.splitext(filename)[1]
if file_ext not in UPLOAD_EXTENSIONS: # disallowed extensions to be fixed!
abort(400)
# saves image to folder
uploaded_file.save(os.path.join(UPLOAD_PATH, filename))
image = UPLOAD_PATH + '/' + filename # sets path for the user's profile image
print(first_name + last_name + dob)
print(password)
# if this returns a user, then the email already exists in database
user = User.query.filter_by(email=email).first()
if user: # if a user is found, we want to redirect back to signup page so user can try again
flash('Email address already exists')
return redirect(url_for('auth.signup'))
# create a new user with the form data. Hash the password so the plaintext version isn't saved.
# use the recently uploaded file as the user's profile picture/face recognition picture
new_user = User(email=email, first_name=first_name, last_name=last_name,
password=generate_password_hash(password, method='sha256'), dob=dob, image=image)
# add the new user to the database
flash('Account created! You can login now!! ')
db.session.add(new_user)
db.session.commit()
result = User.query.filter_by(first_name=first_name).first()
if not result:
print
'No result found'
else:
print(result)
return redirect(url_for('auth.login'))
# medform page
@auth.route('/submitmeds', methods=['GET', 'POST'])
@login_required
def submitmeds():
if request.method == 'GET': # if the request is a GET we return the login page
return render_template('medform.html')
else:
print('Test')
medication_name = request.form.get('medication_name')
medication_type = request.form.get('medication_type')
medication_dose = request.form.get('medication_dose')
medication_time = request.form.get('medication_time')
if(medication_name == ''):
notifError = "No Medication Name"
return render_template('medform.html', notifError=notifError)
elif(medication_type == ''):
notifError = "No Medication Type"
return render_template('medform.html', notifError=notifError)
elif(medication_dose == ''):
notifError = "No Medication Dose"
return render_template('medform.html', notifError=notifError)
elif(medication_time is None):
notifError = "No Medication Time"
return render_template('medform.html', notifError=notifError)
# new_user = User(medication_name =email, first_name=first_name, last_name=last_name,
# password=generate_password_hash(password, method='sha256'), dob=dob, image=image)
new_medication = Medication(medication_name=medication_name, medication_type=medication_type,
medication_dose=medication_dose, medication_time=medication_time)
db.session.add(new_medication)
db.session.commit()
# result = Medication.query.filter_by(medication_name=medication_name).first()
new_prescription = Prescription(
user_id=current_user.id, medication_id=new_medication.medication_id)
db.session.add(new_prescription)
db.session.commit()
if (request.form.get('medication_name2')):
medication_name2 = request.form.get('medication_name2')
medication_type2 = request.form.get('medication_type2')
medication_dose2 = request.form.get('medication_dose2')
medication_time2 = request.form.get('medication_time2')
if(medication_name2 == ''):
notifError = "No Medication Name 2"
return render_template('medform.html', notifError=notifError)
elif(medication_type2 == ''):
notifError = "No Medication Type 2"
return render_template('medform.html', notifError=notifError)
elif(medication_dose2 == ''):
notifError = "No Medication Dose 2"
return render_template('medform.html', notifError=notifError)
elif(medication_time2 is None):
notifError = "No Medication Time 2"
return render_template('medform.html', notifError=notifError)
new_medication2 = Medication(medication_name=medication_name2, medication_type=medication_type2,
medication_dose=medication_dose2, medication_time=medication_time2)
db.session.add(new_medication2)
db.session.commit()
new_prescription2 = Prescription(
user_id=current_user.id, medication_id=new_medication2.medication_id)
db.session.add(new_prescription2)
db.session.commit()
print("Medication Form 2 Existence Test Success")
print("Medication Form 2 Database Entry Success")
else:
print("Medication Form 2 Existence Test Failed")
if (request.form.get('medication_name3')):
medication_name3 = request.form.get('medication_name3')
medication_type3 = request.form.get('medication_type3')
medication_dose3 = request.form.get('medication_dose3')
medication_time3 = request.form.get('medication_time3')
if(medication_name3 == ''):
notifError = "No Medication Name 3"
return render_template('medform.html', notifError=notifError)
elif(medication_type3 == ''):
notifError = "No Medication Type 3"
return render_template('medform.html', notifError=notifError)
elif(medication_dose3 == ''):
notifError = "No Medication Dose 3"
return render_template('medform.html', notifError=notifError)
elif(medication_time3 is None):
notifError = "No Medication Time 3"
return render_template('medform.html', notifError=notifError)
new_medication3 = Medication(medication_name=medication_name3, medication_type=medication_type3,
medication_dose=medication_dose3, medication_time=medication_time3)
db.session.add(new_medication3)
db.session.commit()
new_prescription3 = Prescription(
user_id=current_user.id, medication_id=new_medication3.medication_id)
db.session.add(new_prescription3)
db.session.commit()
print("Medication Form 3 Existence Test Success")
print("Medication Form 3 Database Entry Success")
else:
print("Medication Form 2 Existence Test Failure")
print("Medication Form 2 Database Entry Failure")
#
# if not result:
# print
# 'No result found'
# else:
# print(result)
notif = "Success"
return render_template('medform.html', notif = notif)
# return redirect(url_for('main.profile'))
# settings page
@auth.route('/account', methods=['GET', 'POST']) # define settings path
@login_required
def account():
form = UserForm()
if request.method == "POST":
current_user.first_name = request.form.get('first_name')
current_user.last_name = request.form.get('last_name')
current_user.email = request.form.get('email')
current_user.dob = request.form.get('dob')
#current_user.image = request.form.get('image')
uploaded_file = request.files['image']
filename = secure_filename(uploaded_file.filename)
if filename != '':
file_ext = os.path.splitext(filename)[1]
if file_ext not in UPLOAD_EXTENSIONS: # disallowed extensions to be fixed!
abort(400)
# saves image to folder
uploaded_file.save(os.path.join(UPLOAD_PATH, filename))
image = UPLOAD_PATH + '/' + filename #
current_user.image = image
print("mehh" + current_user.first_name + current_user.last_name)
try:
db.session.commit()
return redirect(url_for('user.account'))
except:
flash("Error! Looks like there was a problem...try again!")
elif request.method == 'GET':
print(current_user.id)
form.first_name.data = current_user.first_name
form.last_name.data = current_user.last_name
form.email.data = current_user.email
form.dob.data = current_user.dob
db.session.commit()
print("hello " + current_user.first_name + current_user.last_name)
return render_template('Settings.html', form=form)
# userDash page
@auth.route('/userDash') # define userdash
@login_required
def userDash():
print("HERE")
querySchedule = db.session.query(Medication)
querySchedule = querySchedule.outerjoin(
Prescription, Medication.medication_id == Prescription.medication_id)
querySchedule = querySchedule.filter(
Prescription.user_id == current_user.id)
queryScheduleMorning = querySchedule.filter(
Medication.medication_time == "Morning")
queryScheduleNoon = querySchedule.filter(
Medication.medication_time == "Noon")
queryScheduleNight = querySchedule.filter(
Medication.medication_time == "Night")
results = queryScheduleMorning.all()
results2 = queryScheduleNoon.all()
results3 = queryScheduleNight.all()
queryList = db.session.query(Medication)
queryList = queryList.outerjoin(
Prescription, Medication.medication_id == Prescription.medication_id)
queryList = queryList.filter(Prescription.user_id == current_user.id)
resultList = queryList.all()
tz = timezone('EST')
now = datetime.now(tz).hour
morning = datetime.now(tz).hour
morning = 6
noon = datetime.now(tz).hour
noon = 12
night = datetime.now(tz).hour
night = 18
alert = "" # tandi, i added this because it wouldn't launch because alert wasn't declared
# since it sometimes never gets declared below
if now > morning and now < noon:
alert = "MORNING PILLS:"
for x in results:
alert += ' π ' + x.medication_name
elif now > noon and now < night:
alert= "NOON PILLS:"
for x in results2:
alert += ' π ' + x.medication_name
else:
alert = "NIGHT PILLS:"
for x in results3:
alert += ' π ' + x.medication_name
alert += ' π'
# if request.method == "POST":
# name = request.form.get('name1')
# queryList = queryList.filter(queryList.id == name).delete()
return render_template("userDash.html", queryScheduleMorning=results, queryScheduleNoon=results2, queryScheduleNight=results3, queryList=resultList, alert=alert)
@auth.route('/logout') # define logout path
@login_required
def logout(): # define the logout function
to_say = ("Good Bye" + current_user.first_name)
logout_user()
#text_to_speech_1(to_say)
return redirect(url_for('main.index'))
@auth.route('/webcam') # define webcam path
@login_required
def webcam():
return render_template('webcam.html')
@auth.route('/getmeds',methods=["GET", "POST"])
@login_required
def getmeds():
# if request.method == 'GET':
query = db.session.query(Medication)
query = query.outerjoin(
Prescription, Medication.medication_id == Prescription.medication_id)
query = query.filter(Prescription.user_id == current_user.id)
results = query.all()
return render_template("userDash.html", query=query)
#
# else:
# return render_template("userDash.html")
@auth.route('/deleteMed/<string:medId>', methods=["GET", "POST"])
@login_required
def deleteMed(medId):
# medId = request.form.get('medId')
# v = request.get_json().get('medId')
medId = json.loads(medId);
print("medId")
print(medId)
print("medId")
query = db.session.query(Medication)
query = query.outerjoin(
Prescription, Medication.medication_id == Prescription.medication_id)
query = query.filter(Prescription.user_id == current_user.id)
querySelectDel = query.filter(
Medication.medication_id == medId).one()
db.session.delete(querySelectDel)
db.session.commit()
return redirect(url_for('auth.userDash')) #Delete does not work