diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py index 99cdee7c08d3878090ec91c84ccd92d972489790..a592e3a83c5b9041938e34871d310a245fdc7802 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 3af2de875455c9b1cf6d878b3bdd8e2733ab3f39..c030672986467d8d0231b94e84b23488fffe1553 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