Browse Source

Implement download api

pull/10/head
Hironsan 6 years ago
parent
commit
8744d42161
4 changed files with 31 additions and 4 deletions
  1. 10
      doccano/app/server/models.py
  2. 8
      doccano/app/server/templates/project_admin.html
  3. 3
      doccano/app/server/urls.py
  4. 14
      doccano/app/server/views.py

10
doccano/app/server/models.py

@ -23,6 +23,9 @@ class Label(models.Model):
'text': self.text, 'text': self.text,
'shortcut': self.shortcut} 'shortcut': self.shortcut}
def __str__(self):
return self.text
class Document(models.Model): class Document(models.Model):
text = models.TextField() text = models.TextField()
@ -32,6 +35,9 @@ class Document(models.Model):
return {'id': self.id, return {'id': self.id,
'text': self.text} 'text': self.text}
def __str__(self):
return self.text[:50]
class Annotation(models.Model): class Annotation(models.Model):
prob = models.FloatField(blank=True, null=True) prob = models.FloatField(blank=True, null=True)
@ -41,7 +47,7 @@ class Annotation(models.Model):
def as_dict(self): def as_dict(self):
return {'id': self.id, return {'id': self.id,
'data_id': self.data.id,
'label_id': self.label.id,
'doc': self.data.as_dict(),
'label': self.label.as_dict(),
'prob': self.prob, 'prob': self.prob,
'manual': self.manual} 'manual': self.manual}

8
doccano/app/server/templates/project_admin.html

@ -106,6 +106,14 @@
</span> </span>
</label> </label>
</div> </div>
<br>
<p>Data Download</p>
<a class="button" href="{% url 'download' project.id %}">
<span class="icon">
<i class="fas fa-download"></i>
</span>
<span>Download</span>
</a>
</div> </div>
</div> </div>
</div> </div>

3
doccano/app/server/urls.py

@ -1,12 +1,13 @@
from django.urls import path from django.urls import path
from .views import AnnotationView, AnnotationAPIView, ProgressAPI, SearchAPI, InboxView from .views import AnnotationView, AnnotationAPIView, ProgressAPI, SearchAPI, InboxView
from .views import ProjectListView, ProjectAdminView, RawDataAPI, LabelAPI
from .views import ProjectListView, ProjectAdminView, RawDataAPI, LabelAPI, DataDownloadAPI
urlpatterns = [ urlpatterns = [
path('', ProjectListView.as_view(), name='project-list'), path('', ProjectListView.as_view(), name='project-list'),
path('<int:pk>/admin', ProjectAdminView.as_view(), name='project-admin'), path('<int:pk>/admin', ProjectAdminView.as_view(), name='project-admin'),
path('<int:project_id>/', AnnotationView.as_view(), name='annotation'), path('<int:project_id>/', AnnotationView.as_view(), name='annotation'),
path('<int:project_id>/download', DataDownloadAPI.as_view(), name='download'),
path('<int:project_id>/inbox', InboxView.as_view(), name='inbox'), path('<int:project_id>/inbox', InboxView.as_view(), name='inbox'),
path('<int:project_id>/apis/data', AnnotationAPIView.as_view()), path('<int:project_id>/apis/data', AnnotationAPIView.as_view()),
path('<int:pk>/apis/raw_data', RawDataAPI.as_view(), name='data_api'), path('<int:pk>/apis/raw_data', RawDataAPI.as_view(), name='data_api'),

14
doccano/app/server/views.py

@ -1,6 +1,6 @@
import json import json
from django.http import JsonResponse
from django.http import JsonResponse, HttpResponse
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.views.generic.list import ListView from django.views.generic.list import ListView
@ -174,3 +174,15 @@ class ProjectAdminView(DetailView):
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
return context return context
class DataDownloadAPI(View):
def get(self, request, *args, **kwargs):
# j = {'hoge': 'fuga'}
annotated_docs = [a.as_dict() for a in Annotation.objects.filter(manual=True)]
json_str = json.dumps(annotated_docs)
response = HttpResponse(json_str, content_type='application/json')
response['Content-Disposition'] = 'attachment; filename=annotation_data.json'
return response
Loading…
Cancel
Save