From 172b8f1ec82793aa3216de5de1df3ae1539c7402 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Fri, 8 Mar 2024 17:54:17 +0100
Subject: [PATCH] allow re-sending cancelled referee invitations

---
 scipost_django/submissions/forms/__init__.py |  1 +
 scipost_django/submissions/views/__init__.py | 22 +++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py
index a1c42de8c..1eac64e0f 100644
--- a/scipost_django/submissions/forms/__init__.py
+++ b/scipost_django/submissions/forms/__init__.py
@@ -2431,6 +2431,7 @@ class InviteRefereeSearchFrom(forms.Form):
                     RefereeInvitation.objects.filter(
                         profile=OuterRef("id"),
                         submission=self.submission,
+                        cancelled=False,
                     )
                 )
             )
diff --git a/scipost_django/submissions/views/__init__.py b/scipost_django/submissions/views/__init__.py
index 756e1055d..509206b25 100644
--- a/scipost_django/submissions/views/__init__.py
+++ b/scipost_django/submissions/views/__init__.py
@@ -618,7 +618,9 @@ def submission_detail(request, identifier_w_vn_nr):
     # Check if Contributor is author of the Submission
     is_author = check_verified_author(submission, request.user)
     is_author_unchecked = check_unverified_author(submission, request.user)
-    is_submission_fellow = submission.fellows.filter(contributor__user=request.user.id).exists()
+    is_submission_fellow = submission.fellows.filter(
+        contributor__user=request.user.id
+    ).exists()
 
     if not submission.visible_public and not is_author:
         if not request.user.is_authenticated:
@@ -1372,9 +1374,13 @@ def invite_referee(
         )
 
     # Guard against already invited referees
-    if RefereeInvitation.objects.filter(
-        profile=profile, submission=submission
-    ).exists():
+    if (
+        RefereeInvitation.objects.filter(
+            profile=profile, submission=submission, cancelled=False
+        )
+        .exclude(email_address=profile_email)
+        .exists()
+    ):
         messages.error(
             request,
             "This referee has already been invited.",
@@ -1430,6 +1436,9 @@ def invite_referee(
         key = get_new_secrets_key()
         referee_invitation.invitation_key = key
         referee_invitation.save()
+    elif referee_invitation.cancelled:
+        referee_invitation.cancelled = False
+        referee_invitation.save()
 
     registration_invitation = None
     has_agreed_to_previous_invitation = RefereeInvitation.objects.filter(
@@ -1518,7 +1527,7 @@ def _hx_quick_invite_referee(request, identifier_w_vn_nr, profile_id):
 
     # Guard against already invited referees
     if RefereeInvitation.objects.filter(
-        profile=profile, submission=submission
+        profile=profile, submission=submission, cancelled=False
     ).exists():
         return HTMXResponse(
             "This referee has already been invited.",
@@ -1558,6 +1567,9 @@ def _hx_quick_invite_referee(request, identifier_w_vn_nr, profile_id):
         key = get_new_secrets_key()
         referee_invitation.invitation_key = key
         referee_invitation.save()
+    elif referee_invitation.cancelled:
+        referee_invitation.cancelled = False
+        referee_invitation.save()
 
     registration_invitation = None
     has_agreed_to_previous_invitation = RefereeInvitation.objects.filter(
-- 
GitLab