-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
2164 lines (1742 loc) · 90.9 KB
/
application.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
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sqlite3
from datetime import datetime
from flask import Flask, render_template, request, jsonify, redirect, url_for, session
import hashlib
from datetime import date, timedelta, datetime
import secrets
def numOfDays(date1, date2):
return (date2-date1).days
application = Flask(__name__)
#flask application creates a secret key
application.secret_key = secrets.token_hex(16)
# puts a timer on how long a user can be in a session. after time, user is sent to home page to restart session
application.permanent_session_lifetime = timedelta(minutes=10)
#sessions are created and if user logs out to go to home page, the loggedin session is popped
@application.route('/')
def home():
if "loggedin" in session:
session.pop("loggedin")
session["code"] = True
session["volunteer"] = True
session["admin"] = True
return render_template('sewawebsitehome.html')
#when user logs out, this pops all the session and redirects to home page
@application.route('/logout')
def logout():
session.pop("loggedin")
if "admin" in session:
session.pop("admin")
if "superadmin" in session:
session.pop("superadmin")
elif "volunteer" in session:
session.pop("volunteer")
return redirect(url_for('home'))
#if user types in signup url when signed in, it signs them out
@application.route('/signup')
def signup():
if "loggedin" in session:
session.pop("loggedin")
return render_template('sewawebsitesignup.html')
#this will end superadmin session so that no one can access it but me
@application.route('/loghours')
def logHours():
if "loggedin" in session and "volunteer" in session:
return render_template('sewawebsiteloghours.html')
else:
return redirect(url_for('home'))
@application.route('/manageadmins')
def manageAdmins():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsitemanageadmins.html')
else:
return redirect(url_for('home'))
@application.route('/reinstateadmins')
def reinstateAdmins():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsitereinstate.html')
else:
return redirect(url_for('home'))
@application.route('/addevents')
def addEvents():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteaddevents.html')
else:
return redirect(url_for('home'))
@application.route('/addcategory')
def addCategory():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteaddcategory.html')
else:
return redirect(url_for('home'))
@application.route('/removeevents')
def removeEvents():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteremoveevents.html')
else:
return redirect(url_for('home'))
@application.route('/removecategory')
def removeCategory():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteremovecategory.html')
else:
return redirect(url_for('home'))
@application.route('/adminprofile')
def adminProfile():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteprofileadmin.html')
else:
return redirect(url_for('home'))
@application.route('/profile')
def volunteerProfile():
if "loggedin" in session and "volunteer" in session:
return render_template('sewawebsiteprofile.html')
else:
return redirect(url_for('home'))
@application.route('/adminhome')
def adminHome():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteadmin.html')
else:
return redirect(url_for('home'))
@application.route('/approvedhours')
def approvedHours():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteapproved.html')
else:
return redirect(url_for('home'))
@application.route('/certifiedhours')
def certifiedHours():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsitecertified.html')
else:
return redirect(url_for('home'))
@application.route('/rejectedhours')
def rejectedHours():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiterejected.html')
else:
return redirect(url_for('home'))
@application.route('/unapprovedhours')
def unapprovedHours():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsiteunapproved.html')
else:
return redirect(url_for('home'))
@application.route('/yearend')
def yearendHours():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsitemedals.html')
else:
return redirect(url_for('home'))
@application.route('/certify')
def certify():
if "loggedin" in session and "admin" in session:
return render_template('sewawebsitecertify.html')
else:
return redirect(url_for('home'))
@application.route('/forgotpass')
def forgot():
if "loggedin" in session:
session.pop("loggedin")
if "code" in session:
return render_template('sewawebsiteforgotmessage.html')
else:
return redirect(url_for('home'))
@application.route('/code')
def code():
if "loggedin" in session:
session.pop("loggedin")
return render_template('sewawebsitecode.html')
#only whenn user enters verification code and email can they access this page. if they didnt enter email, they can access, but wont work
@application.route('/change')
def change():
if "loggedin" in session:
session.pop("loggedin")
if "code" in session:
session.pop("code")
return render_template('sewawebsitechangepass.html')
else:
return redirect(url_for('home'))
#only superadmin(me) can access this page
@application.route("/sql")
def sqlCommand():
if "admin" in session and "superadmin" in session and "loggedin" in session:
return render_template("sewawebsiteSQLeditor.html")
else:
return redirect(url_for("home"))
##### METHODS ######
#this will take the username of a user and appropriatly set a hashed username to then display in local storage
@application.route('/hashing', methods=['POST'])
def hashing():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = str(dataGet[0]["username"])
salt = "p@$$1NgV@R!@B1Es"
dataBase_username = username+salt
hashed = hashlib.md5(dataBase_username.encode())
hashed_username = hashed.hexdigest()
volunteer_username = cursor.execute("SELECT COUNT(volunteerUsername) FROM volunteers WHERE volunteerUsername = ?", (username,),).fetchall()
admin_username = cursor.execute("SELECT COUNT(adminUsername) FROM admins WHERE adminUsername = ?", (username,),).fetchall()
if volunteer_username == [(1,)]:
cursor.execute("UPDATE volunteers SET hashedUsername = ? WHERE volunteerUsername = ?", (hashed_username, username))
connection.commit()
connection.close()
return jsonify({"hashed" : hashed_username})
elif admin_username == [(1,)]:
cursor.execute("UPDATE admins SET hashedUsername = ? WHERE adminUsername = ?", (hashed_username, username))
connection.commit()
connection.close()
return jsonify({"hashed" : hashed_username})
else:
connection.close()
return jsonify({})
# this will take a email from a user and hashe it and store it in database to store in local storage. used for forgot password
@application.route('/hashingtwo', methods=['POST'])
def hashingtwo():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
email = str(dataGet[0]["email"])
salt = "p@$$1NgV@R!@B1Es"
dataBase_email = email+salt
hashed = hashlib.md5(dataBase_email.encode())
hashed_email = hashed.hexdigest()
volunteer_email = cursor.execute("SELECT COUNT(volunteerEmail) FROM volunteers WHERE volunteerEmail = ?", (email,),).fetchall()
admin_email = cursor.execute("SELECT COUNT(adminEmail) FROM admins WHERE adminEmail = ?", (email,),).fetchall()
if volunteer_email == [(1,)]:
cursor.execute("UPDATE volunteers SET hashedEmail = ? WHERE volunteerEmail = ?", (hashed_email, email))
connection.commit()
connection.close()
return jsonify({"hashed" : hashed_email})
elif admin_email == [(1,)]:
cursor.execute("UPDATE admins SET hashedEmail = ? WHERE adminEmail = ?", (hashed_email, email))
connection.commit()
connection.close()
return jsonify({"hashed" : hashed_email})
else:
connection.close()
return jsonify({})
# this gets the hashed email and finds the real email and uses it in transactions in app
@application.route('/gethashedtwo', methods=['POST'])
def getHashedtwo():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
email = str(dataGet[0]["email"])
hashed_v = cursor.execute("SELECT volunteerEmail FROM volunteers WHERE hashedEmail = ?", (email,),).fetchall()
hashed_a = cursor.execute("SELECT adminEmail FROM admins WHERE hashedEmail = ?", (email,),).fetchall()
if hashed_v != []:
return jsonify({"unhash" : hashed_v[0][0]})
else:
return jsonify({"unhash" : hashed_a[0][0]})
# this gets the hashed username and converts it into real username to use in update statments
@application.route('/gethashed', methods=['POST'])
def getHashed():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = str(dataGet[0]["username"])
hashed_v = cursor.execute("SELECT volunteerUsername FROM volunteers WHERE hashedUsername = ?", (username,),).fetchall()
hashed_a = cursor.execute("SELECT adminUsername FROM admins WHERE hashedUsername = ?", (username,),).fetchall()
if hashed_v != []:
return jsonify({"unhash" : hashed_v[0][0]})
else:
return jsonify({"unhash" : hashed_a[0][0]})
# this creates the account of user and inserts into database
@application.route('/account', methods=['POST'])
def volunteerInfo():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
password = str(dataGet[1]['password'])
firstname = dataGet[2]['firstname']
lastname = dataGet[3]['lastname']
email = dataGet[4]['email']
chapter = dataGet[5]['chapter']
birthday = dataGet[6]['birthday']
eligibility = dataGet[7]['eligibility']
admin = dataGet[8]['admin']
adminStatus = "Pending"
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
salt = "?Sew@!NtErNaT!0NaLh0urS?"
dataBase_password = password+salt
hashed = hashlib.md5(dataBase_password.encode())
hashed_password = hashed.hexdigest()
if admin == "No":
duplicate_user = cursor.execute("""SELECT COUNT(volunteerUsername) FROM volunteers WHERE volunteerUsername = ?""", (username,),).fetchall()
duplicate_user_a = cursor.execute("""SELECT COUNT(adminUsername) FROM admins WHERE adminUsername = ?""", (username,),).fetchall()
duplicate_email = cursor.execute("""SELECT COUNT(volunteerEmail) FROM volunteers WHERE volunteerEmail = ?""", (email,),).fetchall()
duplicate_email_a = cursor.execute("""SELECT COUNT(adminEmail) FROM admins WHERE adminEmail = ?""", (email,),).fetchall()
if duplicate_user == [(1,)] or duplicate_user_a == [(1,)]:
return jsonify({"duplicate" : "yes"})
else:
if duplicate_email == [(1,)] or duplicate_email_a == [(1,)]:
return jsonify({"email_dup": 'yes'})
else:
cursor.execute("""INSERT INTO volunteers(volunteerUsername, volunteerPassword,
volunteerFirstName, volunteerLastName, volunteerEmail, volunteerChapter, volunteerBirthday,
immigrationStatus, createdBy, createdDate, modifiedBy, modifiedDate) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", (
username, hashed_password, firstname, lastname, email, chapter, birthday, eligibility, username, dt_string, username, dt_string
))
connection.commit()
connection.close()
return jsonify({"nothing" : "no"})
else:
duplicate_user = cursor.execute("""SELECT COUNT(adminUsername) FROM admins WHERE adminUsername = ?""", (username,),).fetchall()
duplicate_user_v = cursor.execute("""SELECT COUNT(volunteerUsername) FROM volunteers WHERE volunteerUsername = ?""", (username,),).fetchall()
duplicate_email_v = cursor.execute("""SELECT COUNT(volunteerEmail) FROM volunteers WHERE volunteerEmail = ?""", (email,),).fetchall()
duplicate_email = cursor.execute("""SELECT COUNT(adminEmail) FROM admins WHERE adminEmail = ?""", (email,),).fetchall()
if duplicate_user == [(1,)] or duplicate_user_v == [(1,)]:
return jsonify({"duplicate" : "yes"})
else:
if duplicate_email == [(1,)] or duplicate_email_v == [(1,)]:
return jsonify({"email_dup": 'yes'})
else:
cursor.execute("""INSERT INTO admins(adminUsername, adminPassword,
adminFirstName, adminLastName, adminEmail, adminChapter, adminStatus, adminBirthday,
immigrationStatus, createdBy, createdDate, modifiedBy, modifiedDate) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", (
username, hashed_password, firstname, lastname, email, chapter, adminStatus, birthday, eligibility, username, dt_string, username, dt_string
))
connection.commit()
connection.close()
return jsonify({"nothing" : "no"})
# this checks credentials of user by hashed salt and then admits them or rejects them based on user not found, invalid credentials, rejected, removed, pending
# this also starts the sessiosn for loggedin, admin/volunteer/superadmin
@application.route('/login', methods=['POST'])
def login():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
password = str(dataGet[1]['password'])
salt = "?Sew@!NtErNaT!0NaLh0urS?"
dataBase_password = password+salt
hashed = hashlib.md5(dataBase_password.encode())
hashed_password = hashed.hexdigest()
volunCount = cursor.execute("SELECT COUNT(volunteerUsername) FROM volunteers WHERE volunteerUsername = ?", (username,),).fetchall()
adminCount = cursor.execute("SELECT COUNT(adminUsername) FROM admins WHERE adminUsername = ?", (username,),).fetchall()
if volunCount == [(1,)]:
volunCount2 = cursor.execute("SELECT COUNT(*) FROM volunteers WHERE volunteerUsername = ? AND volunteerPassword= ?", (username, hashed_password,),).fetchall()
if volunCount2 == [(1,)]:
session["loggedin"] = True
session.pop("admin")
value = {"authenticated" : "volunteer"}
return jsonify(value)
else:
value = {"authenticated" : "No"}
return jsonify(value)
elif adminCount == [(1,)]:
adminCount2 = cursor.execute("SELECT COUNT(*) FROM admins WHERE adminUsername = ? AND adminPassword = ?", (username, hashed_password,),).fetchall()
adminStatus = cursor.execute("SELECT adminStatus FROM admins WHERE adminUsername = ? AND adminPassword = ?", (username, hashed_password,),).fetchall()
if adminCount2 == [(1,)]:
if adminStatus == [("Approved",)]:
session["loggedin"] = True
if username == "abantwal":
session["superadmin"] = True
session.pop("volunteer")
value = {"authenticated" : "admin"}
return jsonify(value)
elif adminStatus == [("Pending",)]:
value = {"authenticated" : "Pending"}
return jsonify(value)
elif adminStatus == [("Removed",)]:
value = {"authenticated" : "Removed"}
return jsonify(value)
elif adminStatus == [("Rejected",)]:
value = {"authenticated" : "Rejected"}
return jsonify(value)
elif adminStatus == [("Permanently Removed",)]:
value = {"authenticated" : "Permanently"}
return jsonify(value)
else:
value = {"authenticated" : "No"}
return jsonify(value)
else:
value = {"authenticated" : "No"}
return jsonify(value)
#this will popualate the text boxes for changing profile admins
@application.route('/populate', methods=['POST'])
def populateProfile():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
adminProfile = cursor.execute("SELECT adminFirstName, adminLastName, adminEmail, adminChapter FROM admins WHERE adminUsername = ?", (username,),).fetchall()
value = {"fname": adminProfile[0][0], "lname": adminProfile[0][1], "email": adminProfile[0][2], "chapter": adminProfile[0][3]}
return (jsonify(value))
#this will popualate the text boxes for changing profile admins
@application.route('/populate2', methods=['POST'])
def populateProfile2():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
volunteerProfile = cursor.execute("SELECT volunteerFirstName, volunteerLastName, volunteerEmail, volunteerChapter, volunteerBirthday, immigrationStatus FROM volunteers WHERE volunteerUsername = ?", (username,),).fetchall()
value = {"fname": volunteerProfile[0][0], "lname": volunteerProfile[0][1], "email": volunteerProfile[0][2], "chapter": volunteerProfile[0][3], "birthday": volunteerProfile[0][4], "immigration": volunteerProfile[0][5]}
return (jsonify(value))
#this updates the database for a volunteer and if same email is owned by same username, it allows, otherwise errors
@application.route('/volunteerProfile', methods=['POST'])
def profile():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
password = str(dataGet[1]['password'])
firstname = dataGet[2]['firstname']
lastname = dataGet[3]['lastname']
email = dataGet[4]['email']
chapter = dataGet[5]['chapter']
birthday = dataGet[6]['birthday']
eligibility = dataGet[7]['eligibility']
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
salt = "?Sew@!NtErNaT!0NaLh0urS?"
dataBase_password = password+salt
hashed = hashlib.md5(dataBase_password.encode())
hashed_password = hashed.hexdigest()
duplicate_email = cursor.execute("""SELECT COUNT(volunteerEmail) FROM volunteers WHERE volunteerEmail = ?""", (email,),).fetchall()
duplicate_email_a = cursor.execute("""SELECT COUNT(adminEmail) FROM admins WHERE adminEmail = ?""", (email,),).fetchall()
data_email = cursor.execute("SELECT volunteerEmail FROM volunteers WHERE volunteerUsername = ?", (username,),).fetchall()
data_email_admin = cursor.execute("SELECT adminEmail FROM admins WHERE adminUsername = ?", (username,),).fetchall()
valid_email = ""
if data_email == []:
valid_email = data_email_admin[0][0]
else:
valid_email = data_email[0][0]
if duplicate_email == [(1,)] or duplicate_email_a == [(1,)]:
if email == valid_email:
cursor.execute("""UPDATE volunteers SET volunteerPassword = ?, volunteerFirstName = ?,
volunteerLastName = ?, volunteerEmail = ?, volunteerChapter = ?, volunteerBirthday = ?,
immigrationStatus = ?, modifiedDate = ? WHERE volunteerUsername = ?""",
(hashed_password, firstname, lastname, email, chapter, birthday, eligibility, dt_string, username))
connection.commit()
connection.close()
return jsonify({"success" : "yes"})
else:
return jsonify({"email_dup" : "yes"})
else:
cursor.execute("""UPDATE volunteers SET volunteerPassword = ?, volunteerFirstName = ?,
volunteerLastName = ?, volunteerEmail = ?, volunteerChapter = ?, volunteerBirthday = ?,
immigrationStatus = ?, modifiedDate = ? WHERE volunteerUsername = ?""",
(hashed_password, firstname, lastname, email, chapter, birthday, eligibility, dt_string, username))
connection.commit()
connection.close()
return jsonify({"success" : "yes"})
# does the same thing as volunteerProfile but for admins
@application.route('/adminProfile', methods=['POST'])
def admprofile():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
password = str(dataGet[1]['password'])
firstname = dataGet[2]['firstname']
lastname = dataGet[3]['lastname']
email = dataGet[4]['email']
chapter = dataGet[5]['chapter']
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
salt = "?Sew@!NtErNaT!0NaLh0urS?"
dataBase_password = password+salt
hashed = hashlib.md5(dataBase_password.encode())
hashed_password = hashed.hexdigest()
duplicate_email_v = cursor.execute("""SELECT COUNT(volunteerEmail) FROM volunteers WHERE volunteerEmail = ?""", (email,),).fetchall()
duplicate_email = cursor.execute("""SELECT COUNT(adminEmail) FROM admins WHERE adminEmail = ?""", (email,),).fetchall()
data_email = cursor.execute("SELECT volunteerEmail FROM volunteers WHERE volunteerUsername = ?", (username,),).fetchall()
data_email_admin = cursor.execute("SELECT adminEmail FROM admins WHERE adminUsername = ?", (username,),).fetchall()
valid_email = ""
if data_email == []:
valid_email = data_email_admin[0][0]
else:
valid_email = data_email[0][0]
if duplicate_email == [(1,)] or duplicate_email_v == [(1,)]:
if email == valid_email:
cursor.execute("""UPDATE admins SET adminPassword = ?, adminFirstName = ?,
adminLastName = ?, adminEmail = ?, adminChapter = ?, modifiedDate = ? WHERE adminUsername = ?""",
(hashed_password, firstname, lastname, email, chapter, dt_string, username))
connection.commit()
connection.close()
return jsonify({"success": 'yes'})
else:
return jsonify({"email_dup" : "yes"})
else:
cursor.execute("""UPDATE admins SET adminPassword = ?, adminFirstName = ?,
adminLastName = ?, adminEmail = ?, adminChapter = ?, modifiedDate = ? WHERE adminUsername = ?""",
(hashed_password, firstname, lastname, email, chapter, dt_string, username))
connection.commit()
connection.close()
return jsonify({"success" : "yes"})
# this will add an event if the event is not already present in database
@application.route('/add', methods=['POST'])
def addevent():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
eventCat = dataGet[1]['eventcat']
event = dataGet[2]['event']
maxhours = dataGet[3]['maxhours']
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
adminChapter = cursor.execute("SELECT adminChapter FROM admins WHERE adminUsername = ?", (username,),).fetchall()
eventCount = cursor.execute("SELECT COUNT(eventName) FROM events WHERE eventName = ? AND eventStatus = 'Active'", (event,),).fetchall()
if eventCount == [(1,)]:
value = {"present": "Yes"}
return jsonify(value)
else:
eventCount = cursor.execute("SELECT COUNT(eventName) FROM events WHERE eventName = ? AND eventStatus = 'Inactive'", (event,),).fetchall()
if eventCount == [(1,)]:
id = cursor.execute("SELECT eventCategoryId FROM eventCategory WHERE eventCategoryName = ?", (eventCat,),).fetchall()
eventCatid = id[0][0]
cursor.execute("""UPDATE events SET eventCategoryId = ?, eventStatus = 'Active', maxAllowedHours = ? WHERE eventName = ?""", (eventCatid, maxhours, event))
connection.commit()
connection.close()
value = {'present': "successful"}
return jsonify(value)
else:
id = cursor.execute("SELECT eventCategoryId FROM eventCategory WHERE eventCategoryName = ?", (eventCat,),).fetchall()
eventCatid = id[0][0]
cursor.execute("""INSERT INTO events(eventCategoryId, eventName, eventStatus, createdBy, createdDate, modifiedBy,
modifiedDate, maxAllowedHours, eventChapter) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)""", (eventCatid, event, "Active", username, dt_string, username, dt_string, maxhours, adminChapter[0][0]))
connection.commit()
connection.close()
value = {'present': "successful"}
return jsonify(value)
# this will add an event category if the event is not already present in database
@application.route('/addcat', methods=['POST'])
def addcategory():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
eventCat = dataGet[1]['eventcat']
maxhours = dataGet[2]['maxhours']
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
adminChapter = cursor.execute("SELECT adminChapter FROM admins WHERE adminUsername = ?", (username,),).fetchall()
catCount = cursor.execute("SELECT COUNT(eventCategoryName) FROM eventCategory WHERE eventCategoryName = ? AND categoryStatus = 'Active'", (eventCat,),).fetchall()
if catCount == [(1,)]:
value = {"present": "Yes"}
return jsonify(value)
else:
catCount = cursor.execute("SELECT COUNT(eventCategoryName) FROM eventCategory WHERE eventCategoryName = ? AND categoryStatus = 'Inactive'", (eventCat,),).fetchall()
if (catCount == [(1,)]):
cursor.execute("""UPDATE eventCategory SET categoryStatus = 'Active', maxAllowedHours = ? WHERE eventCategoryName = ?""", (maxhours, eventCat))
connection.commit()
connection.close()
value = {'present': "successful"}
return jsonify(value)
else:
cursor.execute("""INSERT INTO eventCategory(eventCategoryName, maxAllowedHours, categoryStatus, createdBy, createdDate, modifiedBy,
modifiedDate, eventCategoryChapter) VALUES(?, ?, ?, ?, ?, ?, ?, ?)""", (eventCat, maxhours, "Active", username, dt_string, username, dt_string, adminChapter[0][0]))
connection.commit()
connection.close()
value = {'present': "successful"}
return jsonify(value)
#displays the categories in select box of add event screen
@application.route('/getcats', methods=['POST'])
def getcats():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
adminChapter = cursor.execute("SELECT adminChapter FROM admins WHERE adminUsername = ?", (username,),).fetchall()
eventCat = cursor.execute("SELECT eventCategoryName FROM eventCategory WHERE eventCategoryChapter = ? AND categoryStatus = 'Active'", (adminChapter[0][0],),).fetchall()
information = []
for i in range(0, len(eventCat)):
value = {"name" : eventCat[i][0]}
information.append(value)
return jsonify(information)
# this will display all the hours logged from volunteers in a table
@application.route('/page', methods=['POST'])
def title():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
information = []
adminsinfo = cursor.execute("SELECT adminFirstName, adminChapter FROM admins WHERE adminUsername = ?", (username,),).fetchall()
info = {"firstname": adminsinfo[0][0], "chapter": adminsinfo[0][1]}
information.append(info)
loggedHourinfo = cursor.execute("""SELECT volunteerChapter, loggedHourId,
volunteerFirstName, volunteerLastName, weekStart, weekEnd, eventName, eventCategoryName, submissionDate,
totalHours, submissionStatus, comments FROM loggedHours
JOIN volunteers ON loggedHours.volunteerId = volunteers.volunteerId
JOIN events ON loggedHours.eventNameId = events.eventNameId
JOIN eventCategory ON loggedHours.eventCategoryId = eventCategory.eventCategoryId
WHERE submissionStatus = "Approved" OR submissionStatus = "Rejected" OR submissionStatus = "Unapproved"
ORDER BY submissionStatus DESC, volunteerFirstName ASC, volunteerLastName ASC """,).fetchall()
for i in range(0, len(loggedHourinfo)):
volunteerChapter = loggedHourinfo[i][0]
loggedHourId = loggedHourinfo[i][1]
volunteerFirstName = loggedHourinfo[i][2]
volunteerLastName = loggedHourinfo[i][3]
weekStart = loggedHourinfo[i][4]
weekEnd = loggedHourinfo[i][5]
eventName = loggedHourinfo[i][6]
eventCategoryName = loggedHourinfo[i][7]
submissionDate = loggedHourinfo[i][8]
totalHour = loggedHourinfo[i][9]
submissionStatus = loggedHourinfo[i][10]
comments = loggedHourinfo[i][11]
if volunteerChapter == adminsinfo[0][1]:
name = volunteerFirstName + " " + volunteerLastName
week = weekStart + "-" + weekEnd
info2 = {"id": loggedHourId, "name": name, "week": week, "date": submissionDate, "hours": totalHour, "event": eventName, "eventCat": eventCategoryName, "status": submissionStatus, "comments": comments, "weekStart" : weekStart}
information.append(info2)
return jsonify(information)
# this will populate the certify hour table with all the volunteers and show the unapproved hours yet to be approved before certification
@application.route('/certifyall', methods=['POST'])
def final():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
dataGet = request.get_json()
username = dataGet[0]['username']
information = []
adminChapter = cursor.execute("SELECT adminChapter FROM admins WHERE adminUsername = ?", (username,),).fetchall()
volunteerId = cursor.execute("SELECT volunteerId FROM volunteers").fetchall()
for i in range(0, len(volunteerId)):
loggedHourinfo = cursor.execute("""SELECT volunteerChapter, volunteerFirstName, volunteerLastName, totalHours, submissionStatus, volunteers.volunteerId FROM loggedHours
JOIN volunteers ON loggedHours.volunteerId = volunteers.volunteerId
WHERE volunteerChapter = ? AND (submissionStatus = "Approved" OR submissionStatus = "Unapproved") AND loggedHours.volunteerId = ?
ORDER BY volunteerFirstName ASC, volunteerLastName ASC, volunteerChapter ASC, submissionDate DESC""", (adminChapter[0][0], volunteerId[i][0]),).fetchall()
totalApproved = 0
totalUnapproved = 0
if loggedHourinfo != []:
for i in range(0, len(loggedHourinfo)):
volunteerChapter = loggedHourinfo[i][0]
volunteerFirstName = loggedHourinfo[i][1]
volunteerLastName = loggedHourinfo[i][2]
totalHour = loggedHourinfo[i][3]
submissionStatus = loggedHourinfo[i][4]
name = volunteerFirstName + " " + volunteerLastName
if submissionStatus == "Unapproved":
totalUnapproved += totalHour
elif submissionStatus == "Approved":
totalApproved += totalHour
info2 = {"id": loggedHourinfo[i][5], "name": name, "chapter" : volunteerChapter, "totalhours": totalApproved, "unapproved" : totalUnapproved}
information.append(info2)
return jsonify(information)
#this is for looking at all the approved hours from every chapter
@application.route('/approvereport', methods=['POST'])
def approvalReport():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
information = []
loggedHourinfo = cursor.execute("""SELECT volunteerChapter,
volunteerFirstName, volunteerLastName, weekStart, weekEnd, eventName, eventCategoryName, submissionDate,
totalHours FROM loggedHours
JOIN volunteers ON loggedHours.volunteerId = volunteers.volunteerId
JOIN events ON loggedHours.eventNameId = events.eventNameId
JOIN eventCategory ON loggedHours.eventCategoryId = eventCategory.eventCategoryId
WHERE submissionStatus = "Approved"
ORDER BY volunteerFirstName ASC, volunteerLastName ASC, submissionStatus DESC""",).fetchall()
if loggedHourinfo != []:
for i in range(0, len(loggedHourinfo)):
volunteerChapter = loggedHourinfo[i][0]
volunteerFirstName = loggedHourinfo[i][1]
volunteerLastName = loggedHourinfo[i][2]
weekStart = loggedHourinfo[i][3]
weekEnd = loggedHourinfo[i][4]
eventName = loggedHourinfo[i][5]
eventCategoryName = loggedHourinfo[i][6]
submissionDate = loggedHourinfo[i][7]
totalHour = loggedHourinfo[i][8]
name = volunteerFirstName + " " + volunteerLastName
week = weekStart + "-" + weekEnd
info2 = {"name": name, "eventCat": eventCategoryName, "event" : eventName, "chapter" : volunteerChapter, "week" : week, "date" : submissionDate, "totalhours": totalHour}
information.append(info2)
return jsonify(information)
#this is for looking at all the certified hours from every chapter
@application.route('/certifyreport', methods=['POST'])
def certifiedReport():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
information = []
loggedHourinfo = cursor.execute("""SELECT volunteerChapter,
volunteerFirstName, volunteerLastName, weekStart, weekEnd, eventName, eventCategoryName, submissionDate,
totalHours FROM loggedHours
JOIN volunteers ON loggedHours.volunteerId = volunteers.volunteerId
JOIN events ON loggedHours.eventNameId = events.eventNameId
JOIN eventCategory ON loggedHours.eventCategoryId = eventCategory.eventCategoryId
WHERE submissionStatus = "Certified"
ORDER BY volunteerFirstName ASC, volunteerLastName ASC, submissionStatus DESC""",).fetchall()
if loggedHourinfo != []:
for i in range(0, len(loggedHourinfo)):
volunteerChapter = loggedHourinfo[i][0]
volunteerFirstName = loggedHourinfo[i][1]
volunteerLastName = loggedHourinfo[i][2]
weekStart = loggedHourinfo[i][3]
weekEnd = loggedHourinfo[i][4]
eventName = loggedHourinfo[i][5]
eventCategoryName = loggedHourinfo[i][6]
submissionDate = loggedHourinfo[i][7]
totalHour = loggedHourinfo[i][8]
name = volunteerFirstName + " " + volunteerLastName
week = weekStart + "-" + weekEnd
info2 = {"name": name, "eventCat": eventCategoryName, "event" : eventName, "chapter" : volunteerChapter, "week" : week, "date" : submissionDate, "totalhours": totalHour}
information.append(info2)
return jsonify(information)
#this is for looking at all the rejected hours from every chapter
@application.route('/rejectreport', methods=['POST'])
def rejectedReport():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
information = []
loggedHourinfo = cursor.execute("""SELECT volunteerChapter,
volunteerFirstName, volunteerLastName, weekStart, weekEnd, eventName, eventCategoryName, submissionDate,
totalHours FROM loggedHours
JOIN volunteers ON loggedHours.volunteerId = volunteers.volunteerId
JOIN events ON loggedHours.eventNameId = events.eventNameId
JOIN eventCategory ON loggedHours.eventCategoryId = eventCategory.eventCategoryId
WHERE submissionStatus = "Rejected"
ORDER BY volunteerFirstName ASC, volunteerLastName ASC, submissionStatus DESC""",).fetchall()
if loggedHourinfo != []:
for i in range(0, len(loggedHourinfo)):
volunteerChapter = loggedHourinfo[i][0]
volunteerFirstName = loggedHourinfo[i][1]
volunteerLastName = loggedHourinfo[i][2]
weekStart = loggedHourinfo[i][3]
weekEnd = loggedHourinfo[i][4]
eventName = loggedHourinfo[i][5]
eventCategoryName = loggedHourinfo[i][6]
submissionDate = loggedHourinfo[i][7]
totalHour = loggedHourinfo[i][8]
name = volunteerFirstName + " " + volunteerLastName
week = weekStart + "-" + weekEnd
info2 = {"name": name, "eventCat": eventCategoryName, "event" : eventName, "chapter" : volunteerChapter, "week" : week, "date" : submissionDate, "totalhours": totalHour}
information.append(info2)
return jsonify(information)
#this is for looking at all the unapproved hours from every chapter
@application.route('/unapprovedreport', methods=['POST'])
def unapprovedReport():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
information = []
loggedHourinfo = cursor.execute("""SELECT volunteerChapter,
volunteerFirstName, volunteerLastName, weekStart, weekEnd, eventName, eventCategoryName, submissionDate,
totalHours FROM loggedHours
JOIN volunteers ON loggedHours.volunteerId = volunteers.volunteerId
JOIN events ON loggedHours.eventNameId = events.eventNameId
JOIN eventCategory ON loggedHours.eventCategoryId = eventCategory.eventCategoryId
WHERE submissionStatus = "Unapproved"
ORDER BY volunteerFirstName ASC, volunteerLastName ASC, submissionStatus DESC""",).fetchall()
if loggedHourinfo != []:
for i in range(0, len(loggedHourinfo)):
volunteerChapter = loggedHourinfo[i][0]
volunteerFirstName = loggedHourinfo[i][1]
volunteerLastName = loggedHourinfo[i][2]
weekStart = loggedHourinfo[i][3]
weekEnd = loggedHourinfo[i][4]
eventName = loggedHourinfo[i][5]
eventCategoryName = loggedHourinfo[i][6]
submissionDate = loggedHourinfo[i][7]
totalHour = loggedHourinfo[i][8]
name = volunteerFirstName + " " + volunteerLastName
week = weekStart + "-" + weekEnd
info2 = {"name": name, "eventCat": eventCategoryName, "event" : eventName, "chapter" : volunteerChapter, "week" : week, "date" : submissionDate, "totalhours": totalHour}
information.append(info2)
return jsonify(information)
#this is for looking at approved hours and validating medals
@application.route('/medals', methods=['POST'])
def medalReport():
connection = sqlite3.connect('sewawebapp.db')
cursor = connection.cursor()
if request.method == "POST":
information = []
medal_status = ""
todays_date = date.today()
year = todays_date.year
volunteerId = cursor.execute("SELECT volunteerId FROM volunteers").fetchall()
for i in range(0, len(volunteerId)):
loggedHourinfo = cursor.execute("""SELECT volunteerChapter, loggedHourId,
volunteerFirstName, volunteerLastName, totalHours, submissionStatus, volunteerBirthday, volunteers.volunteerId FROM loggedHours
JOIN volunteers ON loggedHours.volunteerId = volunteers.volunteerId
WHERE (submissionStatus = "Certified" OR submissionStatus = "Unapproved") AND immigrationStatus = "US Citizen or Greencard Holder" AND loggedHours.volunteerId = ?
ORDER BY volunteerFirstName ASC, volunteerLastName ASC, volunteerChapter ASC, submissionDate DESC""", (volunteerId[i][0],),).fetchall()
totalApproved = 0
totalUnapproved = 0
if loggedHourinfo != []:
for i in range(0, len(loggedHourinfo)):
volunteerChapter = loggedHourinfo[i][0]
volunteerFirstName = loggedHourinfo[i][2]
volunteerLastName = loggedHourinfo[i][3]
totalHour = loggedHourinfo[i][4]
submissionStatus = loggedHourinfo[i][5]
name = volunteerFirstName + " " + volunteerLastName
if submissionStatus == "Unapproved":
totalUnapproved += totalHour
elif submissionStatus == "Certified":
totalApproved += totalHour
volunteerBirthday = str(loggedHourinfo[i][6])
tokens = volunteerBirthday.split("-")
age = year - int(tokens[0])
if age >= 5:
if totalApproved >= 26:
medal_status = "Bronze"
elif totalApproved >= 50:
medal_status = "Silver"
elif totalApproved >= 75:
medal_status = "Gold"
else:
medal_status = "N/A"
elif age >= 11:
if totalApproved >= 50:
medal_status = "Bronze"
elif totalApproved >= 74:
medal_status = "Silver"
elif totalApproved >= 100:
medal_status = "Gold"
else:
medal_status = "N/A"
elif age >= 16:
if totalApproved >= 100:
medal_status = "Bronze"
elif totalApproved >= 175:
medal_status = "Silver"
elif totalApproved >= 250:
medal_status = "Gold"
else:
medal_status = "N/A"
elif age >= 26: