You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. from rest_framework import serializers
  2. from .models import Label, Project, Document
  3. from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
  4. class LabelSerializer(serializers.ModelSerializer):
  5. class Meta:
  6. model = Label
  7. fields = ('id', 'text', 'shortcut', 'background_color', 'text_color')
  8. class DocumentSerializer(serializers.ModelSerializer):
  9. class Meta:
  10. model = Document
  11. fields = ('id', 'text')
  12. class ProjectSerializer(serializers.ModelSerializer):
  13. class Meta:
  14. model = Project
  15. fields = ('id', 'name', 'description', 'users', 'project_type', 'image')
  16. class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
  17. def get_queryset(self):
  18. view = self.context.get('view', None)
  19. request = self.context.get('request', None)
  20. queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
  21. if not request or not queryset or not view:
  22. return None
  23. return queryset.filter(project=view.kwargs['project_id'])
  24. class DocumentAnnotationSerializer(serializers.ModelSerializer):
  25. label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  26. class Meta:
  27. model = DocumentAnnotation
  28. fields = ('id', 'prob', 'label')
  29. def create(self, validated_data):
  30. annotation = DocumentAnnotation.objects.create(**validated_data)
  31. return annotation
  32. class SequenceAnnotationSerializer(serializers.ModelSerializer):
  33. label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  34. class Meta:
  35. model = SequenceAnnotation
  36. fields = ('id', 'prob', 'label', 'start_offset', 'end_offset')
  37. def create(self, validated_data):
  38. annotation = SequenceAnnotation.objects.create(**validated_data)
  39. return annotation
  40. class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
  41. class Meta:
  42. model = Seq2seqAnnotation
  43. fields = ('id', 'text')