diff --git a/zesje/api.py b/zesje/api.py index 737ae7ac38c76aad21eabce368a28042f1fc1f6b..6b447847e7ec09e4e52b026a77b202db8b1243aa 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 0000000000000000000000000000000000000000..ad52fbdfac8a09c3ab9ba50035b4e3f83ad9390e --- /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) + ]