From 7bcc1c04b588e719a055299e4f23f86c658f50b5 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Thu, 9 Aug 2018 16:25:20 +0900 Subject: [PATCH] Remove ProjectDocsAPI --- app/server/api.py | 30 ++--------------------------- app/server/models.py | 13 ------------- app/server/serializers.py | 40 ++++++++------------------------------- app/server/urls.py | 5 ++--- 4 files changed, 12 insertions(+), 76 deletions(-) diff --git a/app/server/api.py b/app/server/api.py index dbebe659..7ef7e095 100644 --- a/app/server/api.py +++ b/app/server/api.py @@ -11,7 +11,7 @@ from rest_framework.views import APIView from .models import Project, Label, Document, Seq2seqAnnotation from .permissions import IsAdminUserAndWriteOnly, IsProjectUser, IsOwnAnnotation -from .serializers import ProjectSerializer, LabelSerializer, TextSerializer +from .serializers import ProjectSerializer, LabelSerializer, DocumentSerializer class ProjectViewSet(viewsets.ModelViewSet): @@ -87,35 +87,9 @@ class LabelDetail(generics.RetrieveUpdateDestroyAPIView): return obj -class ProjectDocsAPI(generics.ListCreateAPIView): - queryset = Document.objects.all() - filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter) - search_fields = ('text', ) - permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUserAndWriteOnly) - - def get_serializer_class(self): - project_id = self.kwargs['project_id'] - project = get_object_or_404(Project, pk=project_id) - self.serializer_class = project.get_project_serializer() - - return self.serializer_class - - def get_queryset(self): - project_id = self.kwargs['project_id'] - queryset = self.queryset.filter(project=project_id) - if not self.request.query_params.get('is_checked'): - return queryset - - project = get_object_or_404(Project, pk=project_id) - is_null = self.request.query_params.get('is_checked') == 'true' - queryset = project.get_documents(is_null).distinct() - - return queryset - - class DocumentList(generics.ListCreateAPIView): queryset = Document.objects.all() - serializer_class = TextSerializer + serializer_class = DocumentSerializer filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter) search_fields = ('text', ) permission_classes = (IsAuthenticated, IsProjectUser, IsAdminUserAndWriteOnly) diff --git a/app/server/models.py b/app/server/models.py index 87116300..fc24ec38 100644 --- a/app/server/models.py +++ b/app/server/models.py @@ -73,19 +73,6 @@ class Project(models.Model): return docs - def get_project_serializer(self): - from .serializers import DocumentSerializer - from .serializers import SequenceSerializer - from .serializers import Seq2seqSerializer - if self.is_type_of(Project.DOCUMENT_CLASSIFICATION): - return DocumentSerializer - elif self.is_type_of(Project.SEQUENCE_LABELING): - return SequenceSerializer - elif self.is_type_of(Project.Seq2seq): - return Seq2seqSerializer - else: - raise ValueError('Invalid project_type') - def get_annotation_serializer(self): from .serializers import DocumentAnnotationSerializer from .serializers import SequenceAnnotationSerializer diff --git a/app/server/serializers.py b/app/server/serializers.py index 113b9fc4..a00c643a 100644 --- a/app/server/serializers.py +++ b/app/server/serializers.py @@ -11,13 +11,20 @@ class LabelSerializer(serializers.ModelSerializer): fields = ('id', 'text', 'shortcut', 'background_color', 'text_color') -class TextSerializer(serializers.ModelSerializer): +class DocumentSerializer(serializers.ModelSerializer): class Meta: model = Document fields = ('id', 'text') +class ProjectSerializer(serializers.ModelSerializer): + + class Meta: + model = Project + fields = ('id', 'name', 'description', 'users', 'project_type', 'image') + + class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField): def get_queryset(self): @@ -41,14 +48,6 @@ class DocumentAnnotationSerializer(serializers.ModelSerializer): return annotation -class DocumentSerializer(serializers.ModelSerializer): - labels = DocumentAnnotationSerializer(source='doc_annotations', many=True) - - class Meta: - model = Document - fields = ('id', 'text', 'labels') - - class SequenceAnnotationSerializer(serializers.ModelSerializer): label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all()) @@ -61,31 +60,8 @@ class SequenceAnnotationSerializer(serializers.ModelSerializer): return annotation -class SequenceSerializer(serializers.ModelSerializer): - labels = SequenceAnnotationSerializer(source='seq_annotations', many=True) - - class Meta: - model = Document - fields = ('id', 'text', 'labels') - - class Seq2seqAnnotationSerializer(serializers.ModelSerializer): class Meta: model = Seq2seqAnnotation fields = ('id', 'text') - - -class Seq2seqSerializer(serializers.ModelSerializer): - labels = Seq2seqAnnotationSerializer(source='seq2seq_annotations', many=True) - - class Meta: - model = Document - fields = ('id', 'text', 'labels') - - -class ProjectSerializer(serializers.ModelSerializer): - - class Meta: - model = Project - fields = ('id', 'name', 'description', 'users', 'project_type', 'image') diff --git a/app/server/urls.py b/app/server/urls.py index 38946d7d..60a3f5a6 100644 --- a/app/server/urls.py +++ b/app/server/urls.py @@ -4,7 +4,7 @@ from rest_framework import routers from .views import IndexView from .views import ProjectView, DatasetView, DataUpload, LabelView, StatsView from .views import ProjectsView, DataDownload -from .api import ProjectViewSet, LabelList, ProjectStatsAPI, LabelDetail, ProjectDocsAPI, \ +from .api import ProjectViewSet, LabelList, ProjectStatsAPI, LabelDetail, \ AnnotationList, AnnotationDetail, DocumentList router = routers.DefaultRouter() @@ -16,8 +16,7 @@ urlpatterns = [ path('api/projects//stats/', ProjectStatsAPI.as_view(), name='stats-api'), path('api/projects//labels/', LabelList.as_view(), name='labels'), path('api/projects//labels/', LabelDetail.as_view(), name='label'), - path('api/projects//docs/', ProjectDocsAPI.as_view(), name='docs'), - path('api/projects//texts/', DocumentList.as_view(), name='texts'), + path('api/projects//docs/', DocumentList.as_view(), name='docs'), path('api/projects//docs//annotations/', AnnotationList.as_view(), name='annotations'), path('api/projects//docs//annotations/', AnnotationDetail.as_view(), name='ann'), path('projects/', ProjectsView.as_view(), name='projects'),