From 6d18eab9900fe86b1386fc271fd4d130fbe6895d Mon Sep 17 00:00:00 2001 From: Hironsan Date: Wed, 27 Jun 2018 14:25:17 +0900 Subject: [PATCH] Add AnnotationAPI --- doccano/app/db.sqlite3 | Bin 196608 -> 196608 bytes doccano/app/server/urls.py | 4 +++- doccano/app/server/views.py | 42 +++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/doccano/app/db.sqlite3 b/doccano/app/db.sqlite3 index 01642c63ebfadccd8ae7d083faba2c3844670466..e6e352eea082987fe9150cc99676e62b5d76c374 100644 GIT binary patch delta 327 zcmZo@;Av>!nIO%WKT*b+F@Iyi4LxZg{wxMjA!dEX;?$zD)S~#ryuAF9#FEVXJkHI* z`WrWJG4lUr;Q!74i~rYV!GwqWlLPLv@G)^PFfg#Pv$8OAGIDZlW=!}mzo_XFLuV>6;7UEw%oqYqNG86yu?S>l|>-ial zwy)XG7$Lwc#3?tO{Q^*iQ*N`Nz$XrGPa#eQesfN3MovddN4U!r@{_VslS>pdEH$I6 zl@e1@Qd5+y6sl`$W7&-i3@vmG%yf;+6%0(QObo4zjr9yn4NZ;Aw`bmDoV^X^o%j6j w;oji|dxwpQ9qgTL3z%2}fXaR_@c-a{!v6%Q>>B^{C;yp5**V!57&tgN0V0H6E&u=k delta 240 zcmZo@;Av>!nIO%WH&Mo!F>hnS4LxZ={wxMjA!dEX;?$zD)S~#ryuAF9#FEVXJdVx5 z`WrWJF$gd)@c#nA&4LLJ`6mb5XW?VwU|?WiWoKn(WMpRJ+RT{nUw%=601#~|U=di( z;=q`|CdfaVf&cn;o&}7Z{1XE>K-#9yU(cAzEXcoRI{OAjWuR$I{M*;;XN(YF7UayF z&VB(XW+-6KxjpkHPG$f<4MrjW diff --git a/doccano/app/server/urls.py b/doccano/app/server/urls.py index c26e498b..dda9746f 100644 --- a/doccano/app/server/urls.py +++ b/doccano/app/server/urls.py @@ -5,7 +5,7 @@ from .views import AnnotationAPIView, SearchAPI, InboxView from .views import ProjectsView, ProjectAdminView, RawDataAPI, DataDownloadAPI from rest_framework import routers from .views import ProjectViewSet -from .views import ProjectLabelsAPI, ProjectLabelAPI, ProjectDocsAPI +from .views import ProjectLabelsAPI, ProjectLabelAPI, ProjectDocsAPI, AnnotationsAPI, AnnotationAPI router = routers.DefaultRouter() @@ -17,6 +17,8 @@ urlpatterns = [ path('api/projects//labels/', ProjectLabelsAPI.as_view(), name='labels'), path('api/projects//labels/', ProjectLabelAPI.as_view(), name='label'), path('api/projects//docs/', ProjectDocsAPI.as_view(), name='docs'), + path('api/projects//docs//annotations', AnnotationsAPI.as_view()), + path('api/projects//docs//annotations/', AnnotationAPI.as_view()), path('projects/', ProjectsView.as_view(), name='project-list'), path('projects//admin', ProjectAdminView.as_view(), name='project-admin'), path('projects//download', DataDownloadAPI.as_view(), name='download'), diff --git a/doccano/app/server/views.py b/doccano/app/server/views.py index d88b9466..bb00e292 100644 --- a/doccano/app/server/views.py +++ b/doccano/app/server/views.py @@ -15,7 +15,7 @@ from rest_framework.response import Response from rest_framework.permissions import IsAdminUser from .models import Annotation, Label, Document, Project -from .serializers import LabelSerializer, ProjectSerializer, DocumentSerializer +from .serializers import LabelSerializer, ProjectSerializer, DocumentSerializer, AnnotationSerializer class IndexView(TemplateView): @@ -188,3 +188,43 @@ class ProjectDocsAPI(generics.ListCreateAPIView): queryset = self.queryset.filter(project=project_id) return queryset + + +class AnnotationsAPI(generics.ListCreateAPIView): + queryset = Annotation.objects.all() + serializer_class = AnnotationSerializer + pagination_class = None + + def get_queryset(self): + doc_id = self.kwargs['doc_id'] + queryset = self.queryset.filter(data=doc_id) + return queryset + + def post(self, request, *args, **kwargs): + doc_id = self.kwargs['doc_id'] + label_id = request.data['label_id'] + doc = Document.objects.get(id=doc_id) + label = Label.objects.get(id=label_id) + annotation = Annotation(data=doc, label=label, manual=True) + annotation.save() + + return Response(annotation) + + +class AnnotationAPI(generics.RetrieveUpdateDestroyAPIView): + queryset = Annotation.objects.all() + serializer_class = AnnotationSerializer + + def get_queryset(self): + doc_id = self.kwargs['doc_id'] + queryset = self.queryset.filter(data=doc_id) + + return queryset + + def get_object(self): + annotation_id = self.kwargs['annotation_id'] + queryset = self.filter_queryset(self.get_queryset()) + obj = get_object_or_404(queryset, pk=annotation_id) + self.check_object_permissions(self.request, obj) + + return obj