From 29d4716871a1ba5a76c955a7177bb9fd03d99a11 Mon Sep 17 00:00:00 2001 From: George Katsikas <giorgakis.katsikas@gmail.com> Date: Wed, 16 Oct 2024 11:45:39 +0200 Subject: [PATCH] add country-level nap data --- .../finances/_hx_country_level_data.html | 6 ++++ scipost_django/finances/views.py | 33 +++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/scipost_django/finances/templates/finances/_hx_country_level_data.html b/scipost_django/finances/templates/finances/_hx_country_level_data.html index 12ca90056..68c92cf2b 100644 --- a/scipost_django/finances/templates/finances/_hx_country_level_data.html +++ b/scipost_django/finances/templates/finances/_hx_country_level_data.html @@ -7,6 +7,7 @@ <thead> <tr> <th></th> + <th style="text-align: right;">NAP</th> <th style="text-align: right;">Expenditures</th> <th style="text-align: right;">Subsidy income</th> <th style="text-align: right;">Impact on reserves</th> @@ -15,6 +16,7 @@ <tbody> <tr class="bg-light"> <td>Cumulative</td> + <td style="text-align: right;">{{ cumulative.nap }}</td> <td style="text-align: right;">€{{ cumulative.expenditures }}</td> <td style="text-align: right;">€{{ cumulative.subsidy_income }}</td> <td style="text-align: right;">€{{ cumulative.impact_on_reserves }}</td> @@ -22,6 +24,7 @@ {% for year, val in per_year.items %} <tr> <td>{{ year }}</td> + <td style="text-align: right;">{{ val.nap }}</td> <td style="text-align: right;">€{{ val.expenditures }}</td> <td style="text-align: right;">€{{ val.subsidy_income }}</td> <td style="text-align: right;">€{{ val.impact_on_reserves }}</td> @@ -42,6 +45,7 @@ <tr> <th></th> <th></th> + <th style="text-align: right;">NAP</th> <th style="text-align: right;">Expenditures</th> <th style="text-align: right;">Subsidy Income</th> <th style="text-align: right;">Impact on reserves</th> @@ -51,6 +55,7 @@ <tr class="bg-light"> <td>Total</td> <td></td> + <td style="text-align: right;">{{ cumulative.nap }}</td> <td style="text-align: right;">{{ cumulative.expenditures }}</td> <td style="text-align: right;">{{ cumulative.subsidy_income }}</td> <td style="text-align: right;">{{ cumulative.impact_on_reserves }}</td> @@ -60,6 +65,7 @@ <tr> <td><a href="https://{{ request.get_host }}{{ organization.get_absolute_url }}">{{ organization }}</a></td> <td>{{ organization.get_orgtype_display }}</td> + <td style="text-align: right;">{{ organization.cf_balance_info.cumulative.nap }}</td> <td style="text-align: right;">{{ organization.cf_balance_info.cumulative.expenditures }}</td> <td style="text-align: right;">{{ organization.cf_balance_info.cumulative.subsidy_income }}</td> <td style="text-align: right;">{{ organization.cf_balance_info.cumulative.impact_on_reserves }}</td> diff --git a/scipost_django/finances/views.py b/scipost_django/finances/views.py index 3ae098890..8e3ebf21a 100644 --- a/scipost_django/finances/views.py +++ b/scipost_django/finances/views.py @@ -3,13 +3,13 @@ __license__ = "AGPL v3" import datetime -from itertools import accumulate +from itertools import accumulate, chain import mimetypes from dal import autocomplete from django.contrib.contenttypes.models import ContentType from django.db import models -from django.db.models import Q, Count, OuterRef, Subquery +from django.db.models import Q, Count, Exists, OuterRef, Subquery from django.db.models.functions import Coalesce from django.template.response import TemplateResponse from django.utils.html import format_html @@ -17,6 +17,7 @@ import matplotlib from common.views import HXDynselAutocomplete, HXDynselSelectOptionView from finances.constants import SUBSIDY_TYPE_SPONSORSHIPAGREEMENT, SUBSIDY_PROMISED +from journals.models.publication import PublicationAuthorsTable matplotlib.use("Agg") import matplotlib.pyplot as plt @@ -281,14 +282,40 @@ def _hx_country_level_data(request, country): context = { "country": country, "organizations": organizations, - "cumulative": {"subsidy_income": 0, "expenditures": 0, "impact_on_reserves": 0}, + "cumulative": { + "subsidy_income": 0, + "expenditures": 0, + "impact_on_reserves": 0, + "nap": len( + set(chain.from_iterable([o.get_publications() for o in organizations])) + ), + }, "per_year": {}, } + + def get_yearly_nap_of_country(country: str) -> dict[int, int]: + return dict( + Publication.objects.annotate( + has_author_of_country=Exists( + PublicationAuthorsTable.objects.filter( + publication=OuterRef("pk"), affiliations__country=country + ) + ) + ) + .filter(has_author_of_country=True) + .values("publication_date__year") + .annotate(nr=Count("publication_date__year")) + .order_by("publication_date__year") + .values_list("publication_date__year", "nr") + ) + + country_yearly_nap = get_yearly_nap_of_country(country) for year in pubyears: context["per_year"][year] = { "subsidy_income": 0, "expenditures": 0, "impact_on_reserves": 0, + "nap": country_yearly_nap.get(int(year), 0), } for organization in organizations.all(): for key in ("subsidy_income", "expenditures", "impact_on_reserves"): -- GitLab