-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
145 lines (116 loc) · 5.17 KB
/
view.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
from forms import StudentLoginForm, AddGradeForm, TeacherLoginForm
from forms import flash_errors
from model import Student, Teacher, Subject, Grade, TeacherSubject
from model import get_db
from peewee import DatabaseError
from bcrypt import hashpw
from flask import flash, render_template, redirect, session, url_for
from exceptions import WrongPassword
"""This module contains only methods, which are used in main file gradebook.py"""
db = get_db()
def authorize_student(student):
session['logged_in'] = True
session['user_id'] = student.id
session['username'] = student.username
session['type'] = 'S'
def authorize_teacher(teacher):
session['logged_in'] = True
session['user_id'] = teacher.id
session['username'] = teacher.username
session['type'] = 'T'
def get_current_user():
"""Returns an object of Student or Teacher class, whose credentials are currently saved in session."""
if session['logged_in']:
if session['type'] == 'S':
return Student.get(Student.username == session['username'])
elif session['type'] == 'T':
return Teacher.get(Teacher.username == session['username'])
def student_login_():
form = StudentLoginForm()
if form.validate_on_submit():
try:
student = Student.get(username=form.username.data)
# adequate salt is stored in the password itself
stored_password = student.password.encode('utf-8')
password_to_check = form.password.data.encode('utf-8')
password = hashpw(password_to_check, stored_password)
if password != stored_password:
raise WrongPassword('Wrong password')
except WrongPassword:
flash('Wrong password')
except Student.DoesNotExist:
flash('Wrong username or password')
else:
authorize_student(student)
return redirect(url_for('student_profile'))
flash_errors(form)
return render_template('student_login.html', form=form)
def student_profile_():
student = get_current_user()
subjects = Subject.select()
grades = Grade.select().where(Grade.student == student)
return render_template('student_profile.html', student=student, subjects=subjects, grades=grades)
def student_profile_foreign_(username):
student = Student.get(Student.username == username)
subjects = Subject.select()
grades = Grade.select().where(Grade.student == student)
return render_template('student_profile.html', student=student, subjects=subjects, grades=grades)
def add_grade_():
form = AddGradeForm()
if form.validate_on_submit():
try:
with db.transaction():
grade = Grade.create(
student=Student.get(Student.username == form.student_select.data),
subject=Subject.get(Subject.name == form.subject_select.data),
teacher=get_current_user(),
grade=form.grade.data
)
except DatabaseError:
flash('An error occurred while adding a grade')
else:
flash('Grade ' + str(grade.grade) + ' assigned to student ' + str(grade.student))
return redirect(url_for('groups', group=grade.student.username))
flash_errors(form)
students = Student.select()
subjects = Subject.select()
return render_template('add_grade.html', students=students, subjects=subjects, form=form)
def teacher_login_():
form = TeacherLoginForm()
if form.validate_on_submit():
try:
teacher = Teacher.get(username=form.username.data)
# adequate salt is stored in the password itself
stored_password = teacher.password.encode('utf-8')
password_to_check = form.password.data.encode('utf-8')
password = hashpw(password_to_check, stored_password)
if password != stored_password:
raise WrongPassword('Wrong password')
except WrongPassword:
flash('Wrong password')
except Teacher.DoesNotExist:
flash('Wrong username or password')
else:
authorize_teacher(teacher)
return redirect(url_for('teacher_profile'))
flash_errors(form)
return render_template('teacher_login.html', form=form)
def teacher_profile_():
teacher = get_current_user()
specs = TeacherSubject.select().where(TeacherSubject.teacher == teacher)
return render_template('teacher_profile.html', teacher=teacher, specializations=specs)
def groups_():
student_groups = Student.select(Student.group).distinct().order_by(Student.group.asc())
return render_template('groups.html', student_groups=student_groups)
def group_():
group_number = get_current_user().group
students = Student.select().where(Student.group == group_number)
return render_template('group.html', group=group_number, students=students)
def group_foreign_(group_number):
students = Student.select().where(Student.group == group_number)
return render_template('group.html', group=group_number, students=students)
def logout_():
"""Clears all session elements."""
for field in session:
session[field] = None
return redirect(url_for('homepage'))