diff --git a/scipost_django/journals/forms.py b/scipost_django/journals/forms.py index 6205edc1a9d60e40f43dfe80b1d0095453612187..6420c32a4fe3ec6cf6c2b0392ef752cbdd291510 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 68ebc82f2a5744cf4b9ef94309323e116406ee00..648afac411952a6091b950e4d20cefa66e1251f7 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 ff05dea24c076f725ab3ac49f6b3321fdb4bec5f..6a20f8bfd2fb868b17593ad7557ed660e833a1eb 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 14d1e8358e2a009fe832edb429943e1d61a43876..9ef5d2dd154a669d1ff6aa2d42c234b66a26f865 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):