|
|
from rest_framework import serializers
from .models import Label, Project, Document from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
class LabelSerializer(serializers.ModelSerializer):
class Meta: model = Label fields = ('id', 'text', 'shortcut', 'background_color', 'text_color')
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): view = self.context.get('view', None) request = self.context.get('request', None) queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset() if not request or not queryset or not view: return None return queryset.filter(project=view.kwargs['project_id'])
class DocumentAnnotationSerializer(serializers.ModelSerializer): label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
class Meta: model = DocumentAnnotation fields = ('id', 'prob', 'label')
def create(self, validated_data): annotation = DocumentAnnotation.objects.create(**validated_data) return annotation
class SequenceAnnotationSerializer(serializers.ModelSerializer): label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
class Meta: model = SequenceAnnotation fields = ('id', 'prob', 'label', 'start_offset', 'end_offset')
def create(self, validated_data): annotation = SequenceAnnotation.objects.create(**validated_data) return annotation
class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
class Meta: model = Seq2seqAnnotation fields = ('id', 'text')
|