From 14c0da6ab25ccf398626694af0fb8a38c6ef47fd Mon Sep 17 00:00:00 2001
From: Anton Akhmerov <anton.akhmerov@gmail.com>
Date: Mon, 26 Mar 2018 01:42:32 +0200
Subject: [PATCH] implement solution images endpoint

---
 zesje/api.py              |  6 +++++
 zesje/resources/images.py | 47 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 zesje/resources/images.py

diff --git a/zesje/api.py b/zesje/api.py
index 3c33101e3..84b630313 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 import images
 from .resources.problems import Problems
 from .resources.feedback import Feedback
 
@@ -41,3 +42,8 @@ api_bp.add_url_rule(
     'signature',
     signature.get,
 )
+api_bp.add_url_rule(
+    '/images/solutions/<int:exam_id>/<int:problem_id>/<int:submission_id>',
+    'solution',
+    images.get,
+)
diff --git a/zesje/resources/images.py b/zesje/resources/images.py
new file mode 100644
index 000000000..ed8737fc7
--- /dev/null
+++ b/zesje/resources/images.py
@@ -0,0 +1,47 @@
+import os
+from flask import abort, Response, current_app as app
+from pony import orm
+
+from ..helpers import yaml_helper, image_helper
+from ..models import Exam, Submission, Solution, Problem
+
+
+@orm.db_session
+def get(exam_id, problem_id, submission_id):
+    """get student signature for the given submission.
+
+    Parameters
+    ----------
+    exam_id : int
+    problem_id : int
+    submission_id : int
+        The copy number of the submission. This uniquely identifies
+        the submission *within a given exam*.
+
+    Returns
+    -------
+    Image (JPEG mimetype)
+    """
+    try:
+        exam = Exam[exam_id]
+        sub = Submission.get(exam=exam, copy_number=submission_id)
+        name = Problem[problem_id].name
+    except (KeyError, ValueError):
+        abort(404)
+
+    data_dir = app.config['DATA_DIRECTORY']
+    yaml_abspath = os.path.join(data_dir, sub.exam.yaml_path)
+    *_, widgets = yaml_helper.parse(yaml_helper.read(yaml_abspath))
+    problem_metadata = widgets.loc[name]
+    page = f'page{int(problem_metadata.page)}.'
+    page_path = (
+        sub.pages
+        .select(lambda p: page in p.path)
+        .first().path
+    )
+    image = image_helper.get_widget_image(
+        page_path,
+        problem_metadata
+    )
+
+    return Response(image, 200, mimetype='image/jpeg')
-- 
GitLab