diff --git a/doccano/app/db.sqlite3 b/doccano/app/db.sqlite3 index 01642c63..e6e352ee 100644 Binary files a/doccano/app/db.sqlite3 and b/doccano/app/db.sqlite3 differ 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