Commit 785ca6d2e7a2008124d506911b640940049cd486
1 parent
02299744
Exists in
master
and in
1 other branch
- improved many sqlalchemy queries in app.py
Showing
1 changed file
with
9 additions
and
35 deletions
Show diff stats
app.py
| ... | ... | @@ -219,47 +219,25 @@ class App(object): |
| 219 | 219 | return self.testfactory['questions_dir'] |
| 220 | 220 | def get_student_grades_from_all_tests(self, uid): |
| 221 | 221 | with self.db_session() as s: |
| 222 | - # r = s.query(Test).filter_by(student_id=uid).order_by(Test.finishtime).all() | |
| 223 | - # return [(t.title, t.grade, t.finishtime) for t in r] | |
| 224 | - print('here') | |
| 225 | - return s.query(Test.title, Test.grade, Test.finishtime).filter_by(student_id=uid).order_by(Test.finishtime).all() | |
| 222 | + return s.query(Test.title, Test.grade, Test.finishtime).filter_by(student_id=uid).order_by(Test.finishtime) | |
| 226 | 223 | |
| 227 | 224 | def get_json_filename_of_test(self, test_id): |
| 228 | 225 | with self.db_session() as s: |
| 229 | - return s.query(Test.filename).filter_by(id=test_id).one_or_none()[0] | |
| 230 | - def get_online_students(self): | |
| 231 | - # [('uid', 'name', 'starttime')] | |
| 232 | - return [(k, v['student']['name'], str(v.get('test', {}).get('start_time', '---'))) for k,v in self.online.items() if k != '0'] | |
| 226 | + return s.query(Test.filename).filter_by(id=test_id).scalar() | |
| 233 | 227 | |
| 234 | - def get_offline_students(self): | |
| 235 | - # list of ('uid', 'name') sorted by uid | |
| 236 | - return [u[:2] for u in self.get_all_students() if u[0] not in self.online] | |
| 228 | + def get_online_students(self): # [('uid', 'name', 'starttime')] | |
| 229 | + return [(k, v['student']['name'], str(v.get('test', {}).get('start_time', '---'))) for k,v in self.online.items() if k != '0'] | |
| 237 | 230 | |
| 238 | 231 | def get_all_students(self): |
| 239 | - # list of all ('uid', 'name', 'password') sorted by uid | |
| 240 | 232 | with self.db_session() as s: |
| 241 | - r = s.query(Student).all() | |
| 242 | - return sorted(((u.id, u.name, u.password) for u in r if u.id != '0'), key=lambda k: k[0]) | |
| 233 | + return s.query(Student.id, Student.name, Student.password).filter(Student.id!='0').order_by(Student.id) | |
| 243 | 234 | |
| 244 | 235 | def get_student_grades_from_test(self, uid, testid): |
| 245 | 236 | with self.db_session() as s: |
| 246 | - r = s.query(Test).filter_by(student_id=uid).filter_by(ref=testid).all() | |
| 247 | - return [(u.grade, u.finishtime, u.id) for u in r] | |
| 248 | - | |
| 249 | - | |
| 237 | + return s.query(Test.grade, Test.finishtime, Test.id).filter_by(student_id=uid).filter_by(ref=testid).all() | |
| 250 | 238 | |
| 251 | 239 | def get_students_state(self): |
| 252 | - # [{ | |
| 253 | - # 'uid' : '12345' | |
| 254 | - # 'name' : 'John Smith', | |
| 255 | - # 'start_time': '', | |
| 256 | - # 'grades' : [10.2, 13.1], | |
| 257 | - # ... | |
| 258 | - # }] | |
| 259 | - l = [] | |
| 260 | - for u in self.get_all_students(): | |
| 261 | - uid, name, pw = u | |
| 262 | - l.append({ | |
| 240 | + return [{ | |
| 263 | 241 | 'uid': uid, |
| 264 | 242 | 'name': name, |
| 265 | 243 | 'allowed': uid in self.allowed, |
| ... | ... | @@ -267,9 +245,8 @@ class App(object): |
| 267 | 245 | 'start_time': self.online.get(uid, {}).get('test', {}).get('start_time',''), |
| 268 | 246 | 'password_defined': pw != '', |
| 269 | 247 | 'grades': self.get_student_grades_from_test(uid, self.testfactory['ref']), |
| 270 | - 'focus': self.online.get(uid, {}).get('student', {}).get('focus', True), # FIXME | |
| 271 | - }) | |
| 272 | - return l | |
| 248 | + # 'focus': self.online.get(uid, {}).get('student', {}).get('focus', True), # FIXME | |
| 249 | + } for uid, name, pw in self.get_all_students()] | |
| 273 | 250 | |
| 274 | 251 | def get_allowed_students(self): |
| 275 | 252 | # set of 'uid' allowed to login |
| ... | ... | @@ -307,6 +284,3 @@ class App(object): |
| 307 | 284 | logger.error(f'Insert failed: student {uid} already exists.') |
| 308 | 285 | else: |
| 309 | 286 | logger.info(f'New student inserted: {uid}, {name}') |
| 310 | - | |
| 311 | - def set_student_focus(self, uid, value): | |
| 312 | - self.online[uid]['student']['focus'] = value | ... | ... |