Commit 27d3b588 authored by delanoe's avatar delanoe

[FEAT] Adding download button.

parent 91e14e3e
......@@ -11,8 +11,11 @@ from gargantext.util.http import ValidationException, APIView \
from collections import defaultdict
import csv
_node_available_fields = ['id', 'parent_id', 'name', 'typename', 'hyperdata', 'ngrams']
_node_default_fields = ['id', 'parent_id', 'name', 'typename']
_node_available_formats = ['json', 'csv', 'bibex']
_node_available_types = NODETYPES
......@@ -21,6 +24,7 @@ def _query_nodes(request, node_id=None):
# parameters validation
parameters = get_parameters(request)
parameters = validate(parameters, {'type': dict, 'items': {
'formated': {'type': str, 'required' : False, 'default': 'json'},
'pagination_limit': {'type': int, 'default': 10},
'pagination_offset': {'type': int, 'default': 0},
'fields': {'type': list, 'default': _node_default_fields, 'items': {
......@@ -65,6 +69,8 @@ class NodeListResource(APIView):
"""Displays the list of nodes corresponding to the query.
"""
parameters, query, count = _query_nodes(request)
if parameters['formated'] == 'json':
return JsonHttpResponse({
'parameters': parameters,
'count': count,
......@@ -74,6 +80,29 @@ class NodeListResource(APIView):
]
})
elif parameters['formated'] == 'csv':
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="Gargantext_Corpus.csv"'
writer = csv.writer(response)
keys = [ 'title' , 'journal', 'publication_date'
, 'abstract', 'authors']
writer.writerow(keys)
for node in query:
data = list()
for key in keys:
try:
data.append(node.hyperdata[key])
except:
data.append("")
writer.writerow(data)
return response
def post(self, request):
"""Create a new node.
......
......@@ -145,7 +145,7 @@
<!-- <li class="divider"></li> --!>
<div class="row">
<div class="col-md-4">
<div class="col-md-5">
{% if project %}
<h3><a href="/projects/{{project.id}}">
<span class="glyphicon glyphicon-book" aria-hidden="true"></span>
......@@ -163,14 +163,25 @@
</h3>
{% endif %}
</div>
<div class="col-md-4">
<div class="col-md-5">
<h3>
<span class="glyphicon glyphicon-user" aria-hidden="true"></span>
Author(s):
</h3>
<h4>
<div class="row">
<span class="glyphicon glyphicon-hand-right" aria-hidden="true"></span>
{{ user.username | truncatechars:15}}
</div>
<div class="col-md-4">
<br>
<!-- <a class="btn btn-default" role="button" href="/project/{{project.id}}/corpus/{{corpus.id}}/{{view}}/update">Update</a> -->
<!--
<a class="btn btn-default" role="button" href="/project/{{project.id}}/corpus/{{ corpus.id }}/corpus.csv">Download</a>
</h4>
</div>
<div class="col-md-1">
<div class="row"><br>
<a class="btn btn-default" role="button" href="/api/nodes?parent_id={{corpus.id}}&types[]=DOCUMENT&pagination_limit=100000&formated=csv">Download</a>
<!--<a class="btn btn-default" role="button" href="/project/{{project.id}}/corpus/{{corpus.id}}/{{view}}/update">Update</a>--!>
<!--
<a type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="bottom"
data-content='
<ul>
......@@ -179,8 +190,9 @@
<li><a href="/delete/{{corpus.id}}">Delete</a></li>
</ul>
'>Manage</a>
-->
<!-- <a class="btn btn-primary btn-lg" role="button" href="/admin/documents/corpus/{{ corpus.id }}/">Add documents</a></p> -->
<a class="btn btn-primary btn-lg" role="button" href="/admin/documents/corpus/{{ corpus.id }}/">Add documents</a></p>
--!>
</div>
</div>
</div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment