From a21ba300836f915828e40d70fa868a4ca77d10ef Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Wed, 19 Feb 2025 15:33:44 +0100
Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=A6=BA=20reintroduce=20duplicate?=
 =?UTF-8?q?=20referee=20indication=20error?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 scipost_django/submissions/forms/__init__.py | 44 +++++++++-----------
 scipost_django/submissions/views/__init__.py |  5 ++-
 2 files changed, 24 insertions(+), 25 deletions(-)

diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py
index 99cdee7c0..a592e3a83 100644
--- a/scipost_django/submissions/forms/__init__.py
+++ b/scipost_django/submissions/forms/__init__.py
@@ -4229,32 +4229,28 @@ class RefereeIndicationForm(forms.ModelForm):
                     "The reason is too short, please provide a more detailed explanation.",
                 )
 
-        return cleaned_data
-
-    def save(self, commit=True):
-        if not commit:
-            return RefereeIndication(
-                submission=self.submission,
-                referee=self.cleaned_data.get("referee"),
-                indicated_by=self.profile,
-                indication=self.cleaned_data.get("indication"),
-                reason=self.cleaned_data.get("reason"),
-                first_name=self.cleaned_data.get("first_name"),
-                last_name=self.cleaned_data.get("last_name"),
-                affiliation=self.cleaned_data.get("affiliation"),
-            )
-
-        indication, created = RefereeIndication.objects.update_or_create(
+        # Check if the referee has already been indicated by the user
+        same_indications = RefereeIndication.objects.filter(
             submission=self.submission,
-            referee=self.cleaned_data.get("referee"),
+            referee=referee,
             indicated_by=self.profile,
-            defaults={
-                "indication": self.cleaned_data.get("indication"),
-                "reason": self.cleaned_data.get("reason"),
-                "first_name": self.cleaned_data.get("first_name"),
-                "last_name": self.cleaned_data.get("last_name"),
-                "affiliation": self.cleaned_data.get("affiliation"),
-            },
         )
+        # If the indication already exists and is being updated, exclude it from the check
+        if previous_indication := cleaned_data.get("id"):
+            same_indications = same_indications.exclude(id=previous_indication.id)
+        if same_indications.exists():
+            self.add_error(
+                None,
+                "You have already indicated this referee for this submission.",
+            )
+
+        return cleaned_data
 
+    def save(self, commit=True):
+        indication = super().save(commit=False)
+        indication.submission = self.submission
+        indication.indicated_by = self.profile
+
+        if commit:
+            indication.save()
         return indication
diff --git a/scipost_django/submissions/views/__init__.py b/scipost_django/submissions/views/__init__.py
index 3af2de875..c03067298 100644
--- a/scipost_django/submissions/views/__init__.py
+++ b/scipost_django/submissions/views/__init__.py
@@ -3854,7 +3854,10 @@ def _hx_referee_indication_table(request, identifier_w_vn_nr, profile=None):
         raise PermissionDenied("You must be logged in to view this page.")
 
     referee_indications = (
-        RefereeIndication.objects.all().for_submission(submission).visible_by(profile)
+        RefereeIndication.objects.all()
+        .for_submission(submission)
+        .visible_by(profile)
+        .order_by("-indication", "indicated_by", "referee__last_name", "last_name")
     )
 
     #! Refactor: This is a bit of a hack to avoid having to add a new permission
-- 
GitLab