From 8d7a626a4e2549b48cbf821cb32048675d3b3091 Mon Sep 17 00:00:00 2001
From: Ruben Young On <r.d.youngon@student.tudelft.nl>
Date: Sat, 25 May 2019 13:38:56 +0200
Subject: [PATCH] Rewritten doc, update_database returns submission and exam,
 pregrading now checks for specific submission

---
 zesje/pregrader.py | 18 +++++++++++-------
 zesje/scans.py     | 14 +++++++++-----
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/zesje/pregrader.py b/zesje/pregrader.py
index 2175ee9e..c5907ddc 100644
--- a/zesje/pregrader.py
+++ b/zesje/pregrader.py
@@ -5,16 +5,20 @@ from zesje.database import db, Solution
 from zesje.images import guess_dpi, get_box, fix_corner_markers
 
 
-def add_feedback_to_solution(exam, page, page_img, corner_keypoints):
+def add_feedback_to_solution(sub, exam, page, page_img, corner_keypoints):
     """
     Adds the multiple choice options that are identified as marked as a feedback option to a solution
 
-    Params
+    Parameters
     ------
-    exam: the current exam
-    page_img: image of the page
-    barcode: data from the barcode on the page
-    corner_keypoints: locations of the corner keypoints
+    sub : Submission
+        the current submission
+    exam : Exam
+        the current exam
+    page_img : Image
+        image of the page
+    corner_keypoints : array
+        locations of the corner keypoints as (x, y) tuples
     """
     problems_on_page = [problem for problem in exam.problems if problem.widget.page == page]
 
@@ -25,7 +29,7 @@ def add_feedback_to_solution(exam, page, page_img, corner_keypoints):
     top_left_point = sorted(fixed_corner_keypoints, key=lambda x: x[0])[0] if fixed_corner_keypoints else []
 
     for problem in problems_on_page:
-        sol = Solution.query.filter(Solution.problem_id == problem.id).one_or_none()
+        sol = Solution.query.filter(Solution.problem_id == problem.id, Solution.submission_id == sub.id).one_or_none()
 
         for mc_option in problem.mc_options:
             box = (mc_option.x, mc_option.y)
diff --git a/zesje/scans.py b/zesje/scans.py
index e82223d0..ac0be8e0 100644
--- a/zesje/scans.py
+++ b/zesje/scans.py
@@ -338,8 +338,8 @@ def process_page(image_data, exam_config, output_dir=None, strict=False):
     else:
         return True, "Testing, image not saved and database not updated."
 
-    exam = update_database(image_path, barcode)
-    add_feedback_to_solution(exam, barcode.page, image_array, corner_keypoints)
+    sub, exam = update_database(image_path, barcode)
+    add_feedback_to_solution(sub, exam, barcode.page, image_array, corner_keypoints)
 
     if barcode.page == 0:
         description = guess_student(
@@ -387,8 +387,12 @@ def update_database(image_path, barcode):
 
     Returns
     -------
-    signature_validated : bool
-        If the corresponding submission has a validated signature.
+    sub, exam where
+
+    sub : Submission
+        the current submission
+    exam : Exam
+        the current exam
     """
     exam = Exam.query.filter(Exam.token == barcode.token).first()
     if exam is None:
@@ -408,7 +412,7 @@ def update_database(image_path, barcode):
 
     db.session.commit()
 
-    return exam
+    return sub, exam
 
 
 def decode_barcode(image, exam_config):
-- 
GitLab