-
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.
Signed-off-by: pamfilos <[email protected]>
- Loading branch information
Showing
8 changed files
with
108 additions
and
119 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
This file was deleted.
Oops, something went wrong.
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,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) |
This file was deleted.
Oops, something went wrong.
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,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}) |
This file was deleted.
Oops, something went wrong.
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,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 %} |
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 @@ | ||
{% 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 %} |