-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
956 lines (773 loc) · 32 KB
/
main.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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
'''
Title: Employee Recognition System
Authors: Sarah Cawley, Ryan Cosby, and Conrad Lewin
Oregon State University Capstone Course
Description: This is the main routing file used to handle all of the server side routing calls for our
employee recognition system. The application is used to deliver award recognition emails to employee
recipients. Admin users are able to monitor users and the number of awards being delivered to employees.
'''
from __future__ import print_function #debug
import os
import logging
import json, boto3
from handlers.LaTex import award as ah
from handlers.Database import database
from handlers.Database import models
from handlers.Email import email
from handlers.Report import report
from string import replace
from flask import Flask, render_template, send_file, abort, request, redirect, url_for, jsonify, session, Response, flash
from flask_cors import CORS, cross_origin
from flask_login import LoginManager, login_required, current_user, login_user, logout_user
#"constants" used to change the color of messages flashed to the
#screen after CRUD operations performed by the user
ERROR = 'red'
SUCCESS = 'green'
#init Flask framework
app = Flask('app',template_folder='./templates',static_folder='./static')
app.secret_key = os.environ['SECRET_KEY']
#init CORS
CORS(app)
#connect database to application
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
database.db.init_app(app)
#init Flask login functionality
loginManager = LoginManager()
loginManager.init_app(app)
#init object that will handle database operations
alchemist = database.PostgresDatabase(
models.Question,
models.Account,
models.Admin,
models.Manager,
models.AwardType,
models.Award,
models.AwardBackground,
models.AwardTheme,
models.Employee,
models.AwardBorder)
#init object that will handle email functionality
emailer = email.Emailer()
#init object that will handle report data retrieval
reporter = report.Reporter(alchemist.database)
#landing page route
@app.route('/')
def renderIndex():
#render the landing page
return render_template('index.html')
#login page route
@app.route('/login',methods=['GET','POST'])
def renderLogin():
'''
GET: render login page
POST: using data provided by the login form, validate the user's credentials and
display a status message regarding the state of the login. Relevant session variables
are saved (email, role, name and title(if regular user)).
'''
if request.method == 'GET':
return render_template('login.html')
if request.method == 'POST':
payload = request.form
response = alchemist.login(payload)
if response['status'] != 200:
flash(response['message'],ERROR)
return render_template('login.html')
session['email'] = payload['userName']
session['role'] = payload['account-type']
status = alchemist.setAuthenticated(response['account'],True)
if status == True:
login_user(response['account'])
if payload['account-type'] == 'admin':
session['name'] = '{0} {1}'.format(response['account'].admin.fname,response['account'].admin.lname)
return redirect(url_for('renderAdmin'))
else:
session['name'] = '{0} {1}'.format(response['account'].manager.fname,response['account'].manager.lname)
session['title'] = response['account'].manager.title
return redirect(url_for('renderUser'))
else:
abort(401)
#logout page route
@app.route('/logout')
def renderLogout():
'''logout the user and render the logout page'''
alchemist.setAuthenticated(current_user,False)
logout_user()
return render_template('logout.html')
#admin portal route (admin user must be logged in to access this page)
@app.route('/admin')
@login_required
def renderAdmin():
'''render the admin portal'''
if session['role'] == 'admin':
return render_template('admin.html', username=session['name'],email=session['email'],updateRoute='update-admin-account')
else:
abort(401)
# admin listing rout
@app.route('/admins')
@login_required
def renderAdmins():
'''renders a table of all of the admins in the system'''
if session['role'] == 'admin':
admins = alchemist.getAdmins(session['email'])
return render_template('admins-list.html', admins=admins, username=session['name'], email=session['email'],updateRoute='update-admin-account')
else:
abort(401)
#regular user portal route (regular user must be logged in to access this page)
@app.route('/user')
@login_required
def renderUser():
'''render the user portal'''
if session['role'] == 'user':
return render_template('user.html',username=session['name'],email=session['email'])
else:
abort(401)
#update account route (regular user must be logged in to access this page)
@app.route('/update-account',methods=['GET','POST'])
@login_required
def renderUpdateAccount():
'''
after ensuring a regular user is accessing the route...
GET: retrieve the logged in user's account details from the database and then render the page
POST: get form data and update the user's account in the database. save the updated user details
in the session dictionary and display a status message
'''
if session['role'] == 'user':
if request.method == 'GET':
details = alchemist.getUserDetails(session['email'])
if details is None:
abort(500)
return render_template('update-account.html',username=session['name'],details=details)
if request.method == 'POST':
payload = request.form
status = alchemist.updateAccount(payload,session['email'])
if status == False:
flash('Unable to update account. Either the email provided is already linked to an account or there was a server error. Please, try again.', ERROR)
return redirect(url_for('renderUpdateAccount'))
session['name'] = '{0} {1}'.format(payload['firstName'],payload['lastName'])
session['email'] = payload['email']
session['title'] = payload['jobTitle']
##flash('Account was successfully updated.',SUCCESS) <---this is not displaying correctly
return redirect(url_for('renderUser'))
else:
abort(401)
#create award route (regular user must be logged in to access this page)
@app.route('/create')
@login_required
def renderCreate():
'''
after ensuring a regular user is accessing the route, retrieve award assets from the database
and then render the page.
'''
if session['role'] == 'user':
awardBackgrounds = alchemist.getAwardBackgrounds()
awardThemes = alchemist.getAwardThemes()
awardTypes = alchemist.getAwardTypes()
return render_template('create.html', username=session['name'], awardBackgrounds=awardBackgrounds, awardThemes=awardThemes, awardTypes=awardTypes)
else:
abort(401)
#create new regular user account route
@app.route('/new-account',methods=['GET','POST'])
def renderNewAccount():
'''
GET: get the list of available security questions from the database, then render the page
POST: get form data, use that data to insert account information into the database and
then display a status message before rendering the login (if successful) or new account
page (if unsuccessful).
'''
if request.method == 'GET':
questions = alchemist.getQuestions()
return render_template('new-account.html',questions=questions)
if request.method == 'POST':
payload = request.form
account = alchemist.createAccount(payload)
status = alchemist.save(account)
if status == False:
flash('Unable to create account. The email provided is already linked to an account.',ERROR)
return redirect(url_for('renderNewAccount'))
flash('Account created.',SUCCESS)
return redirect(url_for('renderLogin'))
#create a new admin account route
@app.route('/new-admin-account',methods=['GET','POST'])
def renderNewAdminAccount():
'''
GET: renders the new admin account submittal form
POST: get form data, use that data to insert account information into the database and
then display a status message before rendering the login (if successful) or new account
page (if unsuccessful)
'''
if request.method == 'GET':
questions = alchemist.getQuestions()
return render_template('new-admin-account.html',questions=questions, username=session['name'], email=session['email'],updateRoute='update-admin-account')
if request.method == 'POST':
payload = request.form
account = alchemist.createAdminAccount(payload)
status = alchemist.save(account)
if status == False:
flash('Unable to create admin account. The email provided is already linked to an account.',ERROR)
return redirect(url_for('renderNewAdminAccount', username=session['name'], email=session['email']))###,updateRoute='update-admin-account' maybe
flash('Admin account created.',SUCCESS)
return redirect(url_for('renderAdmin', username=session['name'], email=session['email']))
#update current logged in admin account route here
@app.route('/update-admin-account/',methods=['GET','POST'])
@login_required
def renderUpdateAdminAccount():
'''
GET: renders the update admin account submittal form
POST: get form data, use that data to update account information into the database and
then display a status message before rendering the login (if successful) or new account
page (if unsuccessful)
'''
if session['role'] == 'admin':
if request.method == 'GET':
details = alchemist.getAdminDetails(session['email'])
if details is None:
abort(500)
return render_template('update-admin-account.html',username=session['name'], email=session['email'], details=details,updateRoute='update-admin-account')
if request.method == 'POST':
payload = request.form
status = alchemist.updateAdminAccount(payload,session['email'])
if status == False:
flash('Unable to update admin account. Either the email provided is already linked to an account or there was a server error. Please, try again.', ERROR)
return redirect(url_for('renderUpdateAdminAccount', username=session['name'], email=session['email'], details=details))
session['email'] = payload['email']
session['name'] = '{0} {1}'.format(payload['firstName'],payload['lastName'])
flash('Admin account was successfully updated.',SUCCESS)
return redirect(url_for('renderAdmins'))
else:
abort(401)
#update other admin account route
@app.route('/update-other-admin-account/',methods=['GET','POST'])
@login_required
def renderUpdateOtherAdminAccount():
'''
GET: renders the update admin account submittal form
POST: get form data, use that data to update account information into the database and
then display a status message before rendering the login (if successful) or new account
page (if unsuccessful)
'''
if session['role'] == 'admin':
if request.method == 'GET':
session['admin-email'] = request.args.get('adminemail')
details = alchemist.getAdminDetails(session['admin-email'])
if details is None:
abort(500)
return render_template('update-other-admin-account.html',username=session['name'], email=session['email'], details=details, updateRoute='update-admin-account')
if request.method == 'POST':
payload = request.form
status = alchemist.updateAdminAccount(payload,session['admin-email'])
if status == False:
flash('Unable to update admin account. Either the email provided is already linked to an account or there was a server error. Please, try again.', ERROR)
return redirect(url_for('renderOtherUpdateAdminAccount', username=session['name'], email=session['email'], details=details, updateRoute='update-admin-account'))
flash('Admin account was successfully updated.',SUCCESS)
return redirect(url_for('renderAdmins'))
else:
abort(401)
#delete admin account route
@app.route('/remove-admin/')
def removeAdminUser():
'''
Finds the selected admin account from the admin listing table and deletes the account.
The admin list is rendered again to show the account has been removed
'''
adminID = request.args.get('admin')
admin = alchemist.getAdmin(adminID)
status = alchemist.remove(admin.account)
admins = alchemist.getAdmins(session['email'])
if status == False:
flash('Unable to remove admin. System Error.', ERROR)
return redirect(url_for('renderAdmins', admins=admins, username=session['name'], email=session['email']))
flash('Admin deleted.', SUCCESS)
return redirect(url_for('renderAdmins', admins=admins, username=session['name'], email=session['email']))
#create a new standard user account from admin login
@app.route('/new-manager-account',methods=['GET','POST'])
@login_required
def renderNewUserAccount():
'''
GET: renders the new user account submittal form
POST: get form data, use that data to insert account information into the database and
then display a status message before rendering the login (if successful) or new account
page (if unsuccessful)
'''
if session['role'] == 'admin':
if request.method == 'GET':
questions = alchemist.getQuestions()
return render_template('new-manager-account.html',questions=questions, username=session['name'], email=session['email'],updateRoute='update-admin-account')
if request.method == 'POST':
payload = request.form
account = alchemist.createAccount(payload)
status = alchemist.save(account)
if status == False:
flash('Unable to create account. The email provided is already linked to an account.',ERROR)
return redirect(url_for('renderNewUserAccount', username=session['name'], email=session['email']))
flash('Account created.',SUCCESS)
return redirect(url_for('renderAdmin', username=session['name'], email=session['email']))
else:
abort(401)
# update standard user account from admin login
@app.route('/update-manager-account/',methods=['GET','POST'])
@login_required
def renderUpdateUserAccount():
'''
GET: renders the update account submittal form
POST: get form data, use that data to update account information into the database and
then display a status message before rendering the login (if successful) or update account
page (if unsuccessful)
'''
if session['role'] == 'admin':
if request.method == 'GET':
session['manager-email'] = request.args.get('usremail')
details = alchemist.getUserDetails(session['manager-email'])
if details is None:
abort(500)
return render_template('update-manager-account.html',username=session['name'], email=session['email'], details=details,updateRoute='update-admin-account')
if request.method == 'POST':
payload = request.form
status = alchemist.updateAccount(payload,session['manager-email'])
if status == False:
flash('Unable to update account. Either the email provided is already linked to an account or there was a server error. Please, try again.', ERROR)
return redirect(url_for('renderUpdateUserAccount', username=session['name'], email=session['email'], details=details))
flash('Account was successfully updated.',SUCCESS)
return redirect(url_for('renderUsers'))
else:
abort(401)
#add new employee route
@app.route('/new-employee', methods=['GET','POST'])
@login_required
def addNewEmployee():
'''
GET: renders the new employee submittal form
POST: get form data, use that data to insert employee information into the database and
then display a status message before rendering the login (if successful) or new employee
page (if unsuccessful)
'''
if request.method == 'GET':
return render_template('new-employee.html', username=session['name'], email=session['email'], updateRoute='update-admin-account')
if request.method == 'POST':
payload = request.form
employee = alchemist.createEmployee(payload)
status = alchemist.save(employee)
if status == False:
flash('Unable to create employee. System Error.',ERROR)
return redirect(url_for('addNewEmployee', username=session['name'], email=session['email']))
flash('Employee Added: ' + employee.fname +' '+ employee.lname ,SUCCESS)
return redirect(url_for('renderEmployees', username=session['name'], email=session['email']))
#update employee info route
@app.route('/update-employee/',methods=['GET','POST'])
@login_required
def renderUpdateEmployee():
'''
GET: renders the update employee submittal form
POST: get form data, use that data to update employee information into the database and
then display a status message before rendering the login (if successful) or update employee
page (if unsuccessful)
'''
if session['role'] == 'admin':
if request.method == 'GET':
employeeID = request.args.get('employee')
employee = alchemist.getEmployee(employeeID)
if employee is None:
abort(500)
return render_template('update-employee.html',username=session['name'], email=session['email'], employee=employee,updateRoute='update-admin-account')
if request.method == 'POST':
payload = request.form
status = alchemist.updateEmployee(payload)
if status == False:
flash('Unable to employee information. System Error', ERROR)
return redirect(url_for('renderUpdateEmployee', username=session['name'], email=session['email']))
flash('Employee information was successfully updated',SUCCESS)
return redirect(url_for('renderEmployees',username=session['name'], email=session['email']))
else:
abort(401)
#view awards route (regular user must be logged in to access this page)
@app.route('/awards')
@login_required
def renderAwards():
'''
after ensuring a regular user is accessing the route, get all awards that user has
created from the database and then render the page.
'''
if session['role'] == 'user':
awards = alchemist.getAwards(session['email'])
return render_template('user-awards-list.html', awards=awards, username=session['name'], email=session['email'])
else:
abort(401)
#route to view listing of non-admin users
@app.route('/users')
@login_required
def renderUsers():
'''
after ensuring an admin user is accessing the route, get all users in the database
and render the table in the page.
'''
if session['role'] == 'admin':
users = alchemist.getUsers()
return render_template('users-list.html', users=users, username=session['name'], email=session['email'],updateRoute='update-admin-account')
else:
abort(401)
#remove awards route
@app.route('/remove-award/')
def removeAward():
'''
get the id of the award to be removed (retrieved from the page),
find that award in the database, remove it and then display a status message.
retrieve all remaining awards from the database so that they can be displayed
once the page is reloaded.
'''
awardID = request.args.get('awd')
award = alchemist.getAward(awardID)
status = alchemist.remove(award)
awards = alchemist.getAwards(session['email'])
if status == False:
flash('Unable to remove award. System Error.', ERROR)
return redirect(url_for('renderAwards', awards=awards, username=session['name'], email=session['email']))
flash('Award record deleted', SUCCESS)
return redirect(url_for('renderAwards', awards=awards, username=session['name'], email=session['email']))
#remove user route
@app.route('/remove-user/')
def removeUser():
'''
get the id of the user to be removed (retrieved from the page),
find that user in the database, remove them (as well as their
signature image from the s3 bucket) and then display a status message.
retrieve all remaining users from the database so that they can be displayed
once the page is reloaded.
'''
userID = request.args.get('usr')
user = alchemist.getUser(userID)
users = alchemist.getUsers()
sigFilename = user.signature
sigFilename = sigFilename.replace('https://camelopardalis-assets.s3.amazonaws.com/',"")
if alchemist.remove(user.account) == False:
flash('Unable to remove user. System Error.', ERROR)
return redirect(url_for('renderUsers', users=users, username=session['name'], email=session['email']))
else:
alchemist.deleteUserSig(sigFilename)
awards = alchemist.getAllAwards()
if awards is not None:
for a in awards:
if a.check_row() == True:
alchemist.remove(a)
flash('User deleted.', SUCCESS)
return redirect(url_for('renderUsers', users=users, username=session['name'], email=session['email']))
#list/add award types route (admin user must be logged in to access this page)
@app.route('/award-types',methods=['GET','POST'])
@login_required
def renderAwardTypes():
'''
after ensuring an admin user is accessing the route...
GET: get all available award types from database and render the page
POST: get form data, add that data into the database and then display a status message
before reloading the page.
'''
if session['role'] == 'admin':
if request.method == 'GET':
types = alchemist.getAwardTypes()
return render_template('add-award-type.html',types=types,username=session['name'], email=session['email'],updateRoute='update-admin-account')
if request.method == 'POST':
if alchemist.addAwardType(request.form) == False:
flash('Unable to create award type. Make sure your are not trying to create a duplicate type.',ERROR)
else:
flash('New award type created.',SUCCESS)
return redirect(url_for('renderAwardTypes'))
else:
abort(401)
#display reports route (admin user must be logged in to access this route)
@app.route('/reports',methods=['GET','POST'])
@login_required
def renderReports():
'''
after ensuring an admin user is accessing the route...
GET: render the page
POST: receive a json request object containing a report code from a button on the page.
retrieve the appropriate data needed to generate the selected report and then return that data
as a json object
'''
if session['role'] == 'admin':
if request.method == 'GET':
return render_template('reports.html',username=session['name'], email=session['email'],updateRoute='update-admin-account')
if request.method == 'POST':
if request.json:
payload = request.get_json()
if payload['report'] == '1':
response = reporter.getAllAwards()
elif payload['report'] == '2':
response = reporter.getAwardsByManager()
elif payload['report'] == '3':
response = reporter.getAwardsByEmployee()
else:
abort(400)
return jsonify(response)
else:
abort(400)
else:
abort(401)
#internal route used to submit signature file to Amazon S3 service
#Source: https://devcenter.heroku.com/articles/s3-upload-python
@app.route('/sign_s3/')
def sign_s3():
S3_BUCKET = os.environ.get('S3_BUCKET_NAME')
file_name = request.args.get('file_name')
file_type = request.args.get('file_type')
s3 = boto3.client('s3')
presigned_post = s3.generate_presigned_post(
Bucket = S3_BUCKET,
Key = file_name,
Fields = {"acl": "public-read", "Content-Type": file_type},
Conditions = [
{"acl": "public-read"},
{"Content-Type": file_type}
],
ExpiresIn = 3600
)
return json.dumps({
'data': presigned_post,
'url': 'https://%s.s3.amazonaws.com/%s' % (S3_BUCKET, file_name)
})
#internal route used to delete signature files from the system when user accoutns are deleted
@app.route('/delete_s3/')
def delete_s3():
file_name = request.args.get('file_name')
print (file_name)
alchemist.deleteUserSig(file_name);
return json.dumps({'status': "success"})
#forgot password route
@app.route('/password')
def renderPassword():
'''render forgot password page'''
return render_template('password.html')
#award pdf generation route (a regular user must be logged in to make post requests to the this route)
@app.route('/latex', methods=['POST'])
@login_required
def renderPDF():
#get form data and insert that data into the database
payload = request.form
award = alchemist.createAward(payload, session['email'])
if award is None:
abort(404)
status = alchemist.save(award)
if status == False:
abort(500)
#----------------------------------------------------
#form the filename used to retrieve signature from the s3 bucket
sigFile = session['email']
sigFile = replace(sigFile,'@','_')
sigFile = replace(sigFile,'.','_')
sigFile += '_sig.png'
#download the signagure image from the bucket
alchemist.downloadUserSig(session['email'])
#LaTex code used to generate the two different award borders
if payload['border'] == '1':
border = r'''{\border \char113} % up left
{\border \char109} % up
{\border \char112} % up right
{\border \char108} % left
{\border \char110} % right
{\border \char114} % lower left
{\border \char111} % bottom
{\border \char115} % lower right'''
else:
border = r'''{\border \char005} % up left
{\border \char001} % up
{\border \char004} % up right
{\border \char002} % left
{\border \char000} % right
{\border \char006} % lower left
{\border \char003} % bottom
{\border \char007} % lower right'''
#form award details to be inserted into the LaTex template
filename = 'award'
awardDate = award.issuedOn
awardDateString = awardDate.strftime("%m-%d-%Y")
awdDetails = {
'background':'static/images/' + award.award_background.filename,
'border': border,
'color':award.award_theme.theme,
'logo':'static/images/gateway.png',
'company':'Gateway Mapping, Inc.',
'message': award.message,
'type': award.award_type.name,
'employee':award.employee.fname + ' ' + award.employee.lname,
'admin':session['name'],
'adminTitle':session['title'],
'signature':sigFile,
'granted':awardDateString}
#------------------------------------------------------------
#generate the pdf file
employeeAward = ah.Award(awdDetails,filename)
pdf = employeeAward.genAward()
#if pdf was successfully generated...
if pdf is not None:
#if the user elects to preview the award, then remove the award from the database
#download the pdf to the user's local machine
if 'preview-btn' in payload:
alchemist.remove(award)
return send_file(pdf, as_attachment=True)
#otherwise, email the pdf to the employee specified in the form
elif 'email-btn' in payload:
sender = session['email']
recipient = award.employee.email
response = emailer.sendAward(sender,recipient,pdf)
#display a status message
if response.status_code != 200 and response.status_code != 202:
flash('An error occured and the email was not sent. Please, try again.',ERROR)
else:
flash('The award has been emailed.',SUCCESS)
return redirect(url_for('renderCreate'))
#otherwise, remove the award from the database and display an error
else:
alchemist.remove(award)
abort(500)
#get a list of employees route used for create award page
@app.route('/get-employee',methods=['POST'])
def getEmployees():
'''
receives a json request with a string representing all or part of an
employee's last name. this data is then used to query the database
for all employees with last names matching that string.
'''
if request.json:
payload = request.get_json()
employees = alchemist.getEmployees(payload)
return jsonify(employees)
else:
abort(400)
#get a list of employees route used for employee list page for logged in admin
@app.route('/employees-list')
def renderEmployees():
if session['role'] == 'admin':
employees = alchemist.getAllEmployees()
return render_template('employees-list.html', employees=employees, username=session['name'], email=session['email'],updateRoute='update-admin-account')
else:
abort(401)
#remove employee route
@app.route('/remove-employee/')
def removeEmployee():
'''
get the id of the employee to be removed (retrieved from the page),
find that employee in the database, remove it and then display a status message.
retrieve all remaining employees from the database so that they can be displayed
once the page is reloaded. a check is then made to ensure that there are no awards
in the database that lack both a creator and a recipient. if such an award is found,
it is removed from the database.
'''
userID = request.args.get('employee')
employee = alchemist.getEmployee(userID)
employees = alchemist.getAllEmployees()
if alchemist.remove(employee) == False:
flash('Unable to remove employee. System Error.', ERROR)
return redirect(url_for('renderEmployees', employees=employees, username=session['name'], email=session['email']))
awards = alchemist.getAllAwards()
if awards is not None:
for a in awards:
if a.check_row() == True:
alchemist.remove(a)
flash('Employee deleted.', SUCCESS)
return redirect(url_for('renderEmployees', employees=employees, username=session['name'], email=session['email']))
#get account password route
@app.route('/get-password',methods=['POST'])
def getPassword():
'''
receive json request containing the email linked to the account requesting a password reset
and then find that account in the database. if such an account exists, identify its role and
get the account details, namely the password. extract the reset method from the json request
and return either the security questions linked to the account or send an email to the account
holder with a verification code that will enable them to reset their password.
'''
if request.json:
payload = request.get_json()
user = alchemist.findUser(payload['email'])
if user['role'] == None:
response = {'status':404,'message':'The email you provided is not linked to an account.'}
return jsonify(response)
if user['role'] == 'user':
details = alchemist.getUserDetails(payload['email'])
elif user['role'] == 'admin':
details = alchemist.getAdminDetails(payload['email'])
if payload['reset-method'] == 'question':
response = {'one':str(details['question1']), 'two':str(details['question2']),'status':200}
return jsonify(response)
elif payload['reset-method'] == 'email':
code = alchemist.genVerificationCode(details['account'])
if code is not None:
response = emailer.sendPasswordReset(payload['email'],code)
else:
abort(500)
return jsonify(response)
else:
abort(400)
#reset password (via email/verification code) route
@app.route('/reset-password', methods=['GET', 'POST'])
def resetPasswordViaEmail():
'''
GET: render reset password page
POST: get form data, validate verfication code and reset account password, then
display a status message before rendering the login page (if reset was successful)
or the reset password page (if unsuccessful).
'''
if request.method == 'GET':
return render_template('/reset-password.html')
if request.method == 'POST':
payload = request.form
response = alchemist.resetPasswordByEmail(payload)
if response['status'] != 200:
flash(response['message'],ERROR)
return redirect(url_for('resetPasswordViaEmail'))
flash(response['message'],SUCCESS)
return redirect(url_for('renderLogin'))
#reset password by security question route
@app.route('/reset-pass-via-question', methods=['POST'])
def resetPasswordViaQuestion():
'''
POST: get form data, validate the answers given for the security questions then
display a status message before rendering the login page.
'''
payload = request.form
status = alchemist.resetPasswordByQuestions(payload)
if status == False:
abort(500)
flash('Password was reset.',SUCCESS)
return redirect(url_for('renderLogin'))
#validate security questions route
@app.route('/check-questions',methods=['POST'])
def checkQuestions():
'''
get json request containing the answers to an account's security questions,
verify those answers against data in the database and return the status.
'''
if request.json:
payload = request.get_json()
response = alchemist.verifyAnswers(payload)
return jsonify(response)
else:
abort(400)
#ensures a user is logged in to the application
@loginManager.user_loader
def accountLoader(id):
return alchemist.getAccount(id)
@app.route('/jquery')
def jquerytest():
return render_template('jquery.html')
#handles server errors
@app.errorhandler(500)
def serverError(e):
# Log the error and stacktrace.
logging.exception('An error occurred during a request.')
return 'An internal error occurred: ' + str(e), 500
#handles resource not found errors, i.e. when queried data is not found in the database
@app.errorhandler(404)
def resourceNotFoundError(e):
logging.exception('An error occurred during a request.')
return 'An internal error occurred: ' + str(e), 400
#handles errors that occur when users access routes they are not authorized to view
@app.errorhandler(401)
def unauthorizedError(e):
return '<h1>You are not authorized to access this page.</h1> \
<p>Please login <a href=/login>here</a></p>', 401
#handles errors that arise when a request is malformed
@app.errorhandler(400)
def badRequestError(e):
logging.exception('An error occurred during a request.')
return 'An internal error occurred: ' + str(e), 400
#main
if __name__ == "__main__":
app.run()