diff --git a/scipost_django/submissions/forms/__init__.py b/scipost_django/submissions/forms/__init__.py index 12b8839fc14714359f208eac4bd0f8dc434638be..a1c42de8ce0c03d6190860e775371d62f7de45bd 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 2f6d097ce6c4327fa904e0d5e572a372ee383a39..23dc51d2d19016d3cc3952846aebcf4bdccd7056 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 98ca6a159435968573be148a806ab33fa219afaf..13a8c3730871add161d09c07b615d2bba20da627 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, )