-
Notifications
You must be signed in to change notification settings - Fork 0
/
userinfo_routes.py
580 lines (485 loc) · 20 KB
/
userinfo_routes.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
"""
Module contains routes related to personal info acquired from user, such as routine schedules, medical info, family makeup
"""
import os
import re
from dotenv import load_dotenv
from flask import Blueprint, flash, g, jsonify, redirect, render_template, request, session, url_for
from flask import current_app as app
from sqlalchemy.exc import SQLAlchemyError
from dbmodels import Calendar, Events, Families, Medical, Providers, States
from helpers import apology, login_required
from preppydb import db_session
from utils import load_states
userinfo_routes = Blueprint('userinfo_routes', __name__)
# Import Google API key
load_dotenv('global.env')
api_key = os.getenv('GOOGLE_API_KEY')
@userinfo_routes.route("/family", methods=["GET"])
@login_required
@load_states
def family():
"""
Load user's family information from database
"""
fam = {}
# Check if the user has a family in db
try:
userfam = db_session.query(Families).filter_by(user_id=session['user_id']).first()
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
return apology("Database error accessing household information.")
# If so, retrieve their family info
if userfam:
fam['name'] = userfam.last_name
fam['adults'] = userfam.adults
fam['seniors'] = userfam.seniors
fam['children'] = userfam.children
fam['pets'] = userfam.pets
fam['special'] = userfam.special_needs
try:
fam['state'] = db_session.query(States.state).filter_by(id=userfam.state_id).scalar()
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
return apology("Database error accessing state info.")
# if not, fill family dict with blank info
else:
fam['name'] = 'Unknown'
fam['adults'] = fam['seniors'] = fam['children'] = fam['pets'] = 0
fam['state'] = 'Unknown'
fam['special'] = 'Unknown'
return render_template("family.html", states=g.states, family=fam, nonce=g.nonce)
@userinfo_routes.route("/editfamily", methods=["POST"])
@login_required
@load_states
def editfamily():
"""
Allow user to enter and/or edit family info
"""
# Validate form data
errors, validated_data = validate_form(request.form)
if errors:
flash(" ".join(errors))
return redirect(url_for('userinfo_routes.family'))
# If the user already has family data in the db, update it with the new values
try:
row = db_session.query(Families.user_id).filter_by(user_id=session['user_id']).first()
state_id = db_session.query(States.id).filter_by(state=validated_data["state"]).first().id
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
flash("Error updating household info.")
return redirect(url_for('userinfo_routes.family'))
if row:
try:
db_session.query(Families).filter_by(user_id=session['user_id']).update({
'last_name': validated_data["name"],
'adults': validated_data["adults"],
'seniors': validated_data["seniors"],
'children': validated_data["children"],
'pets': validated_data["pets"],
'state_id': state_id,
'special_needs': validated_data["special"],
})
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
errors.append("Database error updating household.")
# if the user has no family in the db yet, insert information
else:
try:
new_family = Families(user_id=session['user_id'], last_name=validated_data["name"], adults=validated_data["adults"], seniors=validated_data["seniors"],
children=validated_data["children"], pets=validated_data["pets"], state_id=state_id, special_needs=validated_data["special"])
db_session.add(new_family)
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
errors.append("Database error adding household information.")
if errors:
flash(" ".join(errors))
# Set session last name
session['last_name'] = validated_data["name"]
return redirect(url_for('userinfo_routes.family'))
@userinfo_routes.route("/routines", methods=['GET'])
@login_required
def routines():
"""
Send user's saved event data to routines page
"""
if 'last_name' not in session:
flash("Please provide your family info first. This will help us to help you prepare.")
return redirect(url_for('userinfo_routes.family'))
last_name = session['last_name']
try:
results = db_session.query(Calendar.id, Calendar.name).filter_by(
user_id=session['user_id']).all()
events = db_session.query(Events).filter(Events.person_id.in_(
db_session.query(Calendar.id).filter_by(user_id=session['user_id']))).all()
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
return apology("Error retrieving schedule.")
formatted_events = []
for event in events:
start = f"{event.start_day}T{event.start_time}"
end = f"{event.end_day}T{event.end_time}"
formatted_events.append({
'title': event.title,
'start': start,
'end': end,
'description': event.description,
'person_id': event.person_id,
'address': event.address,
'eventId': event.id
})
return render_template('routines.html', last_name=last_name, results=results, events=formatted_events, nonce=g.nonce)
@userinfo_routes.route("/getfamily")
@login_required
def getfamily():
"""
Sends family members saved in calendar table to fullCalendar
"""
try:
rows = db_session.query(Calendar.id, Calendar.name).filter_by(
user_id=session['user_id']).all()
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
return apology("Error retrieving household members.")
return jsonify([{"id": row.id, "title": row.name} for row in rows])
@userinfo_routes.route("/add_family", methods=['POST'])
@login_required
def add_family():
"""
Add family member to calendar options
"""
if not request.form.get('name'):
flash("Please enter a name.")
return redirect(url_for('userinfo_routes.routines'))
name = request.form.get('name')
try:
new_person = Calendar(user_id=session['user_id'], name=name)
db_session.add(new_person)
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error adding household member.")
return redirect(url_for('userinfo_routes.routines'))
@userinfo_routes.route("/delete_family", methods=['POST'])
@login_required
def delete_family():
"""
Handle user deletion of family member from calendar
"""
if not request.form.get('name'):
flash("Please enter a name.")
return redirect(url_for('userinfo_routes.routines'))
name = request.form.get('name')
try:
db_session.query(Calendar).filter_by(user_id=session['user_id'], name=name).delete()
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error deleting household member.")
return redirect(url_for('userinfo_routes.routines'))
@userinfo_routes.route("/delete_event", methods=['POST'])
@login_required
def delete_event():
"""
Handle user deletion of event from calendar
"""
if not request.form.get('eventId'):
flash("Invalid eventId")
return redirect(url_for('userinfo_routes.routines'))
event_id = request.form.get('eventId')
try:
db_session.query(Events).filter_by(id=event_id).delete()
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error deleting event.")
return redirect(url_for('userinfo_routes.routines'))
@userinfo_routes.route("/add_event", methods=['POST'])
@login_required
def add_event():
"""
Handle new additions and updates to events
"""
errors = []
required_fields = ['person_id', 'startTime', 'startDay', 'endTime', 'endDay', 'address']
person_id = None
if not request.form:
flash("No form data.")
return redirect(url_for('userinfo_routes.routines'))
form_data = request.form
for field in required_fields:
if not form_data.get(field):
errors.append(f"{field.replace('_', ' ').capitalize()} is required.")
try:
person_id = int(form_data.get('person_id'))
except (ValueError, TypeError) as e:
app.logger.error("ValueError: %s", e)
errors.append("Invalid person ID.")
title = form_data.get('title', '')
start_time = form_data.get('startTime')
start_day = form_data.get('startDay')
end_time = form_data.get('endTime')
end_day = form_data.get('endDay')
address = form_data.get('address')
if errors:
flash(" ".join(errors))
return redirect(url_for('userinfo_routes.routines'))
description = form_data.get("description", "")
if not form_data.get('eventId'):
try:
new_event = Events(person_id=person_id, title=title, start_time=start_time, start_day=start_day,
end_time=end_time, end_day=end_day, address=address, description=description)
db_session.add(new_event)
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
errors.append("Error adding event.")
else:
event_id = form_data.get('eventId')
try:
db_session.query(Events).filter_by(id=event_id).update({
'person_id': person_id,
'title': title,
'start_time': start_time,
'start_day': start_day,
'end_time': end_time,
'end_day': end_day,
'address': address,
'description': description
})
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
errors.append("Error updating event.")
if errors:
flash(" ".join(errors))
return redirect(url_for('userinfo_routes.routines'))
@userinfo_routes.route('/medical', methods=['GET'])
@login_required
def medical():
"""
Retrieve user's saved medical info and send to front end template
"""
if 'last_name' not in session:
flash("Please provide your family info first. This will help us to help you prepare.")
return redirect(url_for('userinfo_routes.family'))
last_name = session['last_name']
try:
results = db_session.query(Medical).filter_by(user_id=session['user_id'])
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
return apology("Error retrieving medical information.")
return render_template('medical.html', results=results, last_name=last_name, nonce=g.nonce)
@userinfo_routes.route('/add_medical', methods=['POST'])
@login_required
def add_medical():
"""
Add new medical info submitted by user
"""
fields = ['first_name', 'last_name', 'blood-type',
'medications', 'allergies', 'other', 'insurance', 'policy']
data = {field: request.form.get(field) or '' for field in fields}
# Check for required fields
required_fields = ['first_name', 'blood-type']
for field in required_fields:
if not data[field]:
flash(f"Please provide a valid {field.replace('_', ' ')}.")
return redirect(url_for('userinfo_routes.medical'))
try:
new_medical = Medical(user_id=session['user_id'], first_name=data['first_name'], last_name=data['last_name'], blood_type=data['blood-type'],
medications=data['medications'], allergies=data['allergies'], other=data['other'], insurance=data['insurance'], policy=data['policy'])
db_session.add(new_medical)
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error adding medical information.")
return redirect(url_for('userinfo_routes.medical'))
@userinfo_routes.route('/delete_medical', methods=['POST'])
@login_required
def delete_medical():
"""
Delete medical info as requested by user
"""
if not request.form.get('person_id'):
return apology("Invalid person ID.")
person_id = request.form.get('person_id')
try:
db_session.query(Medical).filter_by(id=person_id).delete()
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error deleting medical information.")
return redirect(url_for('userinfo_routes.medical'))
@userinfo_routes.route('/edit_medical', methods=['POST'])
@login_required
def edit_medical():
"""
Edit medical info as requested by user
"""
fields = ['person_id', 'first_name', 'last_name', 'blood-type',
'medications', 'allergies', 'other', 'insurance', 'policy']
data = {field: request.form.get(field) or '' for field in fields}
required_fields = ['first_name', 'blood-type']
for field in required_fields:
if not data[field]:
flash(f"Please provide a valid {field.replace('_', ' ')}")
return redirect(url_for('userinfo_routes.medical'))
try:
db_session.query(Medical).filter_by(id=data['person_id']).update({
'first_name': data['first_name'],
'last_name': data['last_name'],
'blood_type': data['blood-type'],
'medications': data['medications'],
'allergies': data['allergies'],
'other': data['other'],
'insurance': data['insurance'],
'policy': data['policy']
})
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error updating medical information.")
return redirect(url_for('userinfo_routes.medical'))
@userinfo_routes.route('/providers', methods=['GET'])
@login_required
def providers():
"""
Retrieve user's medical providers saved in database and send to template
"""
if 'last_name' not in session:
flash("Please provide your family info first. This will help us to help you prepare.")
return redirect(url_for('userinfo_routes.family'))
last_name = session['last_name']
try:
results = db_session.query(Providers).filter_by(user_id=session['user_id'])
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
return apology("Error retrieving medical providers.")
url = f'https://maps.googleapis.com/maps/api/js?key={api_key}&libraries=places'
return render_template('med_providers.html', results=results, url=url, api_key=api_key, last_name=last_name, nonce=g.nonce)
@userinfo_routes.route('/add_provider', methods=['POST'])
@login_required
def add_provider():
"""
Handle submission of new provider information entered by user
"""
fields = ['first_name', 'last_name', 'patient', 'phone', 'address']
data = {field: request.form.get(field) or '' for field in fields}
required_fields = ['last_name', 'phone', 'address']
for field in required_fields:
if not data[field]:
flash(f"Please provide a valid {field.replace('_', ' ')}.")
return redirect(url_for('userinfo_routes.providers'))
try:
new_provider = Providers(user_id=session['user_id'], first_name=data['first_name'],
last_name=data['last_name'], patient=data['patient'], phone=data['phone'], address=data['address'])
db_session.add(new_provider)
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error adding provider.")
return redirect(url_for('userinfo_routes.providers'))
@userinfo_routes.route('/delete_provider', methods=['POST'])
@login_required
def delete_provider():
"""
Delete provider info selected by user
"""
if not request.form.get('person_id'):
flash("Invalid person ID.")
return redirect(url_for('userinfo_routes.providers'))
person_id = request.form.get('person_id')
try:
db_session.query(Providers).filter_by(id=person_id).delete()
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error deleting provider.")
return redirect(url_for('userinfo_routes.providers'))
@userinfo_routes.route('/edit_provider', methods=['POST'])
@login_required
def edit_provider():
"""
Edit provider info as submitted by user
"""
fields = ['person_id', 'first_name', 'last_name', 'patient', 'phone', 'address']
data = {field: request.form.get(field) or '' for field in fields}
required_fields = ['person_id', 'last_name', 'phone', 'address']
for field in required_fields:
if not data[field]:
flash(f"Please provide a valid {field.replace('_', ' ')}.")
return redirect(url_for('userinfo_routes.providers'))
try:
db_session.query(Providers).filter_by(id=data['person_id']).update({
'first_name': data['first_name'],
'last_name': data['last_name'],
'patient': data['patient'],
'phone': data['phone'],
'address': data['address']
})
db_session.commit()
except SQLAlchemyError as e:
db_session.rollback()
app.logger.error("Database error: %s", e)
flash("Error updating provider information.")
return redirect(url_for('userinfo_routes.providers'))
@userinfo_routes.route('/user_state', methods=["GET"])
@login_required
def user_state():
"""
Provide user's state from db to fullCalendar
"""
try:
subquery = db_session.query(Families.state_id).filter_by(user_id=session['user_id'])
state_record = db_session.query(States.full_name).filter(States.id.in_(subquery)).first()
state = state_record.full_name if state_record else None
except SQLAlchemyError as e:
app.logger.error("Database error: %s", e)
return apology("Error retrieving user state.")
return jsonify(state=state)
def validate_form(form_data):
"""
Function to validate form data when user enters household info
"""
errors = []
validated_data = {}
# Get information from form and return apology if any fields empty
required_fields = ['name', 'adults', 'seniors', 'children', 'pets', 'state', 'special']
if not request.form:
errors.append("No form data.")
return errors, validated_data
form_data = request.form
for field in required_fields:
if not form_data.get(field):
errors.append(f"{field.replace('_', ' ').capitalize()} is required.")
try:
validated_data["adults"] = int(form_data.get('adults'))
validated_data["seniors"] = int(form_data.get('seniors'))
validated_data["children"] = int(form_data.get('children'))
validated_data["pets"] = int(form_data.get('pets'))
except ValueError as e:
app.logger.error("Value error: %s", e)
errors.append("Please enter valid numbers for adults, seniors, children, and pets.")
# Validate state against db
state = form_data.get('state')
if not re.match("^[A-Za-z]+$", state) or state not in g.states:
app.logger.error("Invalid state.")
errors.append("Not a valid state.")
if not errors:
validated_data["name"] = form_data.get('name')
validated_data["special"] = form_data.get('special')
validated_data["state"] = state
return errors, validated_data