From 41a3bd02c4b34bb6e2f9ec5be9e5118b028e6e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org> Date: Sun, 30 Apr 2023 21:48:23 +0200 Subject: [PATCH] Add edadmin view to preallocate pubfracs from affiliations --- scipost_django/journals/forms.py | 4 ++- .../journals/allocate_orgpubfractions.html | 4 +++ scipost_django/journals/urls/general.py | 5 ++++ scipost_django/journals/views.py | 30 +++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/scipost_django/journals/forms.py b/scipost_django/journals/forms.py index 6205edc1a..6420c32a4 100644 --- a/scipost_django/journals/forms.py +++ b/scipost_django/journals/forms.py @@ -927,7 +927,9 @@ class BaseOrgPubFractionsFormSet(BaseModelFormSet): form.is_valid() norm += 1000 * form.cleaned_data.get("fraction", 0) if norm != 1000: - raise forms.ValidationError("The fractions do not add up to one!") + raise forms.ValidationError( + f"The fractions do not add up to one! Getting {norm} / 1000" + ) OrgPubFractionsFormSet = modelformset_factory( diff --git a/scipost_django/journals/templates/journals/allocate_orgpubfractions.html b/scipost_django/journals/templates/journals/allocate_orgpubfractions.html index 68ebc82f2..648afac41 100644 --- a/scipost_django/journals/templates/journals/allocate_orgpubfractions.html +++ b/scipost_django/journals/templates/journals/allocate_orgpubfractions.html @@ -28,6 +28,10 @@ <h3 class="highlight">Which Organizations supported the research in this publication?</h3> <p>Please indicate <strong>which Organizations should be credited with supporting the research published in this publication</strong>.<br/>Data provided here is indicative and does not need to be extremely accurate.<br/>Note however that this data <strong>is used</strong> to set the suggested level of support from external Organizations which SciPost needs to remain sustainable.<br/><br/>The Organizations listed here appear as either host institutions for the authors, or as acknowledged funders.<br/>Is the list of Organizations incomplete? <a href="mailto:edadmin@{{ request.get_host }}?subject=Missing Organizations for {{ publication.doi_label }}&body=[Please provide the missing data here]">Please email EdAdmin with details</a>.</p> + {% if "edadmin" in user_roles %} + <p><span class="text-danger">EdAdmin</span>: <a href="{% url 'journals:preallocate_orgpubfractions_from_affiliations' doi_label=publication.doi_label %}">preallocate fractions based on author affiliations</a> + {% endif %} + <form method="post" action="{% url 'journals:allocate_orgpubfractions' doi_label=publication.doi_label %}"> {% csrf_token %} {{ formset.management_form }} diff --git a/scipost_django/journals/urls/general.py b/scipost_django/journals/urls/general.py index ff05dea24..6a20f8bfd 100644 --- a/scipost_django/journals/urls/general.py +++ b/scipost_django/journals/urls/general.py @@ -258,6 +258,11 @@ urlpatterns = [ journals_views.allocate_orgpubfractions, name="allocate_orgpubfractions", ), + path( + "preallocate_orgpubfractions_from_affiliations/<publication_doi_label:doi_label>", + journals_views.preallocate_orgpubfractions_from_affiliations, + name="preallocate_orgpubfractions_from_affiliations", + ), path( "request_pubfrac_check/<publication_doi_label:doi_label>", journals_views.request_pubfrac_check, diff --git a/scipost_django/journals/views.py b/scipost_django/journals/views.py index 14d1e8358..9ef5d2dd1 100644 --- a/scipost_django/journals/views.py +++ b/scipost_django/journals/views.py @@ -2,6 +2,7 @@ __copyright__ = "Copyright © Stichting SciPost (SciPost Foundation)" __license__ = "AGPL v3" +from decimal import Decimal, getcontext import hashlib import json import os @@ -1280,6 +1281,35 @@ def allocate_orgpubfractions(request, doi_label): return render(request, "journals/allocate_orgpubfractions.html", context) + +@login_required +@permission_required("scipost.can_publish_accepted_submission", return_403=True) +def preallocate_orgpubfractions_from_affiliations(request, doi_label): + """ + Prefill the pubfractions based on the author affiliations. + """ + publication = get_object_or_404(Publication, doi_label=doi_label) + nr_authors = publication.authors.all().count() + # Reset all existing pubfracs to zero + OrgPubFraction.objects.filter(publication=publication).update(fraction=0) + fraction = {} + for org in publication.get_organizations(): + fraction[org.id] = 0 + for author in publication.authors.all(): + nr_affiliations = author.affiliations.all().count() + for aff in author.affiliations.all(): + fraction[aff.id] += 1.0/(nr_authors * nr_affiliations) + for org in publication.get_organizations(): + OrgPubFraction.objects.filter( + publication=publication, + organization=org, + ).update(fraction=Decimal(fraction[org.id])) + return redirect(reverse( + "journals:allocate_orgpubfractions", + kwargs={"doi_label": doi_label}, + )) + + @login_required @permission_required("scipost.can_publish_accepted_submission", return_403=True) def request_pubfrac_check(request, doi_label): -- GitLab