From ad3af33f929acf16c0254010edaf5f87d9c9f5da Mon Sep 17 00:00:00 2001
From: janmax <mail-github@jmx.io>
Date: Fri, 5 May 2017 11:25:33 +0200
Subject: [PATCH] Change definition of submissiion pool and fixed error
 messages

---
 core/models.py                       | 28 ++++++++++++++++++----------
 core/templates/core/message_box.html |  2 +-
 core/views/feedback.py               | 17 +++++++++++------
 3 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/core/models.py b/core/models.py
index 37e8380b..8be60cc1 100644
--- a/core/models.py
+++ b/core/models.py
@@ -50,7 +50,6 @@ from django.db.models import Q
 
 SLUG_LENGTH = 16
 
-
 def random_slug():
     return ''.join(sample(ascii_lowercase, SLUG_LENGTH))
 
@@ -125,7 +124,7 @@ class Submission(models.Model):
         )
 
     @classmethod
-    def assign_tutor(cls, tutor, type_slug=None) -> bool:
+    def assign_tutor(cls, tutor, slug=None) -> bool:
         """Assigns a tutor to a submission
 
         A submission is not assigned to the specified tutor in the case
@@ -144,18 +143,27 @@ class Submission(models.Model):
         if unfinished:
             return False
 
-        ready = cls.objects.filter(feedback__isnull=True)
+        candidates = cls.objects.filter(
+            (
+                  Q(feedback__isnull=True)
+                | Q(feedback__origin=Feedback.DID_NOT_COMPILE)
+                | Q(feedback__origin=Feedback.COULD_NOT_LINK)
+            )
+            & ~Q(feedback__of_tutor=tutor)
+        )
+
+        # we want a submission of a specific type
+        if slug:
+            candidates = candidates.filter(type__slug=slug)
 
-        # we do not want this tutor to correct the same submission twice
-        if type_slug:
-            ready = ready.filter(type__slug=type_slug)
-        ready = ready.exclude(feedback__of_tutor=tutor)
-        if not ready:
+        # we couldn't find any submission to correct
+        if not candidates:
             return False
 
-        submission = ready[0]
-        feedback = Feedback()
+        submission = candidates[0]
+        feedback = submission.feedback if hasattr(submission, 'feedback') else Feedback()
         feedback.origin = Feedback.MANUAL
+        feedback.status = Feedback.EDITABLE
         feedback.of_tutor = tutor
         feedback.of_submission = submission
         feedback.save()
diff --git a/core/templates/core/message_box.html b/core/templates/core/message_box.html
index 10f02995..8bef5525 100644
--- a/core/templates/core/message_box.html
+++ b/core/templates/core/message_box.html
@@ -1,5 +1,5 @@
 {# This is where all the messages pop up #}
-{% if messages %}
+{% if messages or form.errors %}
 <div class="row">
   <div class="col my-2">
     {% for message in messages %}
diff --git a/core/views/feedback.py b/core/views/feedback.py
index 62c4992f..767e14d2 100644
--- a/core/views/feedback.py
+++ b/core/views/feedback.py
@@ -72,13 +72,18 @@ class FeedbackEdit(UpdateView):
         # ugly needs patch
         if 'Next' in self.request.POST['update']:
             if in_groups(self.request.user, ('Reviewers',)):
-                needs_review = Feedback.objects.filter(status=Feedback.NEEDS_REVIEW, of_submission__type=form.instance.of_submission.type)
-                needs_review = needs_review[0] if needs_review else None
+
+                needs_review = Feedback.objects.filter(
+                    status=Feedback.NEEDS_REVIEW,
+                    of_submission__type=form.instance.of_submission.type
+                )
+
                 if needs_review:
-                    return HttpResponseRedirect(reverse('FeedbackEdit', args=(needs_review.slug,)))
-                else:
-                    return HttpResponseRedirect(self.get_success_url())
-            return HttpResponseRedirect(reverse('CreateFeedbackForType', args=(form.instance.of_submission.type.slug,)))
+                    return HttpResponseRedirect(reverse('FeedbackEdit', args=(needs_review[0].slug,)))
+
+            else: # in_groups(self.request.user, ('Tutor',)):
+                return HttpResponseRedirect(reverse('CreateFeedbackForType', args=(form.instance.of_submission.type.slug,)))
+
         return HttpResponseRedirect(self.get_success_url())
 
     def get_context_data(self, **kwargs):
-- 
GitLab