-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
641 lines (488 loc) · 22.3 KB
/
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
from typing import List
from datetime import datetime, timedelta
from enum import unique
from urllib.request import Request
from flask import Flask, render_template, url_for, flash, redirect, request, session
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from markupsafe import Markup
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///assignment3.db'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes = 60)
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
#------------------------------------ Models ------------------------------------#
class User(db.Model): #Creation/Query works.
__tablename__ = 'User'
uid = db.Column(db.Integer, primary_key = True, unique = True, nullable = False) #Utorid equilvant [For login]
password = db.Column(db.String(20), nullable = False) #[for login]
first_name = db.Column(db.String(20), nullable = False)
last_name = db.Column(db.String(20), nullable = False)
email = db.Column(db.String(100), unique=True, nullable=False)
account_type = db.Column(db.String(20), nullable = False) #student,teacher
def __init__(self, uid, password, first_name,last_name, email, account_type):
self.uid = uid
self.password = password
self.first_name = first_name
self.last_name = last_name
self.email = email
self.account_type = account_type
def get_user_info(self):#We dont wanna return password hash
return {"uid": self.uid, "first_name": self.first_name, "last_name": self.last_name, "email": self.email, "account_type": self.account_type, "password": self.password}
def get_user_preview(self):
return {"uid": self.uid, "first_name": self.first_name, "last_name": self.last_name}
def get_teacher_details(self):
return self.first_name + ' ' + self.last_name
def __repr__(self):
return f"User('{self.uid}', '{self.first_name}', '{self.last_name}', '{self.account_type}')"
class StudentMarks(db.Model): #Creation/Query not tested but should work.
__tablename__ = 'StudentMarks'
#Student UserID
uid = db.Column(db.Integer, db.ForeignKey('User.uid'),primary_key = True, nullable = False)
#Assignment Marks
A1 = db.Column(db.Integer,nullable = True)
A2 = db.Column(db.Integer,nullable = True)
A3 = db.Column(db.Integer,nullable = True)
#Lab Marks
Lab1 = db.Column(db.Integer,nullable = True)
Lab2 = db.Column(db.Integer,nullable = True)
Lab3 = db.Column(db.Integer,nullable = True)
#Exan Marks
Midterm = db.Column(db.Integer,nullable = True)
Final = db.Column(db.Integer,nullable = True)
def __init__(self, uid, Assignment1, Assignment2, Assignment3, Lab1, Lab2, Lab3, Midterm, Final):
self.uid = uid #int
self.A1 = Assignment1
self.A2 = Assignment2
self.A3 = Assignment3
self.Lab1 = Lab1
self.Lab2 = Lab2
self.Lab3 = Lab3
self.Midterm = Midterm
self.Final = Final
def get_mark_info(self):
return {"uid": self.uid, "A1": self.A1, "A2": self.A2, "A3":self.A3, "Lab1": self.Lab1, "Lab2": self.Lab2, "Lab3": self.Lab3, "Midterm":self.Midterm, "Final": self.Final}
def __repr__(self):
return f"StudentMarks('{self.uid}')"
class RegradeRequests(db.Model): #Creation/Query works. RegradeRequests.uid
__tablename__ = 'RegradeRequests'
req_id = db.Column(db.Integer, primary_key=True)
uid = db.Column(db.Integer, nullable = False, unique = False)
assessmentRequestType = db.Column(db.String(300), nullable=False, unique = False) #What assignment do they want a remark on.
comment = db.Column(db.String(300), nullable=False, unique = False)
def __init__(self, uid, assessmentRequestType, comment):
self.uid = uid
self.assessmentRequestType = assessmentRequestType
self.comment = comment
def getRegradeRequestInfo(self):
return {"uid": self.uid, "assessmentRequestType": self.assessmentRequestType, "comment": self.comment}
def __repr__(self):
return f"RegradeRequests('{self.uid}', '{self.assessmentRequestType}', '{self.comment}')"
class FeedBack(db.Model): #Creation/Query works. RegradeRequests.uid
__tablename__ = 'FeedBack'
feedbackid = db.Column(db.Integer, primary_key=True) #Anon
name = db.Column(db.String(200), nullable = False)
Q1feedback = db.Column(db.String(300), nullable=False, unique = False)
Q2feedback = db.Column(db.String(300), nullable=False, unique = False)
Q3feedback = db.Column(db.String(300), nullable=False, unique = False)
Q4feedback = db.Column(db.String(300), nullable=False, unique = False)
def __init__(self, Q1feedback, Q2feedback, Q3feedback, Q4feedback,uid):
self.Q1feedback = Q1feedback
self.Q2feedback = Q2feedback
self.Q3feedback = Q3feedback
self.Q4feedback = Q4feedback
self.name = uid
def getFeedBackInfo(self):
return {"Q1feedback": self.Q1feedback, "Q2feedback": self.Q2feedback, "Q3feedback": self.Q3feedback, "Q4feedback": self.Q4feedback, "name": self.name}
def __repr__(self):
return f"FeedBack('{self.Q1feedback}', '{self.Q2feedback}','{self.Q3feedback}','{self.Q4feedback}','{self.name}')"
#------------------------------------ Routes ------------------------------------#
#------------------------------------ DEBUG CODE---------------------------------#
#Sample code to help assist with development
#To see how it all works
#username = '205020521'
# password = 'ohmama'
@app.route("/debug/add")
def add_user_debug():
#Test code on how adding user to a database works
# username = '205020521'
# password = 'ohmama'
# hashed_password = bcrypt.generate_password_hash(password).decode('utf8')
# firstname = 'ryangod'
# lastname = 'jgod'
# email = "name3@google.com"
# account_type = 'admin'
# me = User(username,hashed_password, firstname, lastname, email, account_type) #We first have to create an object of the User, and fill it up
# db.session.add(me) #U have to save it in a database session log, [different from user auth sessions]
# db.session.commit() #Then u commit to have the changes in db.dession applied to commit
usernamee = '1234'
passwordd = 'admin1'
hashed_passwordd = bcrypt.generate_password_hash(passwordd).decode('utf8')
firstnamee = 'first' #Valid name
lastnamee = 'name'
emaill = "admin1@gmail.com"
account_typee = 'admin'
mee = User(usernamee,hashed_passwordd,firstnamee,lastnamee,emaill,account_typee)
db.session.add(mee)
db.session.commit()
return "added successfully"
@app.route("/debug/add1")
def add_user_debug1():
usernamee = '11111'
passwordd = 'admin2'
hashed_passwordd = bcrypt.generate_password_hash(passwordd).decode('utf8')
firstnamee = 'firstadmin'
lastnamee = 'lastadmin'
emaill = "admin2@gmail.com"
account_typee = 'admin'
mee = User(usernamee,hashed_passwordd,firstnamee,lastnamee,emaill,account_typee)
db.session.add(mee)
db.session.commit()
return "added successfully"
@app.route("/debug/readUsers")
def readUsers_debug():
#Test code on dumping everything in database.
result = User.query.all() #Queries all users
user_dict = []
for user in result: #For each user object in db
account = user.get_user_info() #call helper function in class
user_dict.append(account) #add that to user dict
return str(user_dict)
@app.route("/debug/readMarks")
def readMarks_debug():
#Test code on dumping everything in database.
result = StudentMarks.query.all() #Queries all users
user_dict = []
for user in result: #For each user object in db
account = user.get_mark_info() #call helper function in class
user_dict.append(account) #add that to user dict
return str(user_dict)
@app.route("/debug/readUsersSpecificByFilter")
def readUsersQuery_debug():
#If we want a column to have a specific value. #We need the .all() o/w it just retuns an sql query in results
result = User.query.filter_by(uid = '123234', account_type='admin').all()
return str(result)
@app.route("/debug/readUsersSpecificByFilterAndGetValue")
def readUsersQueryBySpecificValue_debug():
#If we want a column to have a specific value. #We need the .all() o/w it just retuns an sql query in results
#Result is an array of user objects that matches the follow query
result = User.query.filter_by(uid = '123234', account_type='admin').all()
#get the first element in results
#Result[0] is a User type object.
#Call get_user_info in the class to extract all information about user object
#it returns a hash map where we can just get the exact information we want.
name = result[0].get_user_info()
return str(name)
@app.route("/whoami")
def whosLoggedIn():
if 'name' in session:
return session['name']
return "Nobody is logged in"
#--------------------------------------Debug Code END ---------------------------------#
#Main Route Code
#Rules
#Do not do any calculation or manipulation of data in the routes function
# The route function must only accept return values from functions
# Or call other functions with params.
# The route should take input from form/post/get requests, sends it to the function to do work
# and return the results as a html_render.
@app.route("/login", methods = ['GET','POST'])
def login():
#If its a POST request it means the user entered detail on the login form and entered
#If its a get request it means that the user vistited the login page.
#added this
if 'name' in session:
return redirect(url_for('home'))
##
result = ''
if request.method == 'POST':
username = request.form.get('uid')
password = request.form.get('password')
result = check_login(username, password)
if(result):
return redirect(url_for('home'))
else:
result = 'Incorrect credentials. Try again.'
return render_template('login.html', msg = result)
@app.route("/logout")
def logout():
session.pop('name', default = None)
return redirect(url_for('login'))
@app.route("/")
def home():
if 'name' not in session:
return redirect(url_for('login'))
return render_template('home.html', pagename = "B20 Home")
@app.route("/register", methods = ['GET','POST'])
def register():
if 'name' in session:
return redirect(url_for('home'))
msg_holder = ''
if request.method == 'POST': #Check if its post, o/w default to get
username = request.form.get('userid') #UID
password = request.form.get('password')
first_name = request.form.get('firstname')
last_name = request.form.get('lastname')
email = request.form.get('email')
nonempty_fields = has_empty_input(username, password, first_name, last_name, email)
uid_exists = user_id_exists(username) #If true then uid doesn't exist [good]
email_exists = email_taken(email)
if(nonempty_fields): #hows it looking W
msg_holder = 'Make sure the fields are not empty'
print("Make sure the fields are not empty")
elif(not(validID(username))):
msg_holder = 'Your Student ID must be digits only'
elif(uid_exists):
msg_holder = 'User ID already exists'
print("UserID already exists")
elif(email_exists):
msg_holder = 'Email is already taken.'
print("Email already exists")
elif '@' not in email:
msg_holder = 'Please provide a valid email'
print("Please provide a valid email")
else:#Checks are good, create account and redirect to homepage.
create_user(username, password, first_name, last_name, email)
check_login(username, password) #Correct password by precondition
return redirect(url_for('home'))
return render_template('register.html', msg = msg_holder)
@app.route("/home")
def homepage():
if 'name' not in session:
return redirect(url_for('login'))
else:
return render_template('home.html')
@app.route("/syllabus")
def syllabus():
if 'name' not in session:
return redirect(url_for('login'))
else:
return render_template('syllabus.html')
@app.route("/assignments")
def assignments():
if 'name' not in session:
return redirect(url_for('login'))
else:
return render_template('assignments.html')
@app.route("/labs")
def labs():
if 'name' not in session:
return redirect(url_for('login'))
else:
return render_template('labs.html')
@app.route("/grades", methods = ['GET','POST'])
def mark_page():
if 'name' not in session:
return redirect(url_for('login'))
user_role = session['name']['account_type']
if(user_role == 'student'):
print(session['name']['uid'])
result = getMarkList(session['name']['uid'])
result.pop('uid')
return render_template('studentmarkview.html', marks = result)
user = readUsers() #gives us a set of all users
regrade = readRegrade()
return render_template('teachersearch.html', user_list = user, regrade_req = regrade)
@app.route("/grades/<id>", methods = ['GET','POST'])
def mark_page_generic(id):
if 'name' not in session:
return redirect(url_for('login'))
user_role = session['name']['account_type']
if(user_role == 'student'):
return redirect(url_for('mark_page'))
sanitized_id = 0
try:
sanitized_id = int(id)
except:
return redirect(url_for('mark_page'))
#Check if its a valid id.
exist = user_id_exists(sanitized_id)
if(not(exist)):
return redirect(url_for('mark_page'))
result = getMarkList(id)
result.pop('uid')
if request.method == 'POST': #Check if its post, o/w default to get
assessment = request.form.get('assign-name') #UID
new_mark = request.form.get('new-mark')
status = updateMark(assessment, new_mark, sanitized_id)
if(status == True):
flash('Mark has been updated sucessfully.')
result = getMarkList(id)
result.pop('uid')
return render_template('teachermarkview.html', marks = result, id = id)
else:
flash('Invalid Assignment Name and/or Mark Entered')
return render_template('teachermarkview.html', marks = result, id = id)
return render_template('teachermarkview.html', marks = result, id = id)
def updateMark(assignment:str, newMark:int, student_id:int)->bool:
#Get the id, of the student
#Get the account detail
try:
account = StudentMarks.query.filter_by(uid = student_id).first()
assignment = assignment.lower()
assignment = assignment.capitalize()
newMark = int(newMark)
except:
return False
if(newMark == None or newMark == '' or newMark >= 101 or newMark <= -1):
return False
try:
print(str(account), assignment, newMark)
print(str(dir(account)))
setattr(account, assignment, newMark)
db.session.commit()
except Exception as e:
print(e)
return False
return True
#how do i update given a column name
def readUsers() -> List[List]: #Returns a list of dicts of all students in the db
result = User.query.filter_by(account_type='student').all() #Queries all users that are students
user_dict = []
for user in result:
account = user.get_user_preview()
user_dict.append(account)
return user_dict
def readRegrade() -> List[List]: #Returns a list of dicts of all students in the db
result = RegradeRequests.query.all() #Queries all users
regrade_dict = []
for regrade in result:
request = regrade.getRegradeRequestInfo()
regrade_dict.append(request)
return regrade_dict
#If the person for somereason went to this url, then redirect them to the mark page.
#Only post requests can be done.
@app.route("/markSendRegrade", methods = ['POST'])
def sendRegrade():
#Check if name in session
#Check if student type
if 'name' not in session:
return redirect(url_for('login'))
if request.method == 'GET':
return redirect(url_for('login'))
type = request.form.get('EvaluationName') #UID
reason = request.form.get('reason')
uid = session['name']['uid']
regradeObj = RegradeRequests(uid, type, reason)
try:
db.session.add(regradeObj)
db.session.commit()
except Exception as e:
print(e)
return redirect(url_for('mark_page'))
#-------------------------------------------------------------------------------------------------------#
def readFeedback() -> List[List]: #Returns a list of dicts of all students in the db
result = FeedBack.query.all() #Change query to fit username.
feedback_dict = []
for feedback in result:
studentfeedback = feedback.getFeedBackInfo()
feedback_dict.append(studentfeedback)
return feedback_dict
#-------------------------------------------------------------------------------------------------------#
@app.route("/feedback", methods = ['GET','POST'])
def Feedback():# should i off it
if 'name' not in session:
return redirect(url_for('login'))
user_role = session['name']['account_type']
if(user_role == 'student'):
result = User.query.filter(User.account_type.like('admin'))
teachers = []
for user in result:
account = user.get_teacher_details()
teachers.append(account)
if request.method == 'POST':
Q1Answer = request.form.get('Question1')
Q2Answer = request.form.get('Question2')
Q3Answer = request.form.get('Question3')
Q4Answer = request.form.get('Question4')
name = request.form.get('ChooseTeacher')
FeedbackObj = FeedBack(Q1Answer,Q2Answer,Q3Answer,Q4Answer,name)
db.session.add(FeedbackObj)
db.session.commit()
return render_template('studentfeedbackview.html', teachers = teachers)
return render_template('studentfeedbackview.html', teachers = teachers)
feedback = readFeedback()
return render_template('teacherfeedbackview.html', feedback_bk = feedback)
#test it how can itest it if it doesnt
#--------------------------------------Generic Form ---------------------------------#
def getMarkList(username:int):
#Precondition: username exists and is a student type.
#Given a username
#iterate through the mark list, and return a html string
result = StudentMarks.query.filter_by(uid = int(username)) #Query user who has that specific userid
result = result[0].get_mark_info()
return result
def validID(username:str):
return username.isdigit()
def has_empty_input(username:str, password:str, first_name:str, last_name:str, email:str)->bool:
if(username == None or password == None or first_name == None or last_name == None or email == None):
return True
#Strip
username = username.strip()
password = password.strip()
first_name = first_name.strip()
last_name = last_name.strip()
email = email.strip()
if((len(username) == 0 or len(password) == 0 or len(first_name) == 0 or len(last_name) == 0 or len(email) == 0)):
#If any of them are length 0
return True
return False
def user_id_exists(uid: int) -> bool: #Checks if the uid is in the database
uid_exist = User.query.filter_by(uid = uid).first()
return True if (uid_exist != None) else False
def email_taken(email: str) -> bool: #Checks if the email is in db
email_exists = User.query.filter_by(email = email).first()
return True if (email_exists != None) else False
def check_login(uid: str, password: str) -> bool:
#Gets the userid/password in raw form [Given in param]
#Do bcrypt on the provided userid
#Check if the user exists ie uuid exists in the database by quering password with userid
#If the result is empty, then return a note saying user or password is not correct
#If the password is incorrect return the same note
#If its correct, then add it to the session and send a redirect to the homepage
if(not(len(uid) and len(password))):
print("No password entered")
return False
if(user_id_exists(uid) == True):
account = User.query.filter_by(uid = uid).first()
account = account.get_user_info()
if not bcrypt.check_password_hash(account['password'], password):
print("Password is incorrect. Try again.")
return False
else:
print("Login Success")
session['name'] = account #hashmap of account information
return True
else:
print("Login Failed")
return False
#yeo, check if user is in the database ye. user will be empty type if it is not in it
#then hash password and check if its valid only if user exists.
#ok
#use the user_exist function
#Registration
#dont touch this code it works fine
def create_user(username:str, password:str , firstname:str ,lastname:str, email:str) -> str:
#Test code on how adding user to a database works
account_type = 'student' #By default.
hashed_password = bcrypt.generate_password_hash(password).decode('utf8')
user = User(int(username),hashed_password, firstname, lastname, email, account_type) #We first have to create an object of the User, and fill it up
db.session.add(user) #U have to save it in a database session log, [different from user auth sessions]
db.session.commit() #Then u commit to have the changes in db.dession applied to commit
#Add user marking
student_mark = StudentMarks(int(username), 0, 0, 0, 0, 0 ,0 ,0 ,0) #Everyone starts off at 0, and earn their grade up.
db.session.add(student_mark)
db.session.commit()
print("Account creation successful")
return
#-----------------------------------------------------------------------------------------------------------------------------------#
##Marks section Code
#d
#We put this function on top of any page that we wanna protect, and just check the session
#If the session is valid ie user is logged in then it does nothing
#Otherwise it just redirects them back to the page.
if __name__ == "__main__":
app.secret_key = b"secretkey"
db.create_all() #Creates the tables if it does not already exist.
app.run(debug=True)