-
Notifications
You must be signed in to change notification settings - Fork 0
/
coco_app.py
1110 lines (959 loc) · 43.1 KB
/
coco_app.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
from flask import url_for, request, render_template, redirect, send_from_directory, g, session, abort, flash, jsonify
from functools import wraps
from lib import *
from coco_scheduling import *
import re
from flask.globals import session, request
from flask_mail import Mail, Message
from db_helper import DBAssistant
# from celery import Celery
app.config.from_object(__name__)
mail_ext = Mail(app)
# celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
# celery.conf.update(app.config)
PERIOD = {}
COLORPREFS = {0: "tomato", 1: "", 2: "yellow", 3: "lightgreen", 5: "lightgrey"}
courseComplete = {} # for the current lecturer: dictionary for courses and resp. preferences for each of them
hrs = {} # hours per week for every course of current lecturer (who is logged in now)
markedTimes = {}
allSolutions = []
incr = 0
newPrefs = 0
app.config.from_envvar('COCO_SETTINGS', silent=True)
db_assist = DBAssistant()
# request login
def login_required(f):
"""
A decorator to disallow accessing restricted pages without being authenticated.
:param f: a function to be decorated
:return: a wrapper
"""
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('You need to login first')
return redirect(url_for('log'))
return wrap
# Redirect to index page
@app.route("/")
def home():
"""
Route function for the home page.
:return: redirects to the function that renders the index.
"""
return redirect(url_for("index"))
@app.route("/index")
def index():
"""
Route function for the index, it renders the index view and check if you are logged in already
redirects to the respective users view.
:return:
"""
passwd1 = db_assist.get_single('select password from users where id = 1')[0]
passwd2 = db_assist.get_single('select password from users where id = 2')[0]
if not passwd1[0] == '$':
passwd1 = bcrypt.generate_password_hash(str(passwd1))
db_assist.update('update users set password = ? where id = 1', [passwd1])
if not passwd2[0] == '$':
passwd2 = bcrypt.generate_password_hash(str(passwd2))
db_assist.update('update users set password = ? where id = 2', [passwd2])
if 'logged_in' in session:
if session['type'] == 0:
return redirect(url_for('coordinators'))
elif session['type'] == 1:
return redirect(url_for('lecturers'))
return render_template("index.html")
# Process values from Register page
@app.route("/register", methods=['POST'])
def register():
"""
The registeration function, takes in fields(e.g name, username, password etc)
from the registration view and commits to the database if user does not exist,
else it flashes and error message.
:var: name: name read from the html name field string
:var: username: username read from the html name field string
:var: password: user password from form string
:var: email: user email from form string
:var: types: user type 1 or 0, coordinator-0 lecturer-1
:var: title: user title string
:return:
"""
name = request.form['name'].strip()
username = request.form['username'].strip()
password = request.form['password'].strip()
email = request.form['email'].strip()
types = request.form.get('type')
title = request.form.get('title')
len_email = len(email)
if not name or not email or not username or not password or not types or not title:
flash('Please fill all the fields')
return redirect(url_for('reg'))
if (len_email < 7) or re.match("^.+@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) is None:
flash('Wrong Email')
return redirect(url_for('reg'))
if not title:
title = None
passwd = request.form['password']
# TODO: EMA: Password hashing # aleady done by good samaritan
# passphrase is a column inside passphrase table in db
# passphrase is also a text that a new coordinator needs to register into the system.
passp = db_assist.get_single('select passphrase from passphrase')
passphrase = passp if not passp else passp[0]
v = db_assist.get_single('select * from users where username = ?', [request.form['username']])
val = v if not v else v[0]
if val is not None:
flash('User/Username already exists')
return redirect(url_for('reg'))
if request.form["confirmpass"] != "" and request.form['password'] != "":
if passwd != request.form['confirmpass']:
flash('Password mismatch')
return redirect(url_for('reg'))
# coordinator registration
if request.form['type'] == '0':
try:
if bcrypt.check_password_hash(passphrase, request.form['passphrase']):
db_assist.update(
'insert into users (title, name, username, password, email, type) values (?, ?, ?, ?, ?, ?)',
[title, request.form['name'], request.form['username'],
bcrypt.generate_password_hash(request.form['password']),
request.form['email'], request.form['type']])
flash('Registration successful')
return redirect(url_for('log'))
else:
flash("Wrong passphrase!")
return redirect(url_for('reg'))
except:
flash("Wrong passphrase!")
return redirect(url_for('reg'))
# lecturer registration
if request.form['type'] == '1':
db_assist.update('insert into users (title, name, username, password, email, type) values (?, ?, ?, ?, ?, ?)',
[title, name, username, bcrypt.generate_password_hash(password), email, types])
flash('Registration successful')
return redirect(url_for('log'))
flash('Fields are empty')
return redirect(url_for('reg'))
@app.route("/reg")
def reg():
"""
Renders the registration view when user click on register
:return: render_template()
"""
return render_template("register.html")
@app.route("/log")
def log():
"""
Renders the login view when user click on view
:return: render_template()
"""
return render_template("login.html")
@app.route("/login", methods=['GET', 'POST'])
def login(): # On login function
u = db_assist.get_single('select username from users where username = ?', [request.form['username']])
user_name = u if not u else u[0]
times = db_assist.get_all(
'select timeslot_id, time from timeslots order by timeslot_id asc') # here we initialize the PERIOD dictionary
for time in times:
PERIOD[time[0]] = time[1]
print PERIOD
if user_name is not None:
m = db_assist.get_single('select password from users where username = ?', [request.form['username']])
passwd = m if not m else m[0] # deal with none or value in a tuple
r = db_assist.get_single('select type, id from users where username = ?', [request.form['username']])
g.user_type = r[0] # get user type
session['user_id'] = r[1] # get user ID !! DO NOT REMOVE
username = request.form['username']
error = None
if request.method == 'POST':
try:
test_passwd = bcrypt.check_password_hash(passwd, request.form['password'])
except:
test_passwd = False
if not test_passwd:
error = 'Invalid Password'
else:
session['logged_in'] = True
session['type'] = g.user_type
session['user'] = username
flash('You are logged in')
if g.user_type == 1:
return redirect(url_for('lecturers'))
if g.user_type == 0:
return redirect(url_for('coordinators'))
else:
error = "Wrong User Name!"
return render_template("login.html", error=error)
@app.route("/changepassword")
def changepassword():
return render_template("change_password.html")
@app.route("/passwordchanged", methods=['POST', 'GET'])
def change_password():
oldpassword = request.form['Old password']
newpassword = request.form['New password']
confirmpassword = request.form['Confirm password']
cur = db_assist.get_single('select password from users where username = ?', [session['user']])
if 'user' in session:
if len(newpassword) > 5:
if len(cur) > 0 and bcrypt.check_password_hash(cur['password'], oldpassword):
if newpassword == confirmpassword:
db_assist.update('Update users set password=? where username=?',
[bcrypt.generate_password_hash(request.form['New password'].strip()),
session['user']])
flash("Your password successfully changed!")
return redirect(url_for('log'))
else:
flash("new password and confirm password mismatch")
return render_template("change_password.html")
else:
flash("old password mismatch")
return render_template("change_password.html")
else:
flash("please enter password with at least six characters")
return render_template("change_password.html")
else:
flash("please first login")
return render_template("login.html")
@app.route("/logout")
@login_required
def logout():
session.pop('logged_in', None)
flash('You are logged out')
return redirect(url_for('index'))
@app.context_processor
def utility_processor():
def rowspan(prefs):
"""
This function returns number of keys in dictionary and dynamically passes in to the web page
:param: prefs:dicitonary - dictionary, in which we count keys
:return: :int - amount of keys in the input dictionary
"""
return len(prefs) + 1
return dict(rowspan=rowspan)
@app.route("/pref")
@login_required
def pref():
"""
This function collects the preferences of the lecturers, structure and sends to the coordinator
view responsible for presenting in a given manner.
:param null
:var client - the user type accessing this function.
:var prefs - the prefereces gotten from the database
:var list_pref - the data from preferences we actually need in a list of a tuple structure
:var p - the dictionary of a dictionary of a list of a tuple, containing our data to present.
:return: p
"""
client = "Coordinator"
set_current_semester()
prefs = db_assist.get_all("select p.timeslot_id, courses.course, p.weekday_id, p.preference_id \
from courses, preferences p where courses.id = p.course_id \
and p.semester_id = " + session['current_semester'])
p = Library.form_marks(prefs, COLORPREFS, True, True, False)
return render_template("preferences.html", prefs=p, times=list(PERIOD.values()), client=client)
@app.route("/get-prefs", methods=['POST', 'GET'])
@login_required
def get_prefs():
"""
Gets the preferences and inputs to database.
:param null
:var course - selected course id
:var hrs - selected hours per week
:var courseComplete - dictionary for preferences loaded from DB for current user and current semester
:var preferences - the prefereces from the user input
:var pref_2 - temporary array for updating data in database
:var upd - array with preferences to be updated with the new value
:var rmv - array with preferences to be deleted
:return void
"""
try:
course = request.form["courses"]
hrs_ = request.form["hours"]
if course is None or hrs is None:
raise Exception("Required fields are empty")
except:
return ('', 204)
else:
global newPrefs
g.user_id = str(session['user_id'])
cur_semester = str(session['current_semester'])
preferences = Library.read_timeslot_marks(request, '1', False)
pref_2 = []
if preferences:
# exclude preferences, that already exist in DB
upd = []
rmv = []
pref = []
if courseComplete:
if int(course) in courseComplete:
c = courseComplete[int(course)]
pref = Library.handle_duplicates(preferences, c)
preferences = pref[0]
upd = pref[1]
rmv = pref[2]
if preferences:
list(map(lambda c:
pref_2.append((g.user_id, c[0], c[1], c[2], course, hrs_, cur_semester)), preferences))
if pref_2:
db_assist.change_data("insert into preferences (user_id, weekday_id, timeslot_id, preference_id, course_id, hours, semester_id) \
values (?, ?, ?, ?, ?, ?, ?)", pref_2)
pref_2 = []
if upd:
list(map(lambda c:
pref_2.append((c[2], hrs_, course, c[0], c[1], cur_semester)),
upd)) # pref_id, course, weekday, timeslot
if pref_2:
db_assist.change_data("update preferences set preference_id = ?, hours = ? \
where course_id = ? and weekday_id = ? and timeslot_id = ? and semester_id = ?", pref_2)
pref_2 = []
if hrs:
if int(course) in hrs:
if hrs[int(course)] != hrs_:
pref_2.append((hrs_, course, cur_semester))
db_assist.change_data("update preferences set hours = ? where \
course_id = ? and semester_id = ?", pref_2)
pref_2 = []
if rmv:
list(map(lambda c:
pref_2.append((course, c[0], c[1], cur_semester)),
rmv)) # course, weekday, timeslot
if pref_2:
db_assist.change_data("delete from preferences \
where course_id = ? and weekday_id = ? and timeslot_id = ? and semester_id = ?", pref_2)
flash("Preferences saved", category='success')
newPrefs = 1
return redirect(url_for('lecturers'))
else:
# if int(course) not in courseComplete: # if the preferences were not saved yet
return ('', 204)
@app.route("/add-pref", defaults={'courses': None}, methods=['POST', 'GET'])
@app.route("/add-pref/<courses>", methods=['POST', 'GET'])
@login_required
def add_pref(courses):
# TODO: refactor!
"""
Shows interface for lecturer. If current user already saved preferences in current semester, load them.
Otherwise open intermediate window to set preferences.
:param courses - list of courses, for which preferences are set by current user for current semester
:var client - the user type accessing this function.
:var courses_list - list of courses, for which preferences are not set or set by current user
:var semester_set - indicates if the current semester is set
:var times - list of timeslots, available to set the preferences
:var hrs - hours per week for the first course with saved preferences
:var current - the first course (name) with saved preferences
:var prefs - preferences (dictionary for: day of week -> timeslot -> value) for the first course with saved preferences
:return client, times, courses_list, current, prefs, hrs, semester_set
"""
add = True
courses_list = db_assist.get_all("select id, course from courses where id not in (select course_id from preferences \
where user_id <> " + str(session['user_id']) + " and semester_id = " + str(
session['current_semester']) + ") order by id desc")
busy_ = db_assist.get_all("select b.weekday_id, b.timeslot_id, 1, r.comment from busy_timeslots b, reasons r\
where b.semester_id = " + str(session['current_semester']) + " and b.reason_id = r.reason_id")
# if there are several reasons for one timeslot, concatenate reasons
busy_times = Library.form_marks(busy_, None, True, False, False) # add here the reason text, call with parameter
client = "Lecturer"
# add = True
temp = -1
temp2 = []
temp_hrs = ''
if courses: # show preferences from DB
p = {}
for crs in courses:
prefs = db_assist.get_all("select weekday_id, timeslot_id, preference_id, hours from preferences \
where course_id = " + str(crs[0]) + " and semester_id = " + str(session['current_semester']) +
" and user_id =" + str(session['user_id']))
p = Library.form_marks(prefs, COLORPREFS, False, False, False)
hrs[crs[0]] = prefs[0][3]
courseComplete[crs[0]] = p
try:
selectedCourse = request.get_json()
except:
selectedCourse = None
else:
if selectedCourse is not None:
temp = int(selectedCourse)
if temp in courseComplete:
temp2 = courseComplete[temp]
else:
temp2 = []
if temp in hrs:
temp_hrs = hrs[temp]
else:
if courses:
temp = courses[0][0] # first course
temp2 = courseComplete[temp] # [0][0][0] = value [0][0][1] = color
temp_hrs = hrs[temp]
flash("Your current preferences are shown below")
return render_template("lecturers.html", client=client, add=add, times=list(PERIOD.values()),
courses=courses_list, current=temp, prefs=temp2, hrs=temp_hrs,
semester_set=True, busy_times=busy_times)
@app.route("/lecturers", methods=['POST', 'GET'])
@login_required
def lecturers():
"""
Checks if there exist preferences for current user for current semester.
If preferences exist, load the list of courses. Redirects to page for setting/changing preferences
:param null
:var client - the user type accessing this function.
:var m - current semester id
:var courses - list of courses, for which current user already set preferences for current semester
:var semester_set - indicates if the current semester is set
:return courses, semester_set
"""
client = 'Lecturer'
m = db_assist.get_single("select id from sessions where current = 1")
if m is None:
flash("The current semester is not set yet. Your preferences cannot be saved")
return render_template("lecturers.html", client=client, semester_set=False)
session['current_semester'] = m if not m else m[0]
courses = db_assist.get_all("select distinct course_id from preferences where user_id=? and semester_id=?",
[str(session['user_id']), str(session['current_semester'])])
if not courses:
return render_template("lecturers.html", client=client, semester_set=True)
else:
return add_pref(courses)
@app.route("/coordinators", methods=['GET', 'POST'])
@login_required
def coordinators():
"""
This function opens coordinator view to select semester.
:return redirect to coordinator menu for the chosen semester
"""
session['home'] = 1
client = 'Coordinator'
semesters = db_assist.get_all(
'select s.id, st.semester, s.years from sessions s, semester_types st where s.semester = st.id order by s.id desc')
return render_template("coordinator.html", client=client, semesters=semesters)
@app.route("/coordinator_menu", methods=['GET', 'POST'])
@login_required
def coordinator_menu():
"""
This function routes to the coordinator menu page.
:param null
:var session - the session instance
:var courses - the list of courses gotten from the database
:var lecturers - the list of lecturers gotten from the database
:return: redirect to coordinator menu page
"""
client = 'Coordinator'
session['home'] = 0
set_current_semester()
courses = db_assist.get_all('select course, field, students_number from courses order by id desc')
lecturers = db_assist.get_all('select name from users where type = 1 order by id desc')
return render_template('coordinator_menu.html', client=client, courses=courses, lecturers=lecturers)
@app.route("/mark_timeslots", methods=['POST', 'GET'])
@login_required
def mark_timeslots():
"""
Renders the view for marking timeslots as busy.
:var: client: client type for the given permission to access this page.
:return: render_template()
"""
client = "Coordinator"
set_current_semester()
reasons = db_assist.get_all("select reason_id, comment from reasons")
used_reas = db_assist.get_all(
"select distinct reason_id from busy_timeslots where semester_id = " + session['current_semester'])
for r in used_reas:
marks = db_assist.get_all("select weekday_id, timeslot_id, 5, reasons.comment from busy_timeslots, reasons \
where reasons.reason_id = busy_timeslots.reason_id and \
semester_id = " + session['current_semester'] + " and busy_timeslots.reason_id = " + str(r[0]))
m = Library.form_marks(marks, COLORPREFS, True, False, False)
markedTimes[r[0]] = m
other_reason = False
try:
selectedReason = request.get_json()
except:
selectedReason = None
else:
if selectedReason is not None:
other_reason = int(selectedReason) == 0
temp = int(selectedReason)
if temp in markedTimes:
temp2 = markedTimes[temp]
else:
temp2 = []
else:
if used_reas:
temp = used_reas[0][0]
temp2 = markedTimes[temp]
else:
temp = ""
temp2 = []
return render_template("mark_timeslots.html", times=list(PERIOD.values()), client=client, reasons=reasons,
prefs=temp2, current=temp, other_reason=other_reason)
@app.route("/save_marks", methods=['POST', 'GET'])
@login_required
def save_marks():
"""
This function reads and saves marked timeslots.
:return on success - redirects to the coordinator menu for the current semester;
else - keeps the user on th current page
"""
global allSolutions
try:
list_reason = request.form["reasons"]
new_reason = request.form["other_reason"]
if list_reason is None or (list_reason == '0' and new_reason == ""):
raise Exception("Required fields are empty")
except:
return ('', 204) # error message is shown by web page
else:
marks = Library.read_timeslot_marks(request, '0', False)
rid = list_reason
if list_reason == '0':
db_assist.update("insert into reasons (comment) values (?)", [new_reason])
rid_ = db_assist.get_single("select reason_id from reasons where comment = \"" + new_reason + "\"")
rid = rid_[0]
# Handle duplicates
markr = []
new = []
rmv = []
if markedTimes:
if int(rid) in markedTimes:
marks_new = Library.handle_duplicates(marks, markedTimes[int(rid)])
markn = marks_new[0]
markr = marks_new[2]
else:
markn = marks
else:
markn = marks
list(map(lambda c:
new.append((c[0], c[1], rid, str(session['current_semester']))), markn))
list(map(lambda c:
rmv.append((c[0], c[1], rid, str(session['current_semester']))), markr))
if new:
db_assist.change_data("insert into busy_timeslots (weekday_id, timeslot_id, reason_id, semester_id) \
values (?, ?, ?, ?)", new)
if rmv:
db_assist.change_data("delete from busy_timeslots where weekday_id = ? \
and timeslot_id = ? and reason_id = ? and semester_id =?", rmv)
flash("Marks for timeslots were successfully changed", category='success')
allSolutions = []
return redirect(url_for('coordinator_menu', **request.args))
@app.route("/semester")
@login_required
def semester():
"""
Renders the semester selection view.
:var: client: client type for the given permission to access this page.
:return: render_template()
"""
client = "Coordinator"
return render_template("new_semester.html", client=client)
@app.route("/passphrase")
@login_required
def update_passphrase():
"""
Renders the update pass phrase view if the passp value is set to true.
:var: client: client type for the given permission to access this page.
:var: passp: boolean
:return: render_template()
"""
client = "Coordinator"
passp = True
return render_template("coordinator_menu.html", passp=passp, client=client)
@app.route("/passphrase", methods=['POST'])
@login_required
def passphrase():
"""
Updates the new pass phrase to register as a coordinator if not None in the database.
:var: error: error message if pass phrase is empty
:return: render_template()
:return: redirect()
"""
error = ""
if request.form['passphrase'] is not None:
db_assist.update('update passphrase set passphrase = ? where id = 1',
[bcrypt.generate_password_hash(request.form['passphrase'])])
flash('Passphrase updated!')
else:
error = "Passphrase update failed, no value provided"
return render_template('coordinator_menu.html', error=error)
return redirect(url_for('coordinator_menu', **request.args))
@app.route("/add-semester", methods=['POST'])
@login_required
def new_semester():
"""
This function stores a new semester in the database.
:param null
:var val - the possible instance of semester from the database
:return: redirect to coordinators page
"""
val = db_assist.get_all(
"select * from sessions where semester = '" + request.form['semester'] + "' and years = '" + request.form[
'year'] + "'")
if val.__len__() == 0:
if request.form['semester'] and request.form['year']:
db_assist.change_data(
'update sessions set current = 0') # ensure there will be only one current semester, the last added one
semester_type = db_assist.get_single(
"select id from semester_types where semester = '" + request.form['semester'] + "'")
db_assist.change_data('insert into sessions (semester, years, current) values (?, ?, 1)',
[semester_type[0], request.form['year']])
flash("New semester added")
else:
flash("Semester already exists")
return redirect('coordinators')
@app.route("/add-course")
@login_required
def add_course():
"""
Renders the add course view, if add is true, disable other views.
:var: add: boolean
:return: render_template()
"""
client = "Coordinator"
add = True
courses = db_assist.get_all('select id, course, field, students_number from courses order by id desc')
return render_template("coordinator_menu.html", add=add, courses=courses, client=client)
@app.route("/course", methods=['POST'])
@login_required
def course():
"""
This function stores a new course in the database.
:param null
:var db - the database instance
:var val - the list of courses gotten from the database
:return: redirect to coordinator menu page
"""
val = db_assist.get_all('select * from courses where course = ? and field = ?',
[request.form['course'], request.form['field']])
if val.__len__() == 0:
db_assist.update('insert into courses (course, field, students_number) values (?, ?, ?)',
[request.form['course'], request.form['field'], request.form['estimate']])
flash("New Course added")
else:
flash("Course already exist")
return redirect(url_for('coordinator_menu', **request.args))
@app.route("/edit-course", methods=['GET', 'POST'])
@login_required
def edit_course():
add = True
course_id = request.form.get('course')
courses = db_assist.get_all('select id, course, field, students_number from courses order by id desc')
course_edit = db_assist.get_single('select id, course, field, students_number from courses where id =?',
[course_id])
return render_template('coordinator_menu.html', add=add, courses=courses, course_edit=course_edit)
@app.route("/update-course", methods=['GET', 'POST'])
@login_required
def update_course():
global allSolutions
course = request.form['course']
field = request.form['field']
estimate = int(request.form['estimate'])
course_id = int(request.form['id'])
error = ""
try:
db_assist.update('update courses set course=?, field=?, students_number=? where id=?', [course, field, estimate,
course_id])
flash("Success updating course")
except Exception as e:
error = "Error updating course!"
return render_template('coordinator_menu.html', error=error)
allSolutions = []
return redirect(url_for('coordinator_menu', **request.args))
@app.route("/reset")
# @app.route("/reset/<string:link>")
def reset():
"""
Renders the reset view, based on reset value disable other views when user click on Forgot password
:var: reset - boolean
:return: render_template()
"""
reset = True
temp = request.args.get('reset')
if temp is not None:
cur = db_assist.get_single("select id from users where reset='"+temp+"'")
if cur:
return render_template("login.html", reset=reset, link=False)
else:
flash("Seems like you used invalid link. Try to reset password one more time")
return render_template("login.html", reset=False, link=True)
return render_template("login.html", reset=reset, link=True)
@app.route("/reset-password", methods=['POST'])
def reset_password():
"""
This function manages the password recovery feature of an account holder.
:param null
:var user_email - the email addresses gotten from the database
:var error - non existent user message
:return: redirect to login page
"""
email = request.form['email']
e = db_assist.get_single('select email,id from users where email = ?', [email])
user_email = e if not e else e[0]
if user_email is not None:
error = ""
host = request.url_root
one_time = Library.generate_one_time_password()
#add the pass into db
usrid = e[1]
db_assist.update("update users set reset = ? where id = ?",[one_time, usrid])
subject = "Password Recovery"
sender_row = db_assist.get_single("select email from users where id = 1")
mail = Message(subject=subject, sender=sender_row[0], recipients=[user_email])
mail.body = "Below is a link to reset your password \n\n " \
+ host + "reset?reset=" + one_time + " \n\nClick or copy and paste this link in your browser" \
"\n\nThis email has been sent automatically, please don't reply."
try:
with app.app_context():
mail_ext.send(mail)
flash("An email was sent to your mailbox, visit to reset your password")
except Exception as e:
print(e)
flash("Something went wrong with sending the mail", category='error')
else:
error = "User does not exist!"
return render_template("login.html", error=error)
@app.route("/resetting", methods=['POST', 'GET'])
def resetting():
passwd = request.form['password']
confirm_passwd = request.form['confirm_password']
if passwd is not None and confirm_passwd is not None:
if passwd == confirm_passwd:
temp = request.args.get('reset')
if temp:
cur = db_assist.get_single("select id from users where reset='"+temp+"'")
usrid = cur if not cur else cur[0]
if usrid:
db_assist.update("update users set password = ?, reset='' where id = ?",
[bcrypt.generate_password_hash(passwd), usrid])
flash("Your password is successfully changed. Log in with a new one")
else:
flash("The link you are using is not valid")
else:
flash("Passwords do not match")
return render_template("login.html", reset=True, link=False)
return redirect(url_for('log'))
@app.route("/save_schedule", methods=['POST', 'GET'])
@login_required
def save_schedule():
try:
solution_num = request.form['solution_number']
if 'current_semester' not in session:
session['current_semester'] = request.args.get("semester")
if 'current_semester' in session:
if session['current_semester'] != request.args.get("semester") and request.args.get("semester") is not None:
session['current_semester'] = request.args.get("semester")
except Exception as e:
solution_num = 0
else:
if len(allSolutions) > int(solution_num) and allSolutions:
solution = allSolutions[int(solution_num)]
solution = Library.transform(solution, int(session['current_semester']))
db_assist.change_data("delete from schedule where semester_id = ?", list(session['current_semester']))
db_assist.change_data("insert into schedule values (?,?,?,?,?)", solution)
flash(
"The schedule for the current semester has been saved. You can edit it on the page 'Current schedule'")
return redirect(url_for('coordinator_menu', **request.args))
@app.route("/current_schedule", defaults={'export': False}, methods=['POST', 'GET'])
@app.route("/current_schedule/<export>", methods=['POST', 'GET'])
@login_required
def current_schedule(export):
client = "Coordinator"
set_current_semester()
schedule = db_assist.get_all("select s.weekday_id, s.timeslot_id, 5, c.course, r.name, r.location \
from schedule s, courses c, rooms r \
where s.course_id = c.id and s.room_id = r.room_id and s.semester_id = " + session[
'current_semester'])
p = Library.form_marks(schedule, COLORPREFS, True, False, True)
return render_template("current_schedule.html", client=client, times=list(PERIOD.values()), prefs=p, export=export)
@app.route("/save_changed_schedule", methods=['POST', 'GET'])
@login_required
def save_changed_schedule():
"""
This function reads and saves changes in the current schedule.
:return on success - redirects to the coordinator menu for the current semester;
else - keeps the user on th current page
"""
try:
marks = Library.read_timeslot_marks(request, '0', True)
set_current_semester()
solution = []
list(map(lambda c:
solution.append((c[0], c[1], c[2], c[3], str(session['current_semester']))), marks))
db_assist.change_data("delete from schedule where semester_id = ?", list(session['current_semester']))
db_assist.change_data("insert into schedule values (?,?,?,?,?)", solution)
flash("The schedule was successfully changed", category='success')
return redirect(url_for('current_schedule', **request.args))
except:
flash("Failed to save the schedule", category='error')
return redirect(url_for('current_schedule', **request.args))
def set_current_semester():
if 'current_semester' not in session:
session['current_semester'] = request.args.get("semester")
if 'current_semester' in session:
if session['current_semester'] != request.args.get("semester") and request.args.get("semester") is not None:
session['current_semester'] = request.args.get("semester")
@app.route('/export_schedule', methods=['POST', 'GET'])
def export_schedule():
set_current_semester()
success = Library.create_pdf(current_schedule(True)) # apply_async
if success:
flash("The schedule has been successfully exported to PDF file")
return redirect(url_for('coordinator_menu', **request.args))
return ('', 204)
@app.route("/send_schedule", methods=['POST', 'GET'])
@login_required
# @celery.task()
def send_schedule():
m = db_assist.get_single("select st.semester, s.years from sessions s, semester_types st \
where s.semester = st.id and s.id = " + session['current_semester'])
# cur = m if not m else m[0]
subject = "The schedule for " + m[0] + " " + m[1]
emails = db_assist.get_all("select distinct u.email from users u, preferences p \
where p.semester_id = " + session['current_semester'] + " and u.id = p.user_id") # "receiver@mail.com"
receivers = Library.rows_to_array(emails)
sender_row = db_assist.get_single("select email from users where id = " + str(session['user_id']))
mail = Message(subject=subject, sender=sender_row[0], recipients=receivers)
mail.body = "The schedule for the upcoming semester is attached.\
\n\nThis email has been sent automatically, please don't reply."
pdf = Library.create_attachment(current_schedule(True))
mail.attach("schedule.pdf", "application/pdf", pdf.getvalue())
try:
with app.app_context():
mail_ext.send(mail)
flash("The email with schedule for the upcoming semester has been sent")
except Exception as e:
flash("Something went wrong with sending the mail", category='error')
return redirect(url_for('coordinator_menu', **request.args))
@app.route("/schedules", methods=['POST', 'GET'])
@login_required
def schedules():
"""
This function returns the possible schedules suggested to the coordinator based on the preferences
entered bu the lecturers.
:param null
:var prefs - the prefereces gotten from the database
:var list_pref - the data from preferences we actually need in a list of a tuple structure
:var p - the dictionary of a dictionary of a list of a tuple, containning our data to present.
:var: solution - the possible schedules from the constraint solver
:var: courses - the list of courses whose schedules have been set
:return: p
"""
real_solution = []
final_sol = {}
display_buttons = False
global incr
global allSolutions
global newPrefs
client = "Coordinator"
var = 0
if request.method == 'POST' and incr > 0:
try:
request.form["newsolution"]
increment = 1
except:
increment = -1
var = len(allSolutions) - (incr - increment)
incr -= increment
if var < 0 or var >= len(allSolutions):
sol = allSolutions[0]
incr = len(allSolutions)
else:
sol = allSolutions[var]
else:
import itertools
set_current_semester()
prefs = db_assist.get_all('select courses.course, weekdays.name, timeslots.time, preferences.preference_id, \
courses.students_number from courses, weekdays, timeslots, preferences where preferences.semester_id = ' +
session['current_semester'] + ' and \
courses.id = preferences.course_id and timeslots.timeslot_id = preferences.timeslot_id and \
weekdays.weekday_id = preferences.weekday_id')
# prefs = cur.fetchall()
list_pref = [(x[0], x[1], x[2], x[3], str(x[4])) for x in prefs]
p = Library.struct_pref(list_pref, COLORPREFS)
# Rooms and capacity
rooms = db_assist.get_all('select name, location, max_capability from rooms')
pp = Library.struct_constraint(p, rooms)