From bde313370722d5896ee1aeacedc4f2eaf872b68d Mon Sep 17 00:00:00 2001
From: Ruben Young On <r.d.youngon@student.tudelft.nl>
Date: Sat, 25 May 2019 13:05:06 +0200
Subject: [PATCH] Pregrading now works

---
 zesje/pregrader.py | 14 ++++++++------
 zesje/scans.py     |  6 +++---
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/zesje/pregrader.py b/zesje/pregrader.py
index 42b45410..22500020 100644
--- a/zesje/pregrader.py
+++ b/zesje/pregrader.py
@@ -17,18 +17,18 @@ from zesje.database import db, Solution, ProblemWidget
 from zesje.images import guess_dpi, get_box, fix_corner_markers
 
 
-def add_feedback_to_solution(page, page_img, corner_keypoints):
+def add_feedback_to_solution(exam, page, page_img, corner_keypoints):
     """
     Adds the multiple choice options that are identified as marked as a feedback option to a solution
 
     Params
     ------
+    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
     """
-    problem_widgets = ProblemWidget.query.filter(ProblemWidget.page == page).all()
-    problems_on_page = [widget.problem for widget in problem_widgets]
+    problems_on_page = [problem for problem in exam.problems if problem.widget.page == page]
 
     fixed_corner_keypoints = fix_corner_markers(corner_keypoints, page_img.shape)
 
@@ -43,13 +43,15 @@ def add_feedback_to_solution(page, page_img, corner_keypoints):
             box = (mc_option.x, mc_option.y)
 
             if box_is_filled(box, page_img, top_left_point):
-                sol.feedback.append(mc_option.feedback)
-                db.session.commit()
+                feedback = mc_option.feedback
 
                 if mc_option.label:
-                    sol.feedback.text = mc_option.label
+                    feedback.text = mc_option.label
                     db.session.commit()
 
+                sol.feedback.append(feedback)
+                db.session.commit()
+
 
 def box_is_filled(box, page_img, corner_keypoints, marker_margin=72/2.54, threshold=225, cut_padding=0.1, box_size=11):
     """
diff --git a/zesje/scans.py b/zesje/scans.py
index a7436eae..e82223d0 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."
 
-    update_database(image_path, barcode)
-    add_feedback_to_solution(barcode.page, image_array, corner_keypoints)
+    exam = update_database(image_path, barcode)
+    add_feedback_to_solution(exam, barcode.page, image_array, corner_keypoints)
 
     if barcode.page == 0:
         description = guess_student(
@@ -408,7 +408,7 @@ def update_database(image_path, barcode):
 
     db.session.commit()
 
-    return sub, exam
+    return exam
 
 
 def decode_barcode(image, exam_config):
-- 
GitLab