From 3149272884649503e7207d204a154dc7d2bd98ad Mon Sep 17 00:00:00 2001 From: Anton Akhmerov <anton.akhmerov@gmail.com> Date: Wed, 9 May 2018 19:30:42 +0200 Subject: [PATCH] tell which submissions miss pages --- zesje/resources/submissions.py | 94 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/zesje/resources/submissions.py b/zesje/resources/submissions.py index 7c9e929dc..0acedcbc3 100644 --- a/zesje/resources/submissions.py +++ b/zesje/resources/submissions.py @@ -1,8 +1,45 @@ +import re + from flask_restful import Resource, reqparse from pony import orm from ..models import Exam, Submission, Student +page_match = re.compile(r'.*page(\d+).jpg').match + + +def page_nr(path): + return int(page_match(path).group(1)) + + +def sub_to_data(sub, all_pages): + """Transform a submission into a data structure frontend expects.""" + return { + 'id': sub.copy_number, + 'student': + { + 'id': sub.student.id, + 'firstName': sub.student.first_name, + 'lastName': sub.student.last_name, + 'email': sub.student.email + } if sub.student else None, + 'validated': sub.signature_validated, + 'problems': + [ + { + 'id': sol.problem.id, + 'graded_by': sol.graded_by, + 'graded_at': sol.graded_at.isoformat() if sol.graded_at else None, + 'feedback': [ + fb.id for fb in sol.feedback + ], + 'remark': sol.remarks + } for sol in sub.solutions.order_by(lambda s: s.problem.id) + ], + 'missing_pages': sorted(all_pages - set(page_nr(i) for i in sub.pages.path)), + } + + class Submissions(Resource): """Getting a list of submissions, and assigning students to them.""" @@ -26,62 +63,27 @@ class Submissions(Resource): Student that completed this submission, null if not assigned. validated: bool True if the assigned student has been validated by a human. + problems: list of problems + missing_pages: list of int + pages that are missing from submission """ # This makes sure we raise ObjectNotFound if the exam does not exist exam = Exam[exam_id] + all_pages = set(page_nr(i) for i in exam.submissions.pages.path) + if submission_id is not None: sub = Submission.get(exam=exam, copy_number=submission_id) if not sub: raise orm.core.ObjectNotFound(Submission) - return { - 'id': sub.copy_number, - 'student': - { - 'id': sub.student.id, - 'firstName': sub.student.first_name, - 'lastName': sub.student.last_name, - 'email': sub.student.email - } if sub.student else None, - 'validated': sub.signature_validated, - 'problems': - [ - { - 'id': sol.problem.id, - 'graded_by': sol.graded_by, - 'graded_at': sol.graded_at.isoformat() if sol.graded_at else None, - 'feedback': [ - fb.id for fb in sol.feedback - ], - 'remark': sol.remarks - } for sol in sub.solutions.order_by(lambda s: s.problem.id) - ] - } + + return sub_to_data(sub, all_pages) return [ - { - 'id': sub.copy_number, - 'student': - { - 'id': sub.student.id, - 'firstName': sub.student.first_name, - 'lastName': sub.student.last_name, - 'email': sub.student.email - } if sub.student else None, - 'validated': sub.signature_validated, - 'problems': - [ - { - 'id': sol.problem.id, - 'graded_by': sol.graded_by, - 'graded_at': sol.graded_at.isoformat() if sol.graded_at else None, - 'feedback': [ - fb.id for fb in sol.feedback - ], - 'remark': sol.remarks - } for sol in sub.solutions.order_by(lambda s: s.problem.id) - ] - } for sub in Submission.select(lambda s: s.exam == exam).order_by(lambda s: s.copy_number) + sub_to_data(sub, all_pages) for sub + in (Submission + .select(lambda s: s.exam == exam) + .order_by(lambda s: s.copy_number)) ] put_parser = reqparse.RequestParser() -- GitLab