Browse Source

Add AnnotationAPI

pull/10/head
Hironsan 6 years ago
parent
commit
6d18eab990
3 changed files with 44 additions and 2 deletions
  1. BIN
      doccano/app/db.sqlite3
  2. 4
      doccano/app/server/urls.py
  3. 42
      doccano/app/server/views.py

BIN
doccano/app/db.sqlite3

4
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/<int:project_id>/labels/', ProjectLabelsAPI.as_view(), name='labels'),
path('api/projects/<int:project_id>/labels/<int:label_id>', ProjectLabelAPI.as_view(), name='label'),
path('api/projects/<int:project_id>/docs/', ProjectDocsAPI.as_view(), name='docs'),
path('api/projects/<int:project_id>/docs/<int:doc_id>/annotations', AnnotationsAPI.as_view()),
path('api/projects/<int:project_id>/docs/<int:doc_id>/annotations/<int:annotation_id>', AnnotationAPI.as_view()),
path('projects/', ProjectsView.as_view(), name='project-list'),
path('projects/<int:pk>/admin', ProjectAdminView.as_view(), name='project-admin'),
path('projects/<int:project_id>/download', DataDownloadAPI.as_view(), name='download'),

42
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
Loading…
Cancel
Save