From 23157d67c239f44600ce7234911b22b7b83fe835 Mon Sep 17 00:00:00 2001 From: pamfilos Date: Thu, 19 Oct 2023 19:32:29 +0200 Subject: [PATCH] admin: update export views Signed-off-by: pamfilos --- config/urls.py | 3 +- scoap3/exports/admin.py | 90 ------------------------ scoap3/exports/forms.py | 10 +++ scoap3/exports/models.py | 21 ------ scoap3/exports/views.py | 58 +++++++++++++++ scoap3/templates/admin/exports_form.html | 7 -- scoap3/templates/admin/index.html | 17 +++++ scoap3/templates/admin/tools.html | 21 ++++++ 8 files changed, 108 insertions(+), 119 deletions(-) delete mode 100644 scoap3/exports/admin.py create mode 100644 scoap3/exports/forms.py delete mode 100644 scoap3/exports/models.py create mode 100644 scoap3/exports/views.py delete mode 100644 scoap3/templates/admin/exports_form.html create mode 100644 scoap3/templates/admin/index.html create mode 100644 scoap3/templates/admin/tools.html diff --git a/config/urls.py b/config/urls.py index 903c45155..b8864b067 100644 --- a/config/urls.py +++ b/config/urls.py @@ -6,13 +6,14 @@ 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, admin.site.urls), # User management path("users/", include("scoap3.users.urls", namespace="users")), diff --git a/scoap3/exports/admin.py b/scoap3/exports/admin.py deleted file mode 100644 index 053ede7d5..000000000 --- a/scoap3/exports/admin.py +++ /dev/null @@ -1,90 +0,0 @@ -import csv -import datetime - -from django.contrib import admin -from django.http import StreamingHttpResponse - -from scoap3.exports.models import AffiliationExportModel, AuthorExportModel -from scoap3.utils.tools import affiliation_export, author_export - - -class Echo: - """An object that implements just the write method of the file-like - interface. - """ - - def write(self, value): - """Write the value by returning it, instead of storing in a buffer.""" - return value - - -class AffiliationExportModelAdmin(admin.ModelAdmin): - add_form_template = "admin/exports_form.html" - - def response_add(self, request, obj, post_url_continue=None): - year = request.POST.get("year", None) - country = request.POST.get("country_code", None) - result = affiliation_export(year or None, country or None) - pseudo_buffer = Echo() - writer = csv.writer(pseudo_buffer) - return StreamingHttpResponse( - (writer.writerow(row) for row in [result["header"]] + result["data"]), - content_type="text/csv", - headers={ - "Content-Disposition": f"attachment; filename=scoap3_export_affiliations_{datetime.datetime.now()}.csv" - }, - ) - - def add_view(self, request, form_url="", extra_context=None): - extra_context = {"title": "Export Affiliations"} - return super().add_view(request, form_url, extra_context=extra_context) - - def save_model(self, request, obj, form, change): - pass - - def has_add_permission(self, request, obj=None): - return True - - def has_change_permission(self, request, obj=None): - return False - - def has_view_permission(self, request, obj=None): # Here - return False - - -class AuthorExportModelAdmin(admin.ModelAdmin): - add_form_template = "admin/exports_form.html" - - def response_add(self, request, obj, post_url_continue=None): - year = request.POST.get("year", None) - country = request.POST.get("country_code", None) - result = author_export(year or None, country or None) - pseudo_buffer = Echo() - writer = csv.writer(pseudo_buffer) - return StreamingHttpResponse( - (writer.writerow(row) for row in [result["header"]] + result["data"]), - content_type="text/csv", - headers={ - "Content-Disposition": f"attachment; filename=scoap3_export_authors_{datetime.datetime.now()}.csv" - }, - ) - - def add_view(self, request, form_url="", extra_context=None): - extra_context = {"title": "Export Authors"} - return super().add_view(request, form_url, extra_context=extra_context) - - def save_model(self, request, obj, form, change): - pass - - def has_add_permission(self, request, obj=None): - return True - - def has_change_permission(self, request, obj=None): - return False - - def has_view_permission(self, request, obj=None): - return False - - -admin.site.register(AffiliationExportModel, AffiliationExportModelAdmin) -admin.site.register(AuthorExportModel, AuthorExportModelAdmin) diff --git a/scoap3/exports/forms.py b/scoap3/exports/forms.py new file mode 100644 index 000000000..05b90c3c8 --- /dev/null +++ b/scoap3/exports/forms.py @@ -0,0 +1,10 @@ +from django import forms + + +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 diff --git a/scoap3/exports/models.py b/scoap3/exports/models.py deleted file mode 100644 index 1f611341a..000000000 --- a/scoap3/exports/models.py +++ /dev/null @@ -1,21 +0,0 @@ -from django.db import models - - -class AffiliationExportModel(models.Model): - country_code = models.CharField(max_length=2, null=True, blank=True) - year = models.IntegerField(null=True, blank=True) - - class Meta: - verbose_name_plural = "Affiliation Export" - app_label = "exports" - managed = False - - -class AuthorExportModel(models.Model): - country_code = models.CharField(max_length=2, null=True, blank=True) - year = models.IntegerField(null=True, blank=True) - - class Meta: - verbose_name_plural = "Author Export" - app_label = "exports" - managed = False diff --git a/scoap3/exports/views.py b/scoap3/exports/views.py new file mode 100644 index 000000000..bbcc686ec --- /dev/null +++ b/scoap3/exports/views.py @@ -0,0 +1,58 @@ +from django.http import HttpResponse +from django.shortcuts import render +from django.contrib import messages +import csv +import datetime + +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"'}, + ) + + writer = csv.writer(response) + if write_header: + writer.writerow(data.get('header')) + print(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: + action_name = "affiliation_export" + affiliation_form = AffiliationExportForm(request.POST) + else: + affiliation_form = AffiliationExportForm() + if "author_export" in request.POST: + action_name = "author_export" + author_form = AuthorExportForm(request.POST) + else: + author_form = AuthorExportForm(None) + + 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') + 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') + 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}') + + return render(request, "admin/tools.html", {"affiliation_form": affiliation_form, "author_form": author_form}) \ No newline at end of file diff --git a/scoap3/templates/admin/exports_form.html b/scoap3/templates/admin/exports_form.html deleted file mode 100644 index 80392679a..000000000 --- a/scoap3/templates/admin/exports_form.html +++ /dev/null @@ -1,7 +0,0 @@ -{% extends 'admin/change_form.html' %} - -{% block submit_buttons_bottom %} -
- -
-{% endblock %} diff --git a/scoap3/templates/admin/index.html b/scoap3/templates/admin/index.html new file mode 100644 index 000000000..0d8c95882 --- /dev/null +++ b/scoap3/templates/admin/index.html @@ -0,0 +1,17 @@ +{% extends "admin/index.html" %} + +{% block content %} +
+
+
+

Tools & Extra links

+
+ Export tools +
+
+
+
+

Content & Settings

+{{block.super}} + +{% endblock %} \ No newline at end of file diff --git a/scoap3/templates/admin/tools.html b/scoap3/templates/admin/tools.html new file mode 100644 index 000000000..7acac16da --- /dev/null +++ b/scoap3/templates/admin/tools.html @@ -0,0 +1,21 @@ +{% extends "admin/base.html" %} + +{% block content %} +

Tools

+ + +
+

Affiliations Export

+ {% csrf_token %} + {{ affiliation_form }} + +
+ +
+

Authors Export

+ {% csrf_token %} + {{ author_form }} + +
+ +{% endblock content %}