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.

112 lines
3.5 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', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  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')
  44. class ClassificationDocumentSerializer(serializers.ModelSerializer):
  45. annotations = serializers.SerializerMethodField()
  46. def get_annotations(self, instance):
  47. request = self.context.get('request')
  48. if request:
  49. annotations = instance.doc_annotations.filter(user=request.user)
  50. serializer = DocumentAnnotationSerializer(annotations, many=True)
  51. return serializer.data
  52. class Meta:
  53. model = Document
  54. fields = ('id', 'text', 'annotations')
  55. class SequenceDocumentSerializer(serializers.ModelSerializer):
  56. annotations = serializers.SerializerMethodField()
  57. def get_annotations(self, instance):
  58. request = self.context.get('request')
  59. if request:
  60. annotations = instance.seq_annotations.filter(user=request.user)
  61. serializer = SequenceAnnotationSerializer(annotations, many=True)
  62. return serializer.data
  63. class Meta:
  64. model = Document
  65. fields = ('id', 'text', 'annotations')
  66. class Seq2seqDocumentSerializer(serializers.ModelSerializer):
  67. annotations = serializers.SerializerMethodField()
  68. def get_annotations(self, instance):
  69. request = self.context.get('request')
  70. if request:
  71. annotations = instance.seq2seq_annotations.filter(user=request.user)
  72. serializer = Seq2seqAnnotationSerializer(annotations, many=True)
  73. return serializer.data
  74. class Meta:
  75. model = Document
  76. fields = ('id', 'text', 'annotations')