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.

164 lines
6.1 KiB

6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
  1. from django.contrib.auth import get_user_model
  2. from rest_framework import serializers
  3. from rest_polymorphic.serializers import PolymorphicSerializer
  4. from rest_framework.exceptions import ValidationError
  5. from .models import Label, Project, Document
  6. from .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject
  7. from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
  8. class UserSerializer(serializers.ModelSerializer):
  9. class Meta:
  10. model = get_user_model()
  11. fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser')
  12. class LabelSerializer(serializers.ModelSerializer):
  13. def validate(self, attrs):
  14. if 'prefix_key' not in attrs and 'suffix_key' not in attrs:
  15. return super().validate(attrs)
  16. prefix_key = attrs['prefix_key']
  17. suffix_key = attrs['suffix_key']
  18. # In the case of user don't set any shortcut key.
  19. if prefix_key is None and suffix_key is None:
  20. return super().validate(attrs)
  21. # Don't allow shortcut key not to have a suffix key.
  22. if prefix_key and not suffix_key:
  23. raise ValidationError('Shortcut key may not have a suffix key.')
  24. # Don't allow to save same shortcut key when prefix_key is null.
  25. try:
  26. context = self.context['request'].parser_context
  27. project_id = context['kwargs']['project_id']
  28. except (AttributeError, KeyError):
  29. pass # unit tests don't always have the correct context set up
  30. else:
  31. if Label.objects.filter(suffix_key=suffix_key,
  32. prefix_key__isnull=True,
  33. project=project_id).exists():
  34. raise ValidationError('Duplicate key.')
  35. return super().validate(attrs)
  36. class Meta:
  37. model = Label
  38. fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')
  39. class DocumentSerializer(serializers.ModelSerializer):
  40. annotations = serializers.SerializerMethodField()
  41. annotation_approver = serializers.SerializerMethodField()
  42. def get_annotations(self, instance):
  43. request = self.context.get('request')
  44. project = instance.project
  45. model = project.get_annotation_class()
  46. serializer = project.get_annotation_serializer()
  47. annotations = model.objects.filter(document=instance.id)
  48. if request:
  49. annotations = annotations.filter(user=request.user)
  50. serializer = serializer(annotations, many=True)
  51. return serializer.data
  52. @classmethod
  53. def get_annotation_approver(cls, instance):
  54. approver = instance.annotations_approved_by
  55. return approver.username if approver else None
  56. class Meta:
  57. model = Document
  58. fields = ('id', 'text', 'annotations', 'meta', 'annotation_approver')
  59. class ProjectSerializer(serializers.ModelSerializer):
  60. class Meta:
  61. model = Project
  62. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  63. 'randomize_document_order')
  64. read_only_fields = ('image', 'updated_at')
  65. class TextClassificationProjectSerializer(serializers.ModelSerializer):
  66. class Meta:
  67. model = TextClassificationProject
  68. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  69. 'randomize_document_order')
  70. read_only_fields = ('image', 'updated_at', 'users')
  71. class SequenceLabelingProjectSerializer(serializers.ModelSerializer):
  72. class Meta:
  73. model = SequenceLabelingProject
  74. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  75. 'randomize_document_order')
  76. read_only_fields = ('image', 'updated_at', 'users')
  77. class Seq2seqProjectSerializer(serializers.ModelSerializer):
  78. class Meta:
  79. model = Seq2seqProject
  80. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  81. 'randomize_document_order')
  82. read_only_fields = ('image', 'updated_at', 'users')
  83. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  84. model_serializer_mapping = {
  85. Project: ProjectSerializer,
  86. TextClassificationProject: TextClassificationProjectSerializer,
  87. SequenceLabelingProject: SequenceLabelingProjectSerializer,
  88. Seq2seqProject: Seq2seqProjectSerializer
  89. }
  90. class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
  91. def get_queryset(self):
  92. view = self.context.get('view', None)
  93. request = self.context.get('request', None)
  94. queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
  95. if not request or not queryset or not view:
  96. return None
  97. return queryset.filter(project=view.kwargs['project_id'])
  98. class DocumentAnnotationSerializer(serializers.ModelSerializer):
  99. # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  100. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  101. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  102. class Meta:
  103. model = DocumentAnnotation
  104. fields = ('id', 'prob', 'label', 'user', 'document')
  105. read_only_fields = ('user', )
  106. class SequenceAnnotationSerializer(serializers.ModelSerializer):
  107. #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  108. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  109. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  110. class Meta:
  111. model = SequenceAnnotation
  112. fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
  113. read_only_fields = ('user',)
  114. class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
  115. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  116. class Meta:
  117. model = Seq2seqAnnotation
  118. fields = ('id', 'text', 'user', 'document')
  119. read_only_fields = ('user',)