Skip to content

Commit

Permalink
articles: search - add country aggregation
Browse files Browse the repository at this point in the history
Signed-off-by: pamfilos <[email protected]>
  • Loading branch information
pamfilos committed Oct 19, 2023
1 parent 23157d6 commit 84cfa54
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
4 changes: 3 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
6 changes: 6 additions & 0 deletions scoap3/articles/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class ArticleDocumentView(BaseDocumentViewSet):
LOOKUP_QUERY_IN,
],
},
"country": "authors.affiliations.country.name",
}

faceted_search_fields = {
Expand All @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions scoap3/articles/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 2 additions & 1 deletion scoap3/exports/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
year = forms.IntegerField(label="Year", required=True)
33 changes: 20 additions & 13 deletions scoap3/exports/views.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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})
return render(
request,
"admin/tools.html",
{"affiliation_form": affiliation_form, "author_form": author_form},
)
2 changes: 1 addition & 1 deletion scoap3/templates/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ <h1>Tools & Extra links</h1>
<h1>Content & Settings</h1>
{{block.super}}

{% endblock %}
{% endblock %}

0 comments on commit 84cfa54

Please sign in to comment.