Browse Source

iss15: add download page and reimplement the csv download function

pull/46/head
Bramble Xu 6 years ago
parent
commit
57029d06be
4 changed files with 63 additions and 5 deletions
  1. 3
      app/server/models.py
  2. 38
      app/server/templates/admin/dataset_download.html
  3. 3
      app/server/urls.py
  4. 24
      app/server/views.py

3
app/server/models.py

@ -150,6 +150,9 @@ class Document(models.Model):
elif self.project.is_type_of(Project.Seq2seq):
return self.seq2seq_annotations.all()
def to_csv(self):
return self.make_dataset()
def make_dataset(self):
if self.project.is_type_of(Project.DOCUMENT_CLASSIFICATION):
return self.make_dataset_for_classification()

38
app/server/templates/admin/dataset_download.html

@ -0,0 +1,38 @@
{% extends "admin/admin_base.html" %}
{% load static %}
{% block content-area %}
<div class="columns">
<div class="column is-12">
<div class="card">
<header class="card-header">
<p class="card-header-title">
Export text items
</p>
<a href="#" class="card-header-icon" aria-label="more options">
<span class="icon">
<i class="fas fa-angle-down" aria-hidden="true"></i>
</span>
</a>
</header>
<div class="card-content">
<p>
<b>There are two data format you can download, the CSV and JSON.</b>
</p>
<p>
If you import as a JSON with line breaks and download it as CSV, the line breaks will render on the result.
We recommend you download it as JSON file.
</p>
<form action="{% url 'download_file' view.kwargs.project_id %}" method="get">
<a class="control">
<button type="submit" class="button is-primary" name="format" value="csv">Download CSV File</button>
</a>
<a class="control">
<button type="submit" class="button is-primary" name="format" value="json">Download JSON File</button>
</a>
</form>
</div>
</div>
</div>
</div>
{% endblock %}

3
app/server/urls.py

@ -3,7 +3,7 @@ from rest_framework import routers
from .views import IndexView
from .views import ProjectView, DatasetView, DataUpload, LabelView, StatsView, GuidelineView
from .views import ProjectsView, DataDownload
from .views import ProjectsView, DataDownload, DataDownloadFile
from .views import DemoTextClassification, DemoNamedEntityRecognition, DemoTranslation
from .api import ProjectViewSet, LabelList, ProjectStatsAPI, LabelDetail, \
AnnotationList, AnnotationDetail, DocumentList
@ -22,6 +22,7 @@ urlpatterns = [
path('api/projects/<int:project_id>/docs/<int:doc_id>/annotations/<int:annotation_id>', AnnotationDetail.as_view(), name='ann'),
path('projects/', ProjectsView.as_view(), name='projects'),
path('projects/<int:project_id>/download', DataDownload.as_view(), name='download'),
path('projects/<int:project_id>/download_file', DataDownloadFile.as_view(), name='download_file'),
path('projects/<int:project_id>/', ProjectView.as_view(), name='annotation'),
path('projects/<int:project_id>/docs/', DatasetView.as_view(), name='dataset'),
path('projects/<int:project_id>/docs/create', DataUpload.as_view(), name='upload'),

24
app/server/views.py

@ -85,22 +85,38 @@ class DataUpload(SuperUserMixin, LoginRequiredMixin, TemplateView):
return HttpResponseRedirect(reverse('upload', args=[project.id]))
class DataDownload(SuperUserMixin, LoginRequiredMixin, View):
class DataDownload(SuperUserMixin, LoginRequiredMixin, TemplateView):
template_name = 'admin/dataset_download.html'
class DataDownloadFile(SuperUserMixin, LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
project_id = self.kwargs['project_id']
project = get_object_or_404(Project, pk=project_id)
docs = project.get_documents(is_null=False).distinct()
export_format = request.GET.get('format')
filename = '_'.join(project.name.lower().split())
try:
if export_format == 'csv':
response = self.get_csv(filename, docs)
elif export_format == 'json':
response = self.get_json(filename, docs)
return response
except:
return HttpResponseRedirect(reverse('download', args=[project.id]))
def get_csv(self, filename, docs):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="{}.csv"'.format(filename)
writer = csv.writer(response)
for d in docs:
writer.writerows(d.make_dataset())
writer.writerows(d.to_csv())
return response
def get_json(self, filename, docs):
pass
class DemoTextClassification(TemplateView):
template_name = 'demo/demo_text_classification.html'

Loading…
Cancel
Save