app.py
11.6 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
# python standard libraries
from os import path
import logging
from contextlib import contextmanager # `with` statement in db sessions
# user installed packages
import bcrypt
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
# this project
from models import Student, Test, Question
import test
from tools import load_yaml
logger = logging.getLogger(__name__)
# ============================================================================
class AppException(Exception):
pass
# ============================================================================
# Application
# ============================================================================
class App(object):
def __init__(self, conf={}):
# online = {
# uid1: {
# 'student': {'number': 123, 'name': john, ...},
# 'test': {...}
# },
# uid2: {...}
# }
logger.info('Starting application')
self.online = dict() # {uid: {'student':{}}}
self.allowed = set([]) # '0' is hardcoded to allowed elsewhere
# build test configuration dictionary
testconf = {}
if conf['filename']:
logger.info(f'Loading test configuration "{conf["filename"]}".')
testconf.update(load_yaml(conf['filename']))
testconf.update(conf) # configuration overrides
# start test factory
try:
self.testfactory = test.TestFactory(testconf)
except test.TestFactoryException:
logger.critical('Can\'t create test factory.')
raise AppException()
# connect to database and check registered students
dbfile = path.expanduser(self.testfactory['database'])
engine = create_engine(f'sqlite:///{dbfile}', echo=False)
self.Session = scoped_session(sessionmaker(bind=engine)) # FIXME not scoped in tornado
try:
with self.db_session() as s:
n = s.query(Student).filter(Student.id != '0').count()
except Exception as e:
logger.critical(f'Database not usable {self.testfactory["database"]}.')
raise e
else:
logger.info(f'Database {self.testfactory["database"]} has {n} students.')
# command line option --allow-all
if conf['allow_all']:
logger.info('Allowing all students')
for student in self.get_all_students():
self.allow_student(student[0])
# -----------------------------------------------------------------------
def exit(self):
if len(self.online) > 1:
online_students = ', '.join(self.online)
logger.warning(f'Students still online: {online_students}')
logger.critical('----------- !!! Server terminated !!! -----------')
# -----------------------------------------------------------------------
# helper to manage db sessions using the `with` statement, for example
# with self.db_session() as s: s.query(...)
@contextmanager
def db_session(self):
try:
yield self.Session()
finally:
self.Session.remove()
# -----------------------------------------------------------------------
def login(self, uid, try_pw):
if uid not in self.allowed and uid != '0':
# not allowed
logger.warning(f'Student {uid}: not allowed to login.')
return False
with self.db_session() as s:
student = s.query(Student).filter(Student.id == uid).one_or_none()
if student is None:
# not found
logger.warning(f'Student {uid}: not found in database.')
return False
if student.password == '':
# update password on first login
hashed_pw = bcrypt.hashpw(try_pw.encode('utf-8'), bcrypt.gensalt())
student.password = hashed_pw
s.commit()
logger.info(f'Student {uid}: first login, password updated.')
elif bcrypt.hashpw(try_pw.encode('utf-8'), student.password) != student.password:
# wrong password
logger.info(f'Student {uid}: wrong password.')
return False
# success
self.allowed.discard(uid)
if uid in self.online:
logger.warning(f'Student {uid}: already logged in.')
else:
self.online[uid] = {'student': {'name': student.name, 'number': uid}}
logger.info(f'Student {uid}: logged in.')
return True
# -----------------------------------------------------------------------
def logout(self, uid):
self.online.pop(uid, None) # remove from dict if exists
logger.info(f'Student {uid}: logged out.')
# -----------------------------------------------------------------------
def generate_test(self, uid):
if uid in self.online:
logger.info(f'Student {uid}: generating new test.')
student_id = self.online[uid]['student']
self.online[uid]['test'] = self.testfactory.generate(student_id)
return self.online[uid]['test']
else:
# this implies an error in the code. should never be here!
logger.critical(f'Student {uid}: offline, can\'t generate test')
return None
# -----------------------------------------------------------------------
# ans is a dictionary {question_index: answer, ...}
# for example: {0:'hello', 1:[1,2]}
def correct_test(self, uid, ans):
t = self.online[uid]['test']
t.update_answers(ans)
grade = t.correct()
# save test in JSON format
fname = ' -- '.join((t['student']['number'], t['ref'], str(t['finish_time']))) + '.json'
fpath = path.join(t['answers_dir'], fname)
t.save_json(fpath)
# insert test and questions into database
with self.db_session() as s:
s.add(Test(
ref=t['ref'],
title=t['title'],
grade=t['grade'],
starttime=str(t['start_time']),
finishtime=str(t['finish_time']),
filename=fpath,
student_id=t['student']['number'],
state=t['state'],
comment=''))
s.add_all([Question(
ref=q['ref'],
grade=q['grade'],
starttime=str(t['start_time']),
finishtime=str(t['finish_time']),
student_id=t['student']['number'],
test_id=t['ref']) for q in t['questions'] if 'grade' in q])
s.commit()
logger.info(f'Student {uid}: finished test.')
return grade
# -----------------------------------------------------------------------
def giveup_test(self, uid):
t = self.online[uid]['test']
t.giveup()
# save JSON with the test
# fname = ' -- '.join((t['student']['number'], t['ref'], str(t['finish_time']))) + '.json'
# fpath = path.abspath(path.join(t['answers_dir'], fname))
fname = ' -- '.join((t['student']['number'], t['ref'], str(t['finish_time']))) + '.json'
fpath = path.join(t['answers_dir'], fname)
t.save_json(fpath)
# insert test into database
with self.db_session() as s:
s.add(Test(
ref=t['ref'],
title=t['title'],
grade=t['grade'],
starttime=str(t['start_time']),
finishtime=str(t['finish_time']),
filename=fpath,
student_id=t['student']['number'],
state=t['state'],
comment=''))
s.commit()
logger.info(f'Student {uid}: gave up.')
return t
# -----------------------------------------------------------------------
# --- helpers (getters)
def get_student_name(self, uid):
return self.online[uid]['student']['name']
def get_student_test(self, uid, default=None):
return self.online[uid].get('test', default)
def get_questions_path(self):
return self.testfactory['questions_dir']
def get_student_grades_from_all_tests(self, uid):
with self.db_session() as s:
r = s.query(Test).filter_by(student_id=uid).order_by(Test.finishtime).all()
return [(t.title, t.grade, t.finishtime) for t in r]
def get_json_filename_of_test(self, test_id):
with self.db_session() as s:
return s.query(Test.filename).filter_by(id=test_id).one_or_none()[0]
def get_online_students(self):
# [('uid', 'name', 'starttime')]
return [(k, v['student']['name'], str(v.get('test', {}).get('start_time', '---'))) for k,v in self.online.items() if k != '0']
def get_offline_students(self):
# list of ('uid', 'name') sorted by uid
return [u[:2] for u in self.get_all_students() if u[0] not in self.online]
def get_all_students(self):
# list of all ('uid', 'name', 'password') sorted by uid
with self.db_session() as s:
r = s.query(Student).all()
return sorted(((u.id, u.name, u.password) for u in r if u.id != '0'), key=lambda k: k[0])
def get_student_grades_from_test(self, uid, testid):
with self.db_session() as s:
r = s.query(Test).filter_by(student_id=uid).filter_by(ref=testid).all()
return [(u.grade, u.finishtime, u.id) for u in r]
def get_students_state(self):
# [{
# 'uid' : '12345'
# 'name' : 'John Smith',
# 'start_time': '',
# 'grades' : [10.2, 13.1],
# ...
# }]
l = []
for u in self.get_all_students():
uid, name, pw = u
l.append({
'uid': uid,
'name': name,
'allowed': uid in self.allowed,
'online': uid in self.online,
'start_time': self.online.get(uid, {}).get('test', {}).get('start_time',''),
'password_defined': pw != '',
'grades': self.get_student_grades_from_test(uid, self.testfactory['ref']),
'focus': self.online.get(uid, {}).get('student', {}).get('focus', True), # FIXME
})
return l
def get_allowed_students(self):
# set of 'uid' allowed to login
return self.allowed
def get_file(self, uid, ref, key):
# return filename corresponding to (uid, ref, name) if declared in the question
t = self.get_test(uid)
for q in t['questions']:
if q['ref'] == ref and key in q['files']:
return path.abspath(path.join(q['path'], q['files'][key]))
# --- helpers (change state)
def allow_student(self, uid):
self.allowed.add(uid)
logger.info(f'Student {uid}: allowed to login.')
def deny_student(self, uid):
self.allowed.discard(uid)
logger.info(f'Student {uid}: denied to login')
def reset_password(self, uid):
with self.db_session() as s:
u = s.query(Student).filter(Student.id == uid).update({'password': ''})
s.commit()
logger.info(f'Student {uid}: password reset')
def insert_new_student(self, uid, name):
try:
with self.db_session() as s:
s.add(Student(id=uid, name=name, password=''))
s.commit()
except Exception:
logger.error(f'Insert failed: student {uid} already exists.')
else:
logger.info(f'New student inserted: {uid}, {name}')
def set_student_focus(self, uid, value):
self.online[uid]['student']['focus'] = value