-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export: add authors and aff admin export
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |