Browse Source

Remove ProjectDocsAPI

pull/10/head
Hironsan 6 years ago
parent
commit
7bcc1c04b5
4 changed files with 12 additions and 76 deletions
  1. 30
      app/server/api.py
  2. 13
      app/server/models.py
  3. 40
      app/server/serializers.py
  4. 5
      app/server/urls.py

30
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)

13
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

40
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')

5
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/<int:project_id>/stats/', ProjectStatsAPI.as_view(), name='stats-api'),
path('api/projects/<int:project_id>/labels/', LabelList.as_view(), name='labels'),
path('api/projects/<int:project_id>/labels/<int:label_id>', LabelDetail.as_view(), name='label'),
path('api/projects/<int:project_id>/docs/', ProjectDocsAPI.as_view(), name='docs'),
path('api/projects/<int:project_id>/texts/', DocumentList.as_view(), name='texts'),
path('api/projects/<int:project_id>/docs/', DocumentList.as_view(), name='docs'),
path('api/projects/<int:project_id>/docs/<int:doc_id>/annotations/', AnnotationList.as_view(), name='annotations'),
path('api/projects/<int:project_id>/docs/<int:doc_id>/annotations/<int:annotation_id>', AnnotationDetail.as_view(), name='ann'),
path('projects/', ProjectsView.as_view(), name='projects'),

Loading…
Cancel
Save