From c8ee015b88094827fc473d6ad192067d18648c4b Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Tue, 5 Mar 2024 13:20:25 +0100 Subject: [PATCH] fix referee invitations using nonprimary emails --- scipost_django/submissions/forms/__init__.py | 11 +++++++- .../_hx_select_referee_table_row.html | 4 +++ scipost_django/submissions/views/__init__.py | 27 ++++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py index 12b8839fc..a1c42de8c 100644 --- a/scipost_django/submissions/forms/__init__.py +++ b/scipost_django/submissions/forms/__init__.py @@ -2420,12 +2420,20 @@ class InviteRefereeSearchFrom(forms.Form): .annotate( has_accepted_previous_invitation=Exists( RefereeInvitation.objects.filter( - referee=OuterRef("contributor"), + profile=OuterRef("id"), submission__thread_hash=self.submission.thread_hash, accepted=True, ).exclude(submission=self.submission) ) ) + .annotate( + already_invited=Exists( + RefereeInvitation.objects.filter( + profile=OuterRef("id"), + submission=self.submission, + ) + ) + ) ) if text := self.cleaned_data.get("text"): @@ -2469,6 +2477,7 @@ class InviteRefereeSearchFrom(forms.Form): profiles = profiles.annotate( can_be_sent_invitation=ExpressionWrapper( Q(emails__isnull=False) + & ~Q(already_invited=True) & Q(accepts_refereeing_requests=True) & ~Q(has_any_competing_interest_with_submission=True) & (Q(is_unavailable=False) | Q(has_accepted_previous_invitation=True)), diff --git a/scipost_django/submissions/templates/submissions/_hx_select_referee_table_row.html b/scipost_django/submissions/templates/submissions/_hx_select_referee_table_row.html index 2f6d097ce..23dc51d2d 100644 --- a/scipost_django/submissions/templates/submissions/_hx_select_referee_table_row.html +++ b/scipost_django/submissions/templates/submissions/_hx_select_referee_table_row.html @@ -58,6 +58,10 @@ hx-swap="outerHTML">Quick Send</button> </div> + {% if profile.already_invited %} + <div class="text-warning">This person has already been invited</div> + {% endif %} + {% if not profile.accepts_refereeing_requests %} <div class="text-danger">This person does not accept refereeing requests</div> {% endif %} diff --git a/scipost_django/submissions/views/__init__.py b/scipost_django/submissions/views/__init__.py index 98ca6a159..13a8c3730 100644 --- a/scipost_django/submissions/views/__init__.py +++ b/scipost_django/submissions/views/__init__.py @@ -1369,6 +1369,21 @@ def invite_referee( ) ) + # Guard against already invited referees + if RefereeInvitation.objects.filter( + profile=profile, submission=submission + ).exists(): + messages.error( + request, + "This referee has already been invited.", + ) + return redirect( + reverse( + "submissions:editorial_page", + kwargs={"identifier_w_vn_nr": identifier_w_vn_nr}, + ) + ) + # Guard against profiles with competing interests if not ( Profile.objects.filter(pk=profile.pk) @@ -1499,6 +1514,15 @@ def _hx_quick_invite_referee(request, identifier_w_vn_nr, profile_id): tag="danger", ) + # Guard against already invited referees + if RefereeInvitation.objects.filter( + profile=profile, submission=submission + ).exists(): + return HTMXResponse( + "This referee has already been invited.", + tag="danger", + ) + # Guard against profiles with competing interests if not ( Profile.objects.filter(pk=profile.pk) @@ -1514,6 +1538,7 @@ def _hx_quick_invite_referee(request, identifier_w_vn_nr, profile_id): if hasattr(profile, "contributor") and profile.contributor: contributor = profile.contributor + primary_or_first_email = profile.emails.order_by("-primary").first().email referee_invitation, created = RefereeInvitation.objects.get_or_create( profile=profile, referee=contributor, @@ -1521,7 +1546,7 @@ def _hx_quick_invite_referee(request, identifier_w_vn_nr, profile_id): 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=primary_or_first_email, auto_reminders_allowed=True, invited_by=request.user.contributor, ) -- GitLab