-
Notifications
You must be signed in to change notification settings - Fork 2
/
routes.py
517 lines (454 loc) · 20.1 KB
/
routes.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
import os
import secrets
from PIL import Image
from flask import render_template, url_for, flash, redirect, request, abort
from learnlogs import app, db, bcrypt
from learnlogs.forms import EnrollForm, LoginForm, Submit_Student_mark, SessionForm
from learnlogs.models import Student, Session, Student_Session
from flask_login import login_user, current_user, logout_user, login_required
from learnlogs.data import get_by_grade, get_top, get_list_of_student_marks
@app.route("/", methods=['GET', 'POST'])
@app.route("/home", methods=['GET', 'POST'])
def home():
"""Home page
Returns:
HTML home page: home page
"""
return render_template('home.html')
@app.route("/enroll", methods=['GET', 'POST'])
def enroll():
"""enroll page
Returns:
the enroll page: this page to register a new student
"""
# if current_user. uthenticated:
# return redirect(url_for('home'))
form = EnrollForm()
photo_file = 'default.jpg'
if form.photo_link.data:
photo_file = save_picture(form.photo_link.data)
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(
form.password.data).decode('utf-8')
student = Student(
student_name=form.student_name.data,
email=form.email.data,
password=hashed_password,
student_phone=form.student_phone.data,
parent_name=form.parent_name.data,
parent_phone=form.parent_phone.data,
address=form.address.data,
photo_link=photo_file,
grade=form.grade.data
)
db.session.add(student)
db.session.commit()
flash('Your account has been created! You are now able to log in',
'success')
return redirect(url_for('login'))
return render_template('new-enroll.html', title='Register', form=form)
@app.route("/login", methods=['GET', 'POST'])
def login():
"""login page
Returns:
the login page: this page to login as a student or teacher
"""
form = LoginForm()
if current_user.is_authenticated:
return redirect(url_for('profile1', id=current_user.id))
if form.validate_on_submit():
if form.email.data == '[email protected]' and form.password.data == '1234':
login_user(
Student.query.filter_by(
email='[email protected]').first(),
remember=False)
return redirect(url_for('dashboard'))
student = Student.query.filter_by(email=form.email.data).first()
if student and bcrypt.check_password_hash(
student.password, form.password.data):
login_user(student, remember=False)
return redirect(url_for('profile1', id=student.id))
else:
flash('Login Unsuccessful. Please check email and password', 'danger')
return render_template('login.html', title='Login', form=form)
@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
"""dashboard page
Returns:
the dashboard: the dashboard page for the teacher
"""
if current_user.is_authenticated:
if current_user.email == ('[email protected]'):
student_first = len(Student.query.filter_by(grade='first').all())
student_second = len(Student.query.filter_by(grade='second').all())
student_third = len(Student.query.filter_by(grade='third').all())
session_first = len(Session.query.filter_by(grade='first').all())
session_second = len(Session.query.filter_by(grade='second').all())
session_third = len(Session.query.filter_by(grade='third').all())
top_student_first = get_top('first')
first_element = next(iter(top_student_first.items()))
first_value1 = first_element[1]
top_student_second = get_top('second')
first_element = next(iter(top_student_second.items()))
first_value2 = first_element[1]
top_student_third = get_top('third')
first_element = next(iter(top_student_third.items()))
first_value3 = first_element[1]
return render_template(
'dashboard.html',
student_first=student_first,
student_second=student_second,
student_third=student_third,
session_first=session_first,
session_second=session_second,
session_third=session_third,
top_student_first=first_value1,
top_student_second=first_value2,
top_student_third=first_value3,
title='Dashboard')
else:
return render_template('error_page.html', title='Register', message='this page is only for teacher')
else:
return render_template('error_page.html', title='Register', message='this page is only for teacher')
@login_required
@app.route('/dashboard/<string:grade>', methods=['GET', 'POST'])
def dashboard_grade(grade):
"""dashboard_grade page
Args:
grade (string): input grade to show the dashboard for this grade
Returns:
the grade page: the dashboard page for the teacher for a specific grade
"""
if current_user.is_authenticated:
if current_user.email == ('[email protected]'):
all_data = get_by_grade(grade)
students = Student.query.filter_by(grade=grade).all()
session = Session.query.filter_by(grade=grade).all()
return render_template(
'dashboard_grade.html',
all_students=all_data,
students=students,
sessions=session,
title='dashboard_grade')
else:
return render_template('error_page.html', title='Register', message='this page is only for teacher')
else:
return render_template('error_page.html', title='Register', message='this page is only for teacher')
@login_required
@app.route('/profile/<int:id>', methods=['GET', 'POST'])
def profile1(id):
"""profile1 page
Args:
id (integer): the id of student
Returns:
the profile page: this page show the profile for student
"""
if current_user.is_authenticated:
if current_user.id == id or current_user.email == (
student = Student.query.filter_by(id=id).first()
all_session = Session.query.filter_by(grade=student.grade).all()
student_attended = sorted(
student.attended, key=lambda session: session.id)
student_marks = db.session.query(Student_Session).filter(
Student_Session.c.student_id == id).order_by(
Student_Session.c.session_id).all()
if student:
return render_template(
'profile.html',
student=student,
marks=student_marks,
attended=student_attended,
sessions=all_session,
title='Profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
@app.route('/statistics/<int:id>', methods=['GET', 'POST'])
def profile2(id):
"""sratestics page
Args:
id (integer): the id of student
Returns:
return the statistics page: return the statistics page for the student
"""
if current_user.is_authenticated:
if current_user.id == id or current_user.email == (
student = Student.query.filter_by(id=id).first()
all_session = Session.query.filter_by(grade=student.grade).all()
student_attended = sorted(
student.attended, key=lambda session: session.id)
student_marks = db.session.query(Student_Session).filter(
Student_Session.c.student_id == id).order_by(
Student_Session.c.session_id).all()
all_grade_data = get_by_grade(student.grade)
student_data = all_grade_data[student.id]
list_of_student_marks = get_list_of_student_marks(student)
students_rank = get_top(student.grade)
rating = 0
for index, (key, value) in enumerate(students_rank.items()):
if key == id:
rating = index + 1
break
if student:
return render_template(
'profile_stats.html',
student=student,
marks=student_marks,
attended=student_attended,
number_of_attended=len(student_attended),
sessions=all_session,
sessions_number=len(all_session),
precentage=student_data['precentage'],
list_of_student_marks=list_of_student_marks,
rating=rating,
title='Profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
@login_required
@app.route('/logs/<int:id>', methods=['GET', 'POST'])
def profile(id):
"""logs page for student
Args:
id (integer): id for the student
Returns:
the logs page: the logs page for the sessions and marks for the student
"""
if current_user.is_authenticated:
if current_user.id == id or current_user.email == (
student = Student.query.filter_by(id=id).first()
all_session = Session.query.filter_by(grade=student.grade).all()
student_attended = sorted(
student.attended, key=lambda session: session.id)
student_marks = db.session.query(Student_Session).filter(
Student_Session.c.student_id == id).order_by(
Student_Session.c.session_id).all()
if student:
return render_template(
'profile_logs.html',
student=student,
marks=student_marks,
attended=student_attended,
sessions=all_session,
title='Profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
@login_required
@app.route('/evaluate/<string:grade>', methods=['GET', 'POST'])
def envaluate_session(grade):
"""envaluate_session page
Args:
grade (string): grade of student
Returns:
envaluate page: to set the marks and attedence for the students
"""
if current_user.is_authenticated:
if current_user.email == ('[email protected]'):
all_sessions = Session.query.filter_by(grade=grade).all()
all_data = db.session.query(Student_Session).all()
all_session_created = []
for info in all_data:
all_session_created.append(info.session_id)
return render_template(
'evaluate_session.html',
sessions=all_sessions,
session_ids=all_session_created,
title='envaluate')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
@login_required
@app.route('/evaluate/<int:id>', methods=['GET', 'POST'])
def evaluate(id):
"""envaluate_session page
Args:
grade (string): grade of student
Returns:
envaluate page: to set the marks and attedence for the students
"""
if current_user.is_authenticated:
if current_user.email == ('[email protected]'):
session = Session.query.filter_by(id=id).first()
students = Student.query.filter_by(grade=session.grade).all()
form = Submit_Student_mark()
if form.validate_on_submit():
for student, student_form in zip(students, form.students_list):
student_session_entry = Student_Session.insert().values(
student_id=student.id,
session_id=id,
mark=student_form.quiz_mark.data,
full_mark=form.quiz_full_mark.data
)
db.session.execute(student_session_entry)
db.session.commit()
return redirect(url_for('dashboard_grade', grade=session.grade))
else:
print("Form validation failed!")
print(form.errors)
return render_template('evalute.html', form=form,
students=students, title='create_session')
else:
return render_template('error_page.html', title='Register', message='Unauthorized access')
@app.route('/session/<string:grade>', methods=['GET', 'POST'])
def create_session(grade):
"""Create new seesion page
Args:
grade (string): grade of student
Returns:
Session page: to create a new session for the teacher
"""
if current_user.is_authenticated:
if current_user.email == ('[email protected]'):
form = SessionForm()
if form.validate_on_submit():
attachment_file = 'default.pdf'
video_file = 'default.mp4'
if form.attachment_link.data:
attachment_file = save_path(
form.attachment_link.data, 'static/attachments')
if form.video_link.data:
video_file = save_path(
form.video_link.data, 'static/videos')
session = Session(title=form.title.data,
description=form.description.data,
attachment_link=attachment_file,
video_link=video_file,
grade=grade)
db.session.add(session)
db.session.commit()
return redirect(url_for('dashboard_grade', grade=grade))
else:
print("Form validation failed!")
print(form.errors)
return render_template(
'session_info_form.html',
form=form,
title='session_info_form')
else:
return render_template('error_page.html', title='Register', message='Unauthorized access')
@app.route('/session/<int:session_id>', methods=['GET', 'POST'])
def session_info_for_student(session_id):
student_id = request.args.get('student_id')
if student_id is None:
# Handle the case where student_id is not provided
return render_template('error_page.html', title='Error', message='Student ID is missing')
if current_user.is_authenticated:
session = Session.query.filter_by(id=session_id).first()
student = Student.query.filter_by(id=student_id).first()
return render_template(
'session_for_student.html',
session=session, student=student,
title='Session Info')
else:
return render_template('error_page.html', title='Error', message='Unauthorized access')
@login_required
@app.route('/all_session/<string:grade>', methods=['GET', 'POST'])
def all_session_grade(grade):
if current_user.is_authenticated:
if current_user.id == id or current_user.email == (
all_session = Session.query.filter_by(grade=grade).all()
student = Student.query.filter_by(grade=grade).first()
number_of_students = len(Student.query.filter_by(grade=grade).all())
return render_template('all_session_grade.html',
sessions=all_session, student=student, number_of_students=number_of_students,
grade=grade, title='session_grade')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
else:
return render_template('error_page.html', title='Register', message='you can only view your profile')
@login_required
@app.route('/student_attended/<int:id>', methods=['GET', 'POST'])
def student_attended(id):
"""dashboard_grade page
Args:
grade (string): input grade to show the dashboard for this grade
Returns:
the grade page: the dashboard page for the teacher for a specific grade
"""
if current_user.is_authenticated:
if current_user.email == ('[email protected]'):
session = Session.query.filter_by(id=id).first()
grade = session.grade
all_data = get_by_grade(grade)
students_attended = session.attended_student
return render_template(
'student_attended.html',
all_students=all_data,
students=students_attended,
sessions=session,
title='student attended')
else:
return render_template('error_page.html', title='Register', message='this page is only for teacher')
else:
return render_template('error_page.html', title='Register', message='this page is only for teacher')
@login_required
@app.route("/logout")
def logout():
"""Logout
Returns:
logout page: to logout from the system
"""
logout_user()
return redirect(url_for('login'))
def save_picture(form_picture):
"""Sava a Photo
Args:
form_picture (straing): name of the photo
Returns:
hashing string: the new name of the photo
"""
random_hex = secrets.token_hex(8)
_, f_ext = os.path.splitext(form_picture.filename)
picture_fn = random_hex + f_ext
picture_path = os.path.join(app.root_path, 'static/images', picture_fn)
output_size = (125, 125)
i = Image.open(form_picture)
i.thumbnail(output_size)
i.save(picture_path)
return picture_fn
def save_path(form_attachment, path):
"""Sava a file or video
Args:
form_picture (straing): name of the file or video
Returns:
hashing string: the new name of the file or video
"""
random_hex = secrets.token_hex(8)
_, f_ext = os.path.splitext(form_attachment.filename)
attachment_fn = random_hex + f_ext
attachment_path = os.path.join(app.root_path, path, attachment_fn)
# output_size = (125, 125)
# i = Image.open(form_attachment)
# i.thumbnail(output_size)
form_attachment.save(attachment_path)
return attachment_fn
"""
@app.route("/account", methods=['GET', 'POST'])
@login_required
def account():
form = UpdateAccountForm()
if form.validate_on_submit():
if form.picture.data:
picture_file = save_picture(form.picture.data)
current_user.image_file = picture_file
current_user.username = form.username.data
current_user.email = form.email.data
db.session.commit()
flash('Your account has been updated!', 'success')
return redirect(url_for('account'))
elif request.method == 'GET':
form.username.data = current_user.username
form.email.data = current_user.email
image_file = url_for('static', filename='profile_pics/' + current_user.image_file)
return render_template('account.html', title='Account',
image_file=image_file, form=form)
"""