From 9fc7da088cbe56e3d96074352c9ccc06d4dd3991 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Mon, 10 Mar 2025 12:09:09 +0100
Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20intended=20delivery?=
 =?UTF-8?q?=20dates=20not=20being=20saved?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Saves intended delivery dates. Allows changing of dates issued after the reporting deadline. Fails changes when report is fulfilled or invitation cancelled.
---
 scipost_django/submissions/forms/__init__.py | 23 +++++++++++++++++---
 scipost_django/submissions/views/__init__.py | 10 +++++++--
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py
index 99cdee7c0..881d2ed57 100644
--- a/scipost_django/submissions/forms/__init__.py
+++ b/scipost_django/submissions/forms/__init__.py
@@ -2912,7 +2912,8 @@ class ConsiderRefereeInvitationForm(forms.Form):
         help_text=("The date by which you intend to deliver your report."),
     )
     refusal_reason = forms.ChoiceField(
-        choices=[(None, "")] + list(EditorialAssignment.REFUSAL_REASONS), required=False
+        choices=[(None, "-" * 9)] + list(EditorialAssignment.REFUSAL_REASONS),
+        required=False,
     )
     other_refusal_reason = forms.CharField(
         required=False,
@@ -2967,8 +2968,16 @@ class ConsiderRefereeInvitationForm(forms.Form):
                     "Please select an intended delivery date for your report.",
                 )
 
-    def save(self):
-        super().save()
+    def save(self, commit=True):
+        accepted = self.cleaned_data.get("accept", None)
+        intended_delivery_date = self.cleaned_data.get("intended_delivery_date", None)
+        refusal_reason = self.cleaned_data.get("refusal_reason", None)
+        other_refusal_reason = self.cleaned_data.get("other_refusal_reason", None)
+
+        self.invitation.accepted = accepted
+        self.invitation.intended_delivery_date = intended_delivery_date
+        self.invitation.refusal_reason = refusal_reason
+        self.invitation.other_refusal_reason = other_refusal_reason
 
         self.invitation.submission.add_event_for_eic(
             f"Referee {self.invitation.referee.full_name} set intended report delivery date to {self.invitation.intended_delivery_date}."
@@ -2977,6 +2986,11 @@ class ConsiderRefereeInvitationForm(forms.Form):
             f"A referee has set their intended report delivery date to {self.invitation.intended_delivery_date}."
         )
 
+        if commit:
+            self.invitation.save()
+
+        return self.invitation
+
 
 class ReportIntendedDeliveryForm(forms.ModelForm):
     """
@@ -2987,6 +3001,9 @@ class ReportIntendedDeliveryForm(forms.ModelForm):
     class Meta:
         model = RefereeInvitation()
         fields = ["intended_delivery_date"]
+        widgets = {
+            "intended_delivery_date": forms.DateInput(attrs={"type": "date"}),
+        }
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
diff --git a/scipost_django/submissions/views/__init__.py b/scipost_django/submissions/views/__init__.py
index 3af2de875..255a5db4d 100644
--- a/scipost_django/submissions/views/__init__.py
+++ b/scipost_django/submissions/views/__init__.py
@@ -1893,6 +1893,7 @@ def accept_or_decline_ref_invitations(request, invitation_id=None):
 
     form = ConsiderRefereeInvitationForm(request.POST or None, invitation=invitation)
     if form.is_valid():
+        invitation = form.save(commit=False)
         invitation.date_responded = timezone.now()
         if form.cleaned_data["accept"] == "True":
             invitation.accepted = True
@@ -2070,9 +2071,14 @@ def _hx_report_intended_delivery_form(request, invitation_id):
             "You do not have permission to edit the intended delivery date."
         )
 
-    if invitation.submission.reporting_deadline_has_passed:
+    if invitation.fulfilled:
         return HTMXResponse(
-            "The refereeing deadline has passed. You cannot change the intended delivery date anymore.",
+            "The report has already been delivered. You may not change the intended delivery date.",
+            tag="danger",
+        )
+    if invitation.cancelled:
+        return HTMXResponse(
+            "The invitation has been cancelled. You may not change the intended delivery date.",
             tag="danger",
         )
 
-- 
GitLab