diff --git a/config/urls.py b/config/urls.py index b8864b067..b2df25c41 100644 --- a/config/urls.py +++ b/config/urls.py @@ -6,14 +6,16 @@ from django.views.generic import TemplateView from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView from rest_framework.authtoken.views import obtain_auth_token + from scoap3.exports.views import get_exports + urlpatterns = [ path("", TemplateView.as_view(template_name="pages/home.html"), name="home"), path( "about/", TemplateView.as_view(template_name="pages/about.html"), name="about" ), # Django Admin, use {% url 'admin:index' %} - path(settings.ADMIN_URL+"tools/", get_exports, name="admin_tools"), + path(settings.ADMIN_URL + "tools/", get_exports, name="admin_tools"), path(settings.ADMIN_URL, admin.site.urls), # User management path("users/", include("scoap3.users.urls", namespace="users")), diff --git a/scoap3/articles/api/views.py b/scoap3/articles/api/views.py index 33ec9794a..29332e8c8 100644 --- a/scoap3/articles/api/views.py +++ b/scoap3/articles/api/views.py @@ -88,6 +88,7 @@ class ArticleDocumentView(BaseDocumentViewSet): LOOKUP_QUERY_IN, ], }, + "country": "authors.affiliations.country.name", } faceted_search_fields = { @@ -104,6 +105,11 @@ class ArticleDocumentView(BaseDocumentViewSet): "facet": TermsFacet, "enabled": True, }, + "country": { + "field": "authors.affiliations.country.name", + "facet": TermsFacet, + "enabled": True, + }, } def get_serializer_class(self): diff --git a/scoap3/articles/documents.py b/scoap3/articles/documents.py index 026852584..92ebdb02f 100644 --- a/scoap3/articles/documents.py +++ b/scoap3/articles/documents.py @@ -62,15 +62,15 @@ class ArticleDocument(Document): } ) - authors = fields.NestedField( + authors = fields.ObjectField( properties={ "first_name": fields.TextField(), "last_name": fields.TextField(), - "affiliations": fields.NestedField( + "affiliations": fields.ObjectField( properties={ "value": fields.TextField(), "organization": fields.TextField(), - "country": fields.NestedField( + "country": fields.ObjectField( properties={ "code": fields.TextField(), "name": fields.KeywordField(), diff --git a/scoap3/exports/forms.py b/scoap3/exports/forms.py index 05b90c3c8..9264eb784 100644 --- a/scoap3/exports/forms.py +++ b/scoap3/exports/forms.py @@ -5,6 +5,7 @@ class AffiliationExportForm(forms.Form): country_code = forms.CharField(label="Country", required=True, max_length=4) year = forms.IntegerField(label="Year", required=True) + class AuthorExportForm(forms.Form): country_code = forms.CharField(label="Country", required=True, max_length=4) - year = forms.IntegerField(label="Year", required=True) \ No newline at end of file + year = forms.IntegerField(label="Year", required=True) diff --git a/scoap3/exports/views.py b/scoap3/exports/views.py index bbcc686ec..4dbf11633 100644 --- a/scoap3/exports/views.py +++ b/scoap3/exports/views.py @@ -1,30 +1,33 @@ -from django.http import HttpResponse -from django.shortcuts import render -from django.contrib import messages import csv import datetime +from django.contrib import messages +from django.http import HttpResponse +from django.shortcuts import render + from scoap3.utils.tools import affiliation_export, author_export from .forms import AffiliationExportForm, AuthorExportForm + def generate_csv_response(data, action_name, write_header=True): response = HttpResponse( content_type="text/csv", headers={ - "Content-Disposition": \ - f'attachment; filename="scoap3_{action_name}_{datetime.datetime.now()}.csv"'}, + "Content-Disposition": f'attachment; filename="scoap3_{action_name}_{datetime.datetime.now()}.csv"' + }, ) writer = csv.writer(response) if write_header: - writer.writerow(data.get('header')) + writer.writerow(data.get("header")) print(data) - for row in data.get('data', []): + for row in data.get("data", []): writer.writerow(row) return response + def get_exports(request): action_name = None if "affiliation_export" in request.POST: @@ -41,18 +44,22 @@ def get_exports(request): if action_name and request.method == "POST": try: if "affiliation_export" in request.POST and affiliation_form.is_valid(): - year= affiliation_form.data.get('year') - country= affiliation_form.data.get('country_code') + year = affiliation_form.data.get("year") + country = affiliation_form.data.get("country_code") result = affiliation_export(year or None, country or None) if "author_export" in request.POST and author_form.is_valid(): - year= author_form.data.get('year') - country= author_form.data.get('country_code') + year = author_form.data.get("year") + country = author_form.data.get("country_code") result = author_export(year or None, country or None) response = generate_csv_response(result, action_name) return response except Exception as ex: - messages.error(request, f'There was an error: {ex}') + messages.error(request, f"There was an error: {ex}") - return render(request, "admin/tools.html", {"affiliation_form": affiliation_form, "author_form": author_form}) \ No newline at end of file + return render( + request, + "admin/tools.html", + {"affiliation_form": affiliation_form, "author_form": author_form}, + ) diff --git a/scoap3/templates/admin/index.html b/scoap3/templates/admin/index.html index 0d8c95882..0441509b1 100644 --- a/scoap3/templates/admin/index.html +++ b/scoap3/templates/admin/index.html @@ -14,4 +14,4 @@