From 698c95b795334dd7598de872755e6ed41c1166b5 Mon Sep 17 00:00:00 2001 From: Joseph Weston <joseph@weston.cloud> Date: Thu, 8 Mar 2018 19:37:27 +0100 Subject: [PATCH] add 'Submissions' resource for getting list of submissions --- zesje/api.py | 4 +++ zesje/resources/submissions.py | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 zesje/resources/submissions.py diff --git a/zesje/api.py b/zesje/api.py index 737ae7ac3..6b447847e 100644 --- a/zesje/api.py +++ b/zesje/api.py @@ -5,6 +5,7 @@ from .resources.graders import Graders from .resources.exams import Exams, ExamConfig from .resources.pdfs import Pdfs from .resources.students import Students +from .resources.submissions import Submissions api_bp = Blueprint(__name__, __name__) @@ -22,3 +23,6 @@ api.add_resource(Exams, '/exams') api.add_resource(ExamConfig, '/exams/<int:exam_id>') api.add_resource(Pdfs, '/pdfs/<int:exam_id>') api.add_resource(Students, '/students') +api.add_resource(Submissions, + '/submissions/<int:exam_id>', + '/submissions/<int:exam_id>/<int:copy_number>') diff --git a/zesje/resources/submissions.py b/zesje/resources/submissions.py new file mode 100644 index 000000000..ad52fbdfa --- /dev/null +++ b/zesje/resources/submissions.py @@ -0,0 +1,47 @@ +from flask_restful import Resource +from pony import orm + +from ..models import Exam, Submission + +class Submissions(Resource): + """Getting a list of submissions, and assigning students to them.""" + + @orm.db_session + def get(self, exam_id, copy_number=None): + """get submissions for the given exam + + Parameters + ---------- + exam_id : int + + Returns + ------- + If 'copy_number' not provided provides a single instance of + (otherwise a list of): + copy_number: int + student_id: int or null + Student that completed this submission, null if not assigned. + validated: bool + True if the assigned student has been validated by a human. + """ + # This makes sure we raise ObjectNotFound if the exam does not exist + exam = Exam[exam_id] + + if copy_number is not None: + s = Submission.get(exam=exam, copy_number=copy_number) + if not s: + raise orm.core.ObjectNotFound(Submission) + return { + 'copy_number': s.copy_number, + 'student_id': s.student.id if s.student else None, + 'validated': s.signature_validated, + } + + return [ + { + 'copy_number': s.copy_number, + 'student': s.student.id if s.student else None, + 'validated': s.signature_validated, + } + for s in Submission.select(lambda s: s.exam == exam) + ] -- GitLab