From ae60a777b68913e2a1d4a338cd80b02e8ae3ebe2 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Thu, 22 Feb 2024 12:14:19 +0100
Subject: [PATCH] refactor invite_referee to accept profile email

---
 .../pool/_referee_invitations.html            |  2 +-
 scipost_django/submissions/urls/__init__.py   |  2 +-
 scipost_django/submissions/views/__init__.py  | 40 +++++++++++--------
 3 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/scipost_django/submissions/templates/submissions/pool/_referee_invitations.html b/scipost_django/submissions/templates/submissions/pool/_referee_invitations.html
index 1121256e6..766635bf7 100644
--- a/scipost_django/submissions/templates/submissions/pool/_referee_invitations.html
+++ b/scipost_django/submissions/templates/submissions/pool/_referee_invitations.html
@@ -41,7 +41,7 @@
       {% if not invitation.cancelled %}
         <span class="text-danger">Invitation email not sent!!</span>
         <br>
-        <a class="btn btn-sm btn-danger text-white" href="{% url 'submissions:invite_referee' identifier_w_vn_nr=invitation.submission.preprint.identifier_w_vn_nr profile_id=invitation.profile.id auto_reminders_allowed=invitation.auto_reminders_allowed|yesno:"1,0" %}">
+        <a class="btn btn-sm btn-danger text-white" href="{% url 'submissions:invite_referee' identifier_w_vn_nr=invitation.submission.preprint.identifier_w_vn_nr profile_id=invitation.profile.id profile_email=invitation.email_address auto_reminders_allowed=invitation.auto_reminders_allowed %}">
           {% include 'bi/arrow-right.html' %}&nbsp;Resend
         </a>
       {% endif %}
diff --git a/scipost_django/submissions/urls/__init__.py b/scipost_django/submissions/urls/__init__.py
index dc150bd2d..ee41f96de 100644
--- a/scipost_django/submissions/urls/__init__.py
+++ b/scipost_django/submissions/urls/__init__.py
@@ -309,7 +309,7 @@ urlpatterns = [
         name="add_referee_profile",
     ),
     path(
-        "invite_referee/<identifier:identifier_w_vn_nr>/<int:profile_id>/<int:auto_reminders_allowed>",
+        "invite_referee/<identifier:identifier_w_vn_nr>/<int:profile_id>/<str:profile_email>/auto_remind/<str:auto_reminders_allowed>",
         views.invite_referee,
         name="invite_referee",
     ),
diff --git a/scipost_django/submissions/views/__init__.py b/scipost_django/submissions/views/__init__.py
index 00eea54e9..92602d9b6 100644
--- a/scipost_django/submissions/views/__init__.py
+++ b/scipost_django/submissions/views/__init__.py
@@ -1220,9 +1220,11 @@ def add_referee_profile(request, identifier_w_vn_nr):
 @login_required
 @fellowship_or_admin_required()
 @transaction.atomic
-def invite_referee(request, identifier_w_vn_nr, profile_id, auto_reminders_allowed):
+def invite_referee(
+    request, identifier_w_vn_nr, profile_id, profile_email, auto_reminders_allowed
+):
     """
-    Invite a referee linked to a Profile.
+    Invite a referee linked to a Profile using their profile email.
     If the Profile has a Contributor object, a simple invitation is sent.
     If there is no associated Contributor, a registration invitation is included.
     """
@@ -1231,6 +1233,13 @@ def invite_referee(request, identifier_w_vn_nr, profile_id, auto_reminders_allow
         preprint__identifier_w_vn_nr=identifier_w_vn_nr,
     )
     profile = get_object_or_404(Profile, pk=profile_id)
+    auto_reminders_allowed = auto_reminders_allowed == "True"
+
+    # We cannot proceed if the Profile has no email address
+    # or if the email address is not linked to the Profile.
+    profile_email = profile.emails.get(email=profile_email)
+    if profile_email is None:
+        raise Http404
 
     contributor = None
     if hasattr(profile, "contributor") and profile.contributor:
@@ -1243,7 +1252,7 @@ def invite_referee(request, identifier_w_vn_nr, profile_id, auto_reminders_allow
         title=profile.title if profile.title else TITLE_DR,
         first_name=profile.first_name,
         last_name=profile.last_name,
-        email_address=profile.email,
+        email_address=profile_email.email,
         auto_reminders_allowed=auto_reminders_allowed,
         invited_by=request.user.contributor,
     )
@@ -1269,19 +1278,18 @@ def invite_referee(request, identifier_w_vn_nr, profile_id, auto_reminders_allow
             invitation=referee_invitation,
         )
     else:  # no Contributor, so registration invitation
-        (
-            registration_invitation,
-            reginv_created,
-        ) = RegistrationInvitation.objects.get_or_create(
-            profile=profile,
-            title=profile.title if profile.title else TITLE_DR,
-            first_name=profile.first_name,
-            last_name=profile.last_name,
-            email=profile.email,
-            invitation_type=INVITATION_REFEREEING,
-            created_by=request.user,
-            invited_by=request.user,
-            invitation_key=referee_invitation.invitation_key,
+        registration_invitation, reginv_created = (
+            RegistrationInvitation.objects.get_or_create(
+                profile=profile,
+                title=profile.title if profile.title else TITLE_DR,
+                first_name=profile.first_name,
+                last_name=profile.last_name,
+                email=profile_email.email,
+                invitation_type=INVITATION_REFEREEING,
+                created_by=request.user,
+                invited_by=request.user,
+                invitation_key=referee_invitation.invitation_key,
+            )
         )
         mail_request = MailEditorSubview(
             request,
-- 
GitLab