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.

158 lines
5.8 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. def get_annotations(self, instance):
  42. request = self.context.get('request')
  43. project = instance.project
  44. model = project.get_annotation_class()
  45. serializer = project.get_annotation_serializer()
  46. annotations = model.objects.filter(document=instance.id)
  47. if request:
  48. annotations = annotations.filter(user=request.user)
  49. serializer = serializer(annotations, many=True)
  50. return serializer.data
  51. class Meta:
  52. model = Document
  53. fields = ('id', 'text', 'annotations', 'meta')
  54. class ProjectSerializer(serializers.ModelSerializer):
  55. class Meta:
  56. model = Project
  57. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  58. 'randomize_document_order')
  59. read_only_fields = ('image', 'updated_at')
  60. class TextClassificationProjectSerializer(serializers.ModelSerializer):
  61. class Meta:
  62. model = TextClassificationProject
  63. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  64. 'randomize_document_order')
  65. read_only_fields = ('image', 'updated_at', 'users')
  66. class SequenceLabelingProjectSerializer(serializers.ModelSerializer):
  67. class Meta:
  68. model = SequenceLabelingProject
  69. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  70. 'randomize_document_order')
  71. read_only_fields = ('image', 'updated_at', 'users')
  72. class Seq2seqProjectSerializer(serializers.ModelSerializer):
  73. class Meta:
  74. model = Seq2seqProject
  75. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at',
  76. 'randomize_document_order')
  77. read_only_fields = ('image', 'updated_at', 'users')
  78. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  79. model_serializer_mapping = {
  80. Project: ProjectSerializer,
  81. TextClassificationProject: TextClassificationProjectSerializer,
  82. SequenceLabelingProject: SequenceLabelingProjectSerializer,
  83. Seq2seqProject: Seq2seqProjectSerializer
  84. }
  85. class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
  86. def get_queryset(self):
  87. view = self.context.get('view', None)
  88. request = self.context.get('request', None)
  89. queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
  90. if not request or not queryset or not view:
  91. return None
  92. return queryset.filter(project=view.kwargs['project_id'])
  93. class DocumentAnnotationSerializer(serializers.ModelSerializer):
  94. # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  95. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  96. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  97. class Meta:
  98. model = DocumentAnnotation
  99. fields = ('id', 'prob', 'label', 'user', 'document')
  100. read_only_fields = ('user', )
  101. class SequenceAnnotationSerializer(serializers.ModelSerializer):
  102. #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  103. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  104. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  105. class Meta:
  106. model = SequenceAnnotation
  107. fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
  108. read_only_fields = ('user',)
  109. class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
  110. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  111. class Meta:
  112. model = Seq2seqAnnotation
  113. fields = ('id', 'text', 'user', 'document')
  114. read_only_fields = ('user',)