Skip to content

Commit

Permalink
export: add authors and aff admin export
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalEgn authored and pamfilos committed Oct 19, 2023
1 parent ecbd098 commit fb0b566
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
"scoap3.articles",
"scoap3.authors",
"scoap3.misc",
"scoap3.exports",
"scoap3",
]
# https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
Expand Down
Empty file added scoap3/exports/__init__.py
Empty file.
90 changes: 90 additions & 0 deletions scoap3/exports/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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)
21 changes: 21 additions & 0 deletions scoap3/exports/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
7 changes: 7 additions & 0 deletions scoap3/templates/admin/exports_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'admin/change_form.html' %}

{% block submit_buttons_bottom %}
<div class="submit-row">
<input type="submit" value="Download" name="_download">
</div>
{% endblock %}

0 comments on commit fb0b566

Please sign in to comment.