From 0fd1fbc2fd0d3f12fe8c69b9b9d36db98b072f49 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jean-S=C3=A9bastien=20Caux?= <git@jscaux.org>
Date: Sun, 27 Mar 2022 20:30:49 +0200
Subject: [PATCH] Add (distinct) authors count to stats, country filter to
 search

---
 scipost_django/api/viewsets/base.py                  |  2 +-
 .../journals/api/filtersets/publication.py           | 12 ++++++++++++
 .../stats/_hx_country_level_authorships.html         |  7 +++++--
 scipost_django/stats/views.py                        |  7 ++++---
 4 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/scipost_django/api/viewsets/base.py b/scipost_django/api/viewsets/base.py
index ffaf219ef..a42fba511 100644
--- a/scipost_django/api/viewsets/base.py
+++ b/scipost_django/api/viewsets/base.py
@@ -26,4 +26,4 @@ class ExtraFilteredReadOnlyModelViewSet(viewsets.ReadOnlyModelViewSet):
                             querydict["%s__%s" % (field, lookup)] = param
                             query = query | Q(**querydict)
                         queryset = queryset.filter(query)
-        return queryset
+        return queryset.distinct()
diff --git a/scipost_django/journals/api/filtersets/publication.py b/scipost_django/journals/api/filtersets/publication.py
index 4fe8d3f38..1384dedbd 100644
--- a/scipost_django/journals/api/filtersets/publication.py
+++ b/scipost_django/journals/api/filtersets/publication.py
@@ -37,6 +37,12 @@ class PublicationPublicAPIFilterSet(df_filters.FilterSet):
             "topics__name": [
                 "icontains",
             ],
+            "authors__affiliations__name": [
+                "icontains",
+            ],
+            "authors__affiliations__country": [
+                "exact",
+            ],
         }
 
 
@@ -70,4 +76,10 @@ class PublicationPublicSearchAPIFilterSet(df_filters.FilterSet):
             "topics__name": [
                 "icontains",
             ],
+            "authors__affiliations__name": [
+                "icontains",
+            ],
+            "authors__affiliations__country": [
+                "iexact",
+            ],
         }
diff --git a/scipost_django/stats/templates/stats/_hx_country_level_authorships.html b/scipost_django/stats/templates/stats/_hx_country_level_authorships.html
index a47db94a6..12db4a7c5 100644
--- a/scipost_django/stats/templates/stats/_hx_country_level_authorships.html
+++ b/scipost_django/stats/templates/stats/_hx_country_level_authorships.html
@@ -7,8 +7,9 @@
   <thead>
     <tr>
       <th></th>
-      <th style="text-align: right;">Publications count</th>
-      <th style="text-align: right;">Authorships count</th>
+      <th style="text-align: right;">Publications</th>
+      <th style="text-align: right;">Authorships</th>
+      <th style="text-align: right;">Authors (distinct)</th>
     </tr>
   </thead>
   <tbody>
@@ -16,12 +17,14 @@
       <td>Cumulative</td>
       <td style="text-align: right;">{{ cumulative.publications_count }}</td>
       <td style="text-align: right;">{{ cumulative.authorships_count }}</td>
+      <td style="text-align: right;">{{ cumulative.authors_count }}</td>
     </tr>
     {% for year, val in per_year.items %}
       <tr>
 	<td>{{ year }}</td>
 	<td style="text-align: right;">{{ val.publications_count }}</td>
 	<td style="text-align: right;">{{ val.authorships_count }}</td>
+	<td style="text-align: right;">{{ val.authors_count }}</td>
       </tr>
     {% endfor %}
   </tbody>
diff --git a/scipost_django/stats/views.py b/scipost_django/stats/views.py
index ab7154567..fd8ba2c18 100644
--- a/scipost_django/stats/views.py
+++ b/scipost_django/stats/views.py
@@ -32,15 +32,16 @@ def _hx_country_level_authorships(request, country):
         "cumulative": {
             "publications_count": publications.count(),
             "authorships_count": authorships.count(),
+            "authors_count": authorships.values_list("profile").distinct().count(),
         },
         "per_year": {}
     }
     for year in pubyears:
+        authorships_year = authorships.filter(publication__publication_date__year=year)
         context["per_year"][year] = {
             "publications_count": publications.filter(publication_date__year=year).count(),
-            "authorships_count": authorships.filter(
-                publication__publication_date__year=year
-            ).count(),
+            "authorships_count": authorships_year.count(),
+            "authors_count": authorships_year.values_list("profile").distinct().count(),
         }
     return render(request, "stats/_hx_country_level_authorships.html", context)
 
-- 
GitLab