Skip to content

Commit

Permalink
admin: update export views
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 fb0b566 commit 23157d6
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 119 deletions.
3 changes: 2 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
90 changes: 0 additions & 90 deletions scoap3/exports/admin.py

This file was deleted.

10 changes: 10 additions & 0 deletions scoap3/exports/forms.py
Original file line number Diff line number Diff line change
@@ -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)
21 changes: 0 additions & 21 deletions scoap3/exports/models.py

This file was deleted.

58 changes: 58 additions & 0 deletions scoap3/exports/views.py
Original file line number Diff line number Diff line change
@@ -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})
7 changes: 0 additions & 7 deletions scoap3/templates/admin/exports_form.html

This file was deleted.

17 changes: 17 additions & 0 deletions scoap3/templates/admin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "admin/index.html" %}

{% block content %}
<hr/>
<div style="margin-top: 20px;" >
<div clasfs="module">
<h1>Tools & Extra links</h1>
<div style="padding: 10px; background-color: #f4f4f4;" >
<a href="/admin/tools">Export tools</a>
</div>
</div>
</div>
<hr style="margin-top: 20px;margin-bottom: 20px;"/>
<h1>Content & Settings</h1>
{{block.super}}

{% endblock %}
21 changes: 21 additions & 0 deletions scoap3/templates/admin/tools.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "admin/base.html" %}

{% block content %}
<h3>Tools</h3>


<form action="/admin/tools/" method="post">
<h4>Affiliations Export</h4>
{% csrf_token %}
{{ affiliation_form }}
<input type="submit" name="affiliation_export" value="Download">
</form>

<form action="/admin/tools/" method="post">
<h4>Authors Export</h4>
{% csrf_token %}
{{ author_form }}
<input type="submit" name="author_export" value="Download">
</form>

{% endblock content %}

0 comments on commit 23157d6

Please sign in to comment.