diff --git a/zesje/api.py b/zesje/api.py index 7aabe96565c1231829325ef9d83d88b480f2bbc1..2d1d4ce5a056e9ce329b612d56a58f20867016a0 100644 --- a/zesje/api.py +++ b/zesje/api.py @@ -7,6 +7,7 @@ from .resources.pdfs import Pdfs from .resources.students import Students from .resources.submissions import Submissions from .resources import signature +from .resources.problems import Problems api_bp = Blueprint(__name__, __name__) @@ -27,6 +28,7 @@ api.add_resource(Students, '/students', '/students/<int:student_id>') api.add_resource(Submissions, '/submissions/<int:exam_id>', '/submissions/<int:exam_id>/<int:submission_id>') +api.add_resource(Problems, '/problems/<int:exam_id>') # Other resources that don't return JSON # It is possible to get flask_restful to work with these, but not diff --git a/zesje/resources/problems.py b/zesje/resources/problems.py new file mode 100644 index 0000000000000000000000000000000000000000..34ebaac49e8e5fa21a11dfd6bb19c6bf88c31a5a --- /dev/null +++ b/zesje/resources/problems.py @@ -0,0 +1,31 @@ +""" REST api for problems """ + +from flask_restful import Resource + +from pony import orm + +from ..models import Exam, Problem + +class Problems(Resource): + """ List of problems associated with a particular exam_id """ + + @orm.db_session + def get(self, exam_id): + """get list of problems of exam + + Returns + ------- + list of: + id: int + name: str + """ + + exam = Exam[exam_id] + + return [ + { + 'id': problem.id, + 'name': problem.name + } + for problem in Problem.select(lambda p: p.exam == exam) + ]