-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
521 lines (411 loc) · 19.8 KB
/
Copy pathapp.py
File metadata and controls
521 lines (411 loc) · 19.8 KB
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
from cmath import log
from flask import Flask, flash, redirect, render_template, request, session, url_for
from flask_session import Session
from flask_mysqldb import MySQL
from tempfile import mkdtemp
from werkzeug.security import check_password_hash, generate_password_hash
from datetime import datetime
from functools import wraps
from datetime import datetime
import re
import json
from app_config import data, roles
# Flask instance
app = Flask(__name__)
# Ensure templates are auto-reloaded
app.config['TEMPLATES_AUTO_RELOAD'] = data['TEMPLATES_AUTO_RELOAD']
# flask-mysqldb
app.config['MYSQL_HOST'] = data['MYSQL_HOST']
app.config['MYSQL_USER'] = data['MYSQL_USER']
app.config['MYSQL_PASSWORD'] = data['MYSQL_PASSWORD']
app.config['MYSQL_DB'] = data['MYSQL_DB']
app.config['MYSQL_CURSORCLASS'] = data['MYSQL_CURSORCLASS']
mysql = MySQL(app)
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config['SECRET_KEY'] = data['SECRET_KEY']
Session(app)
# Function decorator to request the user to be logged in
def login_required(f):
"""
Decorate routes to require login.
https://flask.palletsprojects.com/en/1.1.x/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function
# Making sure responses are not cached
@app.after_request
def after_request(response):
"""Ensure responses aren't cached"""
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/grades", methods=["POST"])
@login_required
def grades():
"""Grading each student's activities"""
# User is not a teacher
if session['role'] == 'student':
return redirect(url_for('index'))
else:
# User reached before grading a student
if not request.form.get("activity_grade"):
with mysql.connection.cursor() as cursor:
# Retrieving students per activity
cursor.execute(
"""
SELECT
users.id,
users.fullname,
students_activities.submitted,
students_activities.grade
FROM users
INNER JOIN students_activities ON students_activities.student_id=users.id
WHERE students_activities.subject_id = %s AND students_activities.activity_id = %s
""", [request.form.get("subject_id"), request.form.get("activity_id")])
students = cursor.fetchall()
return render_template("teacher_grades.html", students=students, activity_id=request.form.get("activity_id"))
# User graded a student
else:
with mysql.connection.cursor() as cursor:
# Updating student's grade on activity
cursor.execute(
"""
UPDATE students_activities SET grade = %s WHERE student_id = %s AND activity_id = %s
""", [
request.form.get("activity_grade"),
request.form.get("student_id"),
request.form.get("activity_id"),
])
mysql.connection.commit()
return redirect(url_for('subjects'))
# Main route / Home page
@app.route("/")
@login_required
def index():
"""Home page"""
# User logged in is an admin
if session["role"] == "admin":
with mysql.connection.cursor() as cursor:
# Retrieves all subjects and activities registered
cursor.execute('SELECT subjects.id, subjects.name, activities.id, activities.title, activities.description, activities.due_date FROM subjects JOIN activities ON subjects.id=activities.subject_id ORDER BY subjects.id')
data = list(cursor.fetchall())
# Formating due dates as strings
for d in data:
d['due_date'] = d['due_date'].strftime("%Y/%m/%d %H:%M:%S")
return render_template("index_admin.html", data=data)
# User logged in is a teacher
elif session["role"] == "teacher":
with mysql.connection.cursor() as cursor:
# Retrieving teacher's subjects and activities
cursor.execute(
'''
SELECT subjects.id, subjects.name, activities.id, activities.title, activities.description, activities.due_date
FROM subjects
INNER JOIN activities ON subjects.id=activities.subject_id
WHERE subjects.teacher_id=%s
''',
[session['user_id']])
data = list(cursor.fetchall())
return render_template('index_teacher.html', data=data)
# User logged in is a student
elif session["role"] == "student":
with mysql.connection.cursor() as cursor:
# Retrieving data of the subjects and activities assigned to the student
cursor.execute(
"""
SELECT subjects.id, subjects.name, activities.title, activities.description, activities.due_date
FROM subjects
INNER JOIN activities ON subjects.id=activities.subject_id
INNER JOIN students_subjects ON subjects.id=students_subjects.subject_id
WHERE students_subjects.student_id=%s
""",
[session['user_id']])
data = list(cursor.fetchall())
# Formatting due dates as strings
for d in data:
d['due_date'] = d['due_date'].strftime("%Y/%m/%d %H:%M:%S")
return render_template('index_student.html', data=data)
# Login route
@app.route("/login", methods=["POST", "GET"])
def login():
""""Log user in"""
# Forget any user_id
session.clear()
# User reached via POST
if request.method == "POST":
# Ensure username was submitted
if not request.form.get("username"):
flash('You must provide a username', 'error')
return render_template("login.html")
# Ensure password was submitted
elif not request.form.get("password"):
flash('You must provide a password', 'error')
return render_template("login.html")
# Query database for username
with mysql.connection.cursor() as cursor:
cursor = mysql.connection.cursor()
cursor.execute('SELECT * FROM users WHERE username = %s', [request.form.get("username")])
query = list(cursor.fetchall())
user_data = query[0]
user_data['creation_date'] = user_data['creation_date'].strftime("%Y/%m/%d %H:%M:%S")
# Ensure username exists and password is correct
if len(query) != 1 or not check_password_hash(user_data['password'], request.form.get("password")):
flash('Invalid username and/or password', 'error')
return render_template("login.html")
# Remember which user has logged in
session["user_id"] = user_data['id']
session["username"] = user_data['username']
session["fullname"] = user_data['fullname']
session["role"] = user_data['role']
session["creation_date"] = user_data['creation_date']
# Redirect user to homepage
flash('You were successfully logged in', 'success')
return redirect(url_for('index'))
# User reached via GET
else:
return render_template("login.html")
# Logout route
@app.route("/logout")
@login_required
def logout():
"""Log user out"""
# Forget any user_id
session.clear()
# Redirect user to login form
flash('You were successfully logged out', 'success')
return render_template("login.html")
# Register a new user
@app.route("/register", methods=["POST", "GET"])
def register():
"""Register user"""
# User reached via POST
if request.method == "POST":
# User did not provide the requested information
if not request.form.get("username") or not request.form.get("password") or not request.form.get("confirmation"):
flash('please provide complete information', 'error')
return render_template('register.html')
else:
try:
if not bool(re.search("^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$", str(request.form.get("password")))):
flash('invalid password', 'error')
return render_template('register.html')
# Storing provided information
username = request.form.get("username")
password = request.form.get("password")
email = request.form.get("email")
fullname = request.form.get("fullname")
role = request.form.get("role")
confirmation = request.form.get("confirmation")
# User did not select a valid role from the list
if role not in roles:
flash('select a valid role')
return render_template('register.html')
# User did not confirm provided password
if password != confirmation:
flash('please confirm password', 'error')
return render_template('register.html')
else:
# Storing new user in the database
cursor = mysql.connection.cursor()
cursor.execute('INSERT INTO users (username, password, email, fullname, role) VALUES (%s, %s, %s, %s, %s)', [username, generate_password_hash(password), email, fullname, role])
mysql.connection.commit()
flash('You were successfully registered', 'success')
return render_template('login.html')
# Username already in use
except Exception as e:
print(e)
flash('username is not available', 'error')
return render_template('register.html')
# User reached via GET
else:
return render_template("register.html")
# Register a new subject
@app.route("/registration", methods=["POST", "GET"])
@login_required
def registration():
"""Registration of subjects for teachers and students"""
# User reached via POST
if request.method == "POST":
user_id = session['user_id']
subject_id = request.form.get("subject_id")
# User is a teacher
if session['role'] == 'teacher':
teacher_id = session['user_id']
subject_id = request.form.get('subject_id')
with mysql.connection.cursor() as cursor:
# Assigning subject to teacher
cursor.execute('UPDATE subjects SET teacher_id = %s WHERE id = %s',[teacher_id, subject_id])
mysql.connection.commit()
return redirect(url_for('registration'))
# User is a student
elif session['role'] == 'student':
with mysql.connection.cursor() as cursor:
# Assigning subject to student
cursor.execute(
"""
INSERT INTO students_subjects(student_id, subject_id)
VALUES (%s, %s)
""", [session["user_id"], request.form.get("subject_id")])
mysql.connection.commit()
# Retrieving assigned subject's activities
cursor.execute('SELECT * FROM activities WHERE subject_id = %s', [request.form.get("subject_id")])
activities = list(cursor.fetchall())
# Assigning activities to student
for a in activities:
print(a)
cursor.execute(
"""
INSERT INTO students_activities(student_id, activity_id, subject_id)
VALUES (%s, %s, %s)
""", [session['user_id'], a['id'], request.form.get("subject_id")])
mysql.connection.commit()
print(f"activity_id: {a['id']} - DONE")
return redirect(url_for('subjects'))
# User reached via GET
else:
# User is a teacher
if session["role"] == 'teacher':
with mysql.connection.cursor() as cursor:
# Retrieving data of the subjects assigned to the teacher
cursor.execute('''
SELECT subjects.*, users.fullname
FROM subjects
INNER JOIN users ON subjects.teacher_id=users.id
ORDER BY subjects.teacher_id
''')
subjects = list(cursor.fetchall())
return render_template('teacher_registration.html', subjects=subjects)
# User is a student
else:
with mysql.connection.cursor() as cursor:
# Retrieving subjects assigned to the student
cursor.execute(
"""
SELECT
users.id,
users.fullname,
subjects.id,
subjects.name
FROM subjects
INNER JOIN users ON subjects.teacher_id=users.id
WHERE subjects.id NOT IN (
SELECT subject_id FROM students_subjects WHERE student_id = %s
);
""", [session['user_id']])
data = list(cursor.fetchall())
return render_template('student_registration.html', data=data)
# Review subjects assigned to the user
@app.route("/subjects", methods=["POST", "GET"])
@login_required
def subjects():
""""Accessing the user's assigned subjects"""
# User reached via POST
if request.method == "POST":
# User is a student
if session['role'] == 'student':
# Redirecting student to the same page in case they did not select a subject to review
if request.form.get("subject_id") is None:
return redirect(url_for('subjects'))
with mysql.connection.cursor() as cursor:
# Retrieving subjects assigned to the student
cursor.execute(
"""
SELECT subjects.id, subjects.name, students_subjects.grade, users.fullname
FROM subjects
INNER JOIN users ON subjects.teacher_id=users.id
INNER JOIN students_subjects ON subjects.id=students_subjects.subject_id
WHERE subjects.id=%s AND students_subjects.student_id=%s
""", [request.form.get("subject_id"), session["user_id"]])
subject = cursor.fetchall()[0]
data = json.loads((request.form.get("data")).replace("'", '"'))
# Retrieving data of the activities assigned to the student
cursor.execute(
"""
SELECT
activities.id,
activities.title,
activities.description,
activities.due_date,
students_activities.submitted,
students_activities.grade
FROM students_activities
INNER JOIN activities
ON students_activities.activity_id=activities.id
WHERE students_activities.student_id = %s
AND students_activities.subject_id = %s;
""", [session["user_id"], request.form.get("subject_id")])
activities = list(cursor.fetchall())
# Formatting due dates as strings
for a in activities:
a['due_date'] = a['due_date'].strftime("%Y/%m/%d %H:%M:%S")
return render_template('student_subjects.html', data=data, subject=subject, activities=activities)
# User is a teacher
elif session['role'] == 'teacher':
# Redirecting student to the same page in case they did not select a subject to review
if request.form.get("subject_id") is None:
return redirect(url_for('subjects'))
with mysql.connection.cursor() as cursor:
# Retrieving subjects assigned to the student
cursor.execute(
"""
SELECT users.id, users.fullname, subjects.id, subjects.name, activities.id, activities.title, activities.description, activities.due_date
FROM users
INNER JOIN subjects ON users.id=subjects.teacher_id
INNER JOIN activities ON subjects.id=activities.subject_id
WHERE subject_id = %s AND teacher_id=%s
""", [request.form.get("subject_id"), session["user_id"]])
activities = cursor.fetchall()
for a in activities:
a['due_date'] = a['due_date'].strftime("%Y/%m/%d %H:%M:%S")
data = json.loads((request.form.get("data")).replace("'", '"'))
return render_template('teacher_subjects.html', data=data, activities=activities)
# User reacherd via GET
else:
# User is a student
if session['role'] == 'student':
with mysql.connection.cursor() as cursor:
# Retrieving subjects assigned to the student
cursor.execute(
"""
SELECT subjects.id, subjects.name, users.fullname, students_subjects.grade
FROM subjects
INNER JOIN users ON subjects.teacher_id=users.id
INNER JOIN students_subjects ON subjects.id=students_subjects.subject_id
WHERE students_subjects.student_id=%s
""", [session['user_id']])
data = list(cursor.fetchall())
return render_template('student_subjects.html', data=data)
# User is a teacher
elif session['role'] == 'teacher':
with mysql.connection.cursor() as cursor:
# Retrieving subjects assigned to the teacher
cursor.execute('SELECT * FROM subjects WHERE teacher_id = %s', [session['user_id']])
data = list(cursor.fetchall())
return render_template('teacher_subjects.html', data=data)
@app.route("/submit", methods=["POST"])
@login_required
def submit():
"""Submitting an activity"""
# User is not a student
if session['role'] == 'teacher':
return redirect(url_for('index'))
# User is a student
else:
with mysql.connection.cursor() as cursor:
# Update activity to submitted
cursor.execute(
"""
UPDATE students_activities
SET submitted = 1
WHERE student_id = %s AND activity_id = %s AND subject_id = %s
""", [session['user_id'], request.form.get("activity_id"), request.form.get("subject_id")])
mysql.connection.commit()
flash('Activity submitted', 'success')
return redirect(url_for('subjects'))