Browse Source

Allow document serializer to return annotations

pull/110/head
Hironsan 5 years ago
parent
commit
880c179966
5 changed files with 24 additions and 84 deletions
  1. 12
      app/server/models.py
  2. 60
      app/server/serializers.py
  3. 12
      app/server/tests/test_api.py
  4. 18
      app/server/tests/test_models.py
  5. 6
      app/server/tests/test_serializers.py

12
app/server/models.py

@ -46,10 +46,6 @@ class TextClassificationProject(Project):
def get_template_name(self): def get_template_name(self):
return 'annotation/document_classification.html' return 'annotation/document_classification.html'
def get_document_serializer(self):
from .serializers import ClassificationDocumentSerializer
return ClassificationDocumentSerializer
def get_annotation_serializer(self): def get_annotation_serializer(self):
from .serializers import DocumentAnnotationSerializer from .serializers import DocumentAnnotationSerializer
return DocumentAnnotationSerializer return DocumentAnnotationSerializer
@ -67,10 +63,6 @@ class SequenceLabelingProject(Project):
def get_template_name(self): def get_template_name(self):
return 'annotation/sequence_labeling.html' return 'annotation/sequence_labeling.html'
def get_document_serializer(self):
from .serializers import SequenceDocumentSerializer
return SequenceDocumentSerializer
def get_annotation_serializer(self): def get_annotation_serializer(self):
from .serializers import SequenceAnnotationSerializer from .serializers import SequenceAnnotationSerializer
return SequenceAnnotationSerializer return SequenceAnnotationSerializer
@ -88,10 +80,6 @@ class Seq2seqProject(Project):
def get_template_name(self): def get_template_name(self):
return 'annotation/seq2seq.html' return 'annotation/seq2seq.html'
def get_document_serializer(self):
from .serializers import Seq2seqDocumentSerializer
return Seq2seqDocumentSerializer
def get_annotation_serializer(self): def get_annotation_serializer(self):
from .serializers import Seq2seqAnnotationSerializer from .serializers import Seq2seqAnnotationSerializer
return Seq2seqAnnotationSerializer return Seq2seqAnnotationSerializer

60
app/server/serializers.py

@ -1,3 +1,4 @@
from django.shortcuts import get_object_or_404
from rest_framework import serializers from rest_framework import serializers
from rest_polymorphic.serializers import PolymorphicSerializer from rest_polymorphic.serializers import PolymorphicSerializer
@ -14,10 +15,22 @@ class LabelSerializer(serializers.ModelSerializer):
class DocumentSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer):
annotations = serializers.SerializerMethodField()
def get_annotations(self, instance):
request = self.context.get('request')
view = self.context.get('view', None)
if request and view:
project = get_object_or_404(Project, pk=view.kwargs['project_id'])
model = project.get_annotation_class()
serializer = project.get_annotation_serializer()
annotations = model.objects.filter(user=request.user, document=instance.id)
serializer = serializer(annotations, many=True)
return serializer.data
class Meta: class Meta:
model = Document model = Document
fields = ('id', 'text')
fields = ('id', 'text', 'annotations')
class ProjectSerializer(serializers.ModelSerializer): class ProjectSerializer(serializers.ModelSerializer):
@ -79,48 +92,3 @@ class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Seq2seqAnnotation model = Seq2seqAnnotation
fields = ('id', 'text') fields = ('id', 'text')
class ClassificationDocumentSerializer(serializers.ModelSerializer):
annotations = serializers.SerializerMethodField()
def get_annotations(self, instance):
request = self.context.get('request')
if request:
annotations = instance.doc_annotations.filter(user=request.user)
serializer = DocumentAnnotationSerializer(annotations, many=True)
return serializer.data
class Meta:
model = Document
fields = ('id', 'text', 'annotations')
class SequenceDocumentSerializer(serializers.ModelSerializer):
annotations = serializers.SerializerMethodField()
def get_annotations(self, instance):
request = self.context.get('request')
if request:
annotations = instance.seq_annotations.filter(user=request.user)
serializer = SequenceAnnotationSerializer(annotations, many=True)
return serializer.data
class Meta:
model = Document
fields = ('id', 'text', 'annotations')
class Seq2seqDocumentSerializer(serializers.ModelSerializer):
annotations = serializers.SerializerMethodField()
def get_annotations(self, instance):
request = self.context.get('request')
if request:
annotations = instance.seq2seq_annotations.filter(user=request.user)
serializer = Seq2seqAnnotationSerializer(annotations, many=True)
return serializer.data
class Meta:
model = Document
fields = ('id', 'text', 'annotations')

12
app/server/tests/test_api.py

@ -271,10 +271,10 @@ class TestDocumentListAPI(APITestCase):
password=cls.super_user_pass, password=cls.super_user_pass,
email='fizz@buzz.com') email='fizz@buzz.com')
cls.main_project = mommy.make('server.Project', users=[project_member, super_user])
cls.main_project = mommy.make('server.TextClassificationProject', users=[project_member, super_user])
mommy.make('server.Document', project=cls.main_project) mommy.make('server.Document', project=cls.main_project)
sub_project = mommy.make('server.Project', users=[non_project_member])
sub_project = mommy.make('server.TextClassificationProject', users=[non_project_member])
mommy.make('server.Document', project=sub_project) mommy.make('server.Document', project=sub_project)
cls.url = reverse(viewname='doc_list', args=[cls.main_project.id]) cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
cls.data = {'text': 'example'} cls.data = {'text': 'example'}
@ -328,7 +328,7 @@ class TestDocumentDetailAPI(APITestCase):
super_user = User.objects.create_superuser(username=cls.super_user_name, super_user = User.objects.create_superuser(username=cls.super_user_name,
password=cls.super_user_pass, password=cls.super_user_pass,
email='fizz@buzz.com') email='fizz@buzz.com')
project = mommy.make('server.Project', users=[project_member, super_user])
project = mommy.make('server.TextClassificationProject', users=[project_member, super_user])
cls.doc = mommy.make('server.Document', project=project) cls.doc = mommy.make('server.Document', project=project)
cls.url = reverse(viewname='doc_detail', args=[project.id, cls.doc.id]) cls.url = reverse(viewname='doc_detail', args=[project.id, cls.doc.id])
cls.data = {'text': 'example'} cls.data = {'text': 'example'}
@ -537,7 +537,7 @@ class TestSearch(APITestCase):
non_project_member = User.objects.create_user(username=cls.non_project_member_name, non_project_member = User.objects.create_user(username=cls.non_project_member_name,
password=cls.non_project_member_pass) password=cls.non_project_member_pass)
cls.main_project = mommy.make('server.Project', users=[project_member])
cls.main_project = mommy.make('server.TextClassificationProject', users=[project_member])
cls.search_term = 'example' cls.search_term = 'example'
doc1 = mommy.make('server.Document', text=cls.search_term, project=cls.main_project) doc1 = mommy.make('server.Document', text=cls.search_term, project=cls.main_project)
doc2 = mommy.make('server.Document', text='Lorem', project=cls.main_project) doc2 = mommy.make('server.Document', text='Lorem', project=cls.main_project)
@ -546,7 +546,7 @@ class TestSearch(APITestCase):
mommy.make('server.SequenceAnnotation', document=doc1, user=project_member, label=label1) mommy.make('server.SequenceAnnotation', document=doc1, user=project_member, label=label1)
mommy.make('server.SequenceAnnotation', document=doc2, user=project_member, label=label2) mommy.make('server.SequenceAnnotation', document=doc2, user=project_member, label=label2)
sub_project = mommy.make('server.Project', users=[non_project_member])
sub_project = mommy.make('server.TextClassificationProject', users=[non_project_member])
mommy.make('server.Document', text=cls.search_term, project=sub_project) mommy.make('server.Document', text=cls.search_term, project=sub_project)
cls.url = reverse(viewname='doc_list', args=[cls.main_project.id]) cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
cls.data = {'q': cls.search_term} cls.data = {'q': cls.search_term}
@ -604,7 +604,7 @@ class TestFilter(APITestCase):
cls.project_member_pass = 'project_member_pass' cls.project_member_pass = 'project_member_pass'
project_member = User.objects.create_user(username=cls.project_member_name, project_member = User.objects.create_user(username=cls.project_member_name,
password=cls.project_member_pass) password=cls.project_member_pass)
cls.main_project = mommy.make('server.Project', users=[project_member])
cls.main_project = mommy.make('server.TextClassificationProject', users=[project_member])
cls.label1 = mommy.make('server.Label', project=cls.main_project) cls.label1 = mommy.make('server.Label', project=cls.main_project)
cls.label2 = mommy.make('server.Label', project=cls.main_project) cls.label2 = mommy.make('server.Label', project=cls.main_project)
doc1 = mommy.make('server.Document', project=cls.main_project) doc1 = mommy.make('server.Document', project=cls.main_project)

18
app/server/tests/test_models.py

@ -3,9 +3,9 @@ from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from mixer.backend.django import mixer from mixer.backend.django import mixer
from ..models import Label, DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation from ..models import Label, DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
from ..serializers import ClassificationDocumentSerializer, DocumentAnnotationSerializer
from ..serializers import SequenceDocumentSerializer, SequenceAnnotationSerializer
from ..serializers import Seq2seqDocumentSerializer, Seq2seqAnnotationSerializer
from ..serializers import DocumentAnnotationSerializer
from ..serializers import SequenceAnnotationSerializer
from ..serializers import Seq2seqAnnotationSerializer
class TestTextClassificationProject(TestCase): class TestTextClassificationProject(TestCase):
@ -22,10 +22,6 @@ class TestTextClassificationProject(TestCase):
template = self.project.get_template_name() template = self.project.get_template_name()
self.assertEqual(template, 'annotation/document_classification.html') self.assertEqual(template, 'annotation/document_classification.html')
def test_get_document_serializer(self):
serializer = self.project.get_document_serializer()
self.assertEqual(serializer, ClassificationDocumentSerializer)
def test_get_annotation_serializer(self): def test_get_annotation_serializer(self):
serializer = self.project.get_annotation_serializer() serializer = self.project.get_annotation_serializer()
self.assertEqual(serializer, DocumentAnnotationSerializer) self.assertEqual(serializer, DocumentAnnotationSerializer)
@ -49,10 +45,6 @@ class TestSequenceLabelingProject(TestCase):
template = self.project.get_template_name() template = self.project.get_template_name()
self.assertEqual(template, 'annotation/sequence_labeling.html') self.assertEqual(template, 'annotation/sequence_labeling.html')
def test_get_document_serializer(self):
serializer = self.project.get_document_serializer()
self.assertEqual(serializer, SequenceDocumentSerializer)
def test_get_annotation_serializer(self): def test_get_annotation_serializer(self):
serializer = self.project.get_annotation_serializer() serializer = self.project.get_annotation_serializer()
self.assertEqual(serializer, SequenceAnnotationSerializer) self.assertEqual(serializer, SequenceAnnotationSerializer)
@ -76,10 +68,6 @@ class TestSeq2seqProject(TestCase):
template = self.project.get_template_name() template = self.project.get_template_name()
self.assertEqual(template, 'annotation/seq2seq.html') self.assertEqual(template, 'annotation/seq2seq.html')
def test_get_document_serializer(self):
serializer = self.project.get_document_serializer()
self.assertEqual(serializer, Seq2seqDocumentSerializer)
def test_get_annotation_serializer(self): def test_get_annotation_serializer(self):
serializer = self.project.get_annotation_serializer() serializer = self.project.get_annotation_serializer()
self.assertEqual(serializer, Seq2seqAnnotationSerializer) self.assertEqual(serializer, Seq2seqAnnotationSerializer)

6
app/server/tests/test_serializers.py

@ -1,11 +1,7 @@
from django.test import TestCase from django.test import TestCase
from mixer.backend.django import mixer from mixer.backend.django import mixer
from ..serializers import LabelSerializer from ..serializers import LabelSerializer
from ..models import Label, DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
from ..serializers import ClassificationDocumentSerializer, DocumentAnnotationSerializer
from ..serializers import SequenceDocumentSerializer, SequenceAnnotationSerializer
from ..serializers import Seq2seqDocumentSerializer, Seq2seqAnnotationSerializer
from ..serializers import Seq2seqAnnotationSerializer
class TestLabelSerializer(TestCase): class TestLabelSerializer(TestCase):

Loading…
Cancel
Save