-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
543 lines (411 loc) · 14.9 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
from datetime import datetime, timedelta
from os import urandom
from flask import Flask, render_template, url_for, flash, redirect, request, session
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__, template_folder="templates")
"""
Session Config
"""
app.config['SECRET_KEY'] = urandom(16)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///assignment3.db'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=2)
app.secret_key = b"secretkey"
"""
Encryption Config
"""
bcrypt = Bcrypt(app)
"""
Database Config
"""
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'User'
id = db.Column(db.Integer, primary_key=True)
type = db.Column(db.String(30), nullable=False, default='student')
username = db.Column(db.String(30), unique=True, nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
password = db.Column(db.String(100), nullable=False)
name = db.Column(db.String(30), nullable=False)
grades = db.relationship('Grade', backref='author', lazy=True) # Student has grades
feedback = db.relationship('Feedback', backref='author', lazy=True) # Instructor has feedback
def __repr__(self):
return f"User('{self.username}')"
class Grade(db.Model):
__tablename__ = 'Grade'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False)
ass_name = db.Column(db.String(30), nullable=False)
score = db.Column(db.Float, nullable=False)
weight = db.Column(db.Float, nullable=False)
def __repr__(self):
return f"Grade('{self.score}%')"
class RemarkRequest(db.Model):
__tablename__ = 'RemarkRequest'
id = db.Column(db.Integer, primary_key=True)
grade_id = db.Column(db.Integer, db.ForeignKey('Grade.id'), nullable=False)
details = db.Column(db.Text())
closed = db.Column(db.Integer, default=0)
def __repr__(self):
return f"RemarkRequest('{self.details}')"
class Feedback(db.Model):
__tablename__ = 'Feedback'
id = db.Column(db.Integer, primary_key=True)
instructor_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False)
response = db.Column(db.Text())
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
"""
Routes & Rendering
"""
"""
Login
"""
@app.route('/login', methods=['GET', 'POST'])
def login():
if 'username' in session:
return redirect(url_for('root'))
if request.method == 'GET':
page_name = "login"
return render_template("login.html", page_name=page_name)
if request.method == 'POST':
username = request.form['loginUsername']
password = request.form['loginPassword']
# Check auth
if check_auth(username, password):
print("User/Passwd Matched: Logging the user in")
session['username'] = username
session['is_student'] = is_student(username)
session.permanent = True
return redirect(url_for('root'))
else:
flash("Please check your login credentials and try again!", "error")
page_name = "login"
return render_template('login.html', page_name=page_name)
"""
Logout
"""
@app.route('/logout')
def logout():
session.pop('username', default=None)
return redirect(url_for('login'))
"""
Register
"""
@app.route('/register', methods=['GET', 'POST'])
def register():
if 'username' in session:
return redirect(url_for('root'))
if request.method == 'GET':
page_name = "register"
return render_template("register.html", page_name=page_name)
if request.method == 'POST':
username = request.form['regUsername']
name = request.form['regName'].capitalize()
email = request.form['regEmail']
password = bcrypt.generate_password_hash(
request.form['regPassword']).decode('utf-8')
type = 'student'
if 'regType' in request.form:
type = 'instructor'
# Prevents integrity error
if user_exists(username, email):
flash("A user with that username or email aleady exists.", "error")
return redirect(url_for('register'))
else:
# Add to database
create_user(username, name, email, password, type)
# Flash & Render
flash("You were successfully registered! Please log in.", "success")
return redirect(url_for('login'))
# Can users see this page without being logged in? -
# I think they should, but we can ask on piazza
"""
Home
"""
@app.route('/')
@app.route('/home')
def root():
if 'username' not in session:
return redirect(url_for('login'))
else:
page_name = "home"
name = get_name(session['username']).capitalize()
return render_template('home.html',
page_name=page_name,
name=name)
"""
View Grades
Student view only
"""
@app.route('/view/grades')
def view_grades():
if 'username' not in session:
return redirect(url_for('login'))
if not session['is_student']:
return redirect(url_for('root'))
page_name = 'view_grades'
grades = get_grades(session['username'])
requests = []
for grade in grades:
remark_request = RemarkRequest.query.filter_by(grade_id=grade.id).first()
if remark_request is None:
requests.append("none")
else:
if remark_request.closed == 0:
requests.append("ongoing")
else:
requests.append("closed")
return render_template('view_grades.html', page_name=page_name, grades=grades, requests=requests)
@app.route('/remark_request/<grade_id>', methods=['POST'])
def submit_remark_request(grade_id):
details = request.form['remarkContent']
remark_request = RemarkRequest(grade_id=grade_id, details=details)
db.session.add(remark_request)
db.session.commit()
flash("Your remark request was submitted", "success")
return redirect(url_for('view_grades'))
"""
View all students
Instructor view only
"""
@app.route('/view/students')
def view_students():
if 'username' not in session:
return redirect(url_for('login'))
if session['is_student']:
return redirect(url_for('view_grades'))
if request.method == 'GET':
page_name = 'view_students'
students = get_all_students()
return render_template('view_students.html', page_name=page_name, students=students)
"""
View and add student grades
Instructor view only
"""
@app.route('/view/student/<username>', methods=['GET', 'POST'])
def view_student(username):
if 'username' not in session:
return redirect(url_for('login'))
if session['is_student']:
return redirect(url_for('view_grades'))
student = User.query.filter_by(username=username).first()
grades = Grade.query.filter_by(user_id=student.id).all()
if request.method == 'POST':
ass_name = request.form['gradeAssName']
weight = request.form['gradeWeight']
score = request.form['gradeScore']
create_grade(ass_name, weight, score, student.id)
flash("Grade added", "success")
return redirect(url_for('view_student', username=username)) # do not remove
page_name = "view_student"
return render_template('view_student.html', page_name=page_name, student=student, grades=grades)
@app.route('/view/remark_requests')
def view_remark_requests():
if 'username' not in session:
return redirect(url_for('login'))
if session['is_student']:
return redirect(url_for('view_grades'))
requests = RemarkRequest.query.all()
# Paralel array
grades = []
students = []
for request in requests:
grade = Grade.query.filter_by(id=request.grade_id).first()
grades.append(grade)
student = User.query.filter_by(id=grade.user_id).first()
students.append(student)
page_name = 'view_remark_requests'
return render_template('view_remark_requests.html',
page_name=page_name,
requests=requests,
grades=grades,
students=students)
@app.route('/close_request/<id>', methods=['POST'])
def close_request(id):
db.session.query(RemarkRequest).filter(RemarkRequest.id == id).update({'closed': 1})
db.session.commit()
return redirect(url_for('view_remark_requests'))
@app.route('/edit/student/<username>/grade/<grade_id>', methods=['GET', 'POST'])
def edit_grade(username, grade_id):
if 'username' not in session:
return redirect(url_for('login'))
if session['is_student']:
return redirect(url_for('home'))
if request.method == 'GET':
page_name = 'edit_grade'
grade = Grade.query.filter_by(id=grade_id).first()
return render_template('edit_grade.html', page_name=page_name, username=username, grade_id=grade_id,
grade=grade)
if request.method == 'POST':
new_name = request.form['newAssName']
new_weight = request.form['newWeight']
new_score = request.form['newScore']
db.session.query(Grade).filter(Grade.id == grade_id).update({
'ass_name': new_name,
'weight': new_weight,
'score': new_score
})
db.session.commit()
return redirect(url_for('view_student', username=username))
"""
Announcements
"""
@app.route('/announcements')
def announcements():
if 'username' not in session:
return redirect(url_for('login'))
page_name = "announcements"
return render_template("announcements.html", page_name=page_name)
"""
Anonymous Feedback (Students)
"""
@app.route('/add/feedback', methods=['GET', 'POST'])
def add_feedback():
if 'username' not in session:
return redirect(url_for('login'))
if not session['is_student']:
return redirect(url_for('view_feedback'))
if request.method == 'GET':
instructors = get_instructors()
print(instructors)
for instructor in instructors:
print(instructor.name)
page_name = "add_feedback"
return render_template("add_feedback.html",
page_name=page_name,
questions=questions,
instructors=instructors)
if request.method == 'POST':
instructor_id = int(request.form['feedbackInstructor'])
response = ""
for i in range(4): # assumption: 4 questions in total
response += "<h4>" + questions[i + 1] + "</h4>"
response += "<p>" + request.form["feedbackQuestion" + str(i + 1)] + "</p>"
# add response as feedback
create_feedback(instructor_id, response)
flash("Your feedback was successfully sent! Thank you", "success")
return redirect(url_for('add_feedback'))
"""
Anonymous Feedback (Instructors)
"""
@app.route('/view/feedback')
def view_feedback():
if 'username' not in session:
return redirect(url_for('login'))
if session['is_student']:
return redirect(url_for('add_feedback'))
page_name = 'view_feedback'
feedbacks = get_all_feedback(session['username'])
return render_template("view_feedback.html", page_name=page_name, feedbacks=feedbacks)
"""
Assignments
"""
@app.route('/assignments')
def assignments():
if 'username' not in session:
return redirect(url_for('login'))
page_name = "assignments"
return render_template("assignments.html", page_name=page_name)
"""
Instructors
"""
@app.route('/instructors')
def instructors():
if 'username' not in session:
return redirect(url_for('login'))
page_name = "instructors"
return render_template("instructors.html", page_name=page_name)
"""
Labs
"""
@app.route('/labs')
def labs():
if 'username' not in session:
return redirect(url_for('login'))
page_name = "labs"
return render_template("labs.html", page_name=page_name)
"""
Midterms
"""
@app.route('/midterms')
def midterms():
if 'username' not in session:
return redirect(url_for('login'))
page_name = "midterms"
return render_template("midterms.html", page_name=page_name)
"""
Weekly content
"""
@app.route('/weekly_content')
def weekly_content():
if 'username' not in session:
return redirect(url_for('login'))
page_name = "weekly_content"
return render_template("weekly_content.html", page_name=page_name)
"""
Helper methods
"""
"""
Precondition: all parameters are validated
"""
def create_user(username, name, email, password, type):
user = User(username=username,
name=name,
email=email,
password=password,
type=type)
db.session.add(user)
db.session.commit()
"""
Precondition: <password> is not hashed.
"""
def check_auth(username, password):
user = User.query.filter_by(username=username).first()
return user and bcrypt.check_password_hash(user.password, password)
# Edge case? bugs when no user in system
def get_name(username):
if User.query.filter_by(username=username).first():
return User.query.filter_by(username=username).first().name
else:
return "ERROR: No user found by that name"
def user_exists(username, email):
username_match = User.query.filter_by(username=username).first()
email_match = User.query.filter_by(email=email).first()
return email_match is not None or username_match is not None
def is_student(username):
return User.query.filter_by(username=username).first().type == 'student'
def create_feedback(instructor_id, response):
feedback = Feedback(instructor_id=instructor_id, response=response)
db.session.add(feedback)
db.session.commit()
"""
Return all instructors
"""
def get_instructors():
return User.query.filter_by(type="instructor").all()
def get_all_feedback(username):
return Feedback.query.filter_by(
instructor_id=User.query.filter_by(username=username).first().id).all()
def get_all_students():
return User.query.filter_by(type='student').all()
def create_grade(ass_name, weight, score, student_id):
grade = Grade(ass_name=ass_name, weight=weight, score=score, user_id=student_id)
db.session.add(grade)
db.session.commit()
def get_grades(username):
user = User.query.filter_by(username=username).first()
return Grade.query.filter_by(user_id=user.id).all()
"""
Constants & Other variables
"""
# Can easily make this dynamic, but not a requirement.
# Using dictionary because it plays nicely with Jinja template
questions = {1: "What do you like about the instructor teaching?",
2: "What do you recommend the instructor to do to improve their teaching?",
3: "What do you like about the labs?",
4: "What do you recommend the lab instructors to do to improve their lab teaching?"}
"""
Run
"""
if __name__ == '__main__':
app.run(debug=True)