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.

154 lines
5.6 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. read_only_fields = ('image', 'updated_at')
  59. class TextClassificationProjectSerializer(serializers.ModelSerializer):
  60. class Meta:
  61. model = TextClassificationProject
  62. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  63. read_only_fields = ('image', 'updated_at', 'users')
  64. class SequenceLabelingProjectSerializer(serializers.ModelSerializer):
  65. class Meta:
  66. model = SequenceLabelingProject
  67. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  68. read_only_fields = ('image', 'updated_at', 'users')
  69. class Seq2seqProjectSerializer(serializers.ModelSerializer):
  70. class Meta:
  71. model = Seq2seqProject
  72. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  73. read_only_fields = ('image', 'updated_at', 'users')
  74. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  75. model_serializer_mapping = {
  76. Project: ProjectSerializer,
  77. TextClassificationProject: TextClassificationProjectSerializer,
  78. SequenceLabelingProject: SequenceLabelingProjectSerializer,
  79. Seq2seqProject: Seq2seqProjectSerializer
  80. }
  81. class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
  82. def get_queryset(self):
  83. view = self.context.get('view', None)
  84. request = self.context.get('request', None)
  85. queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
  86. if not request or not queryset or not view:
  87. return None
  88. return queryset.filter(project=view.kwargs['project_id'])
  89. class DocumentAnnotationSerializer(serializers.ModelSerializer):
  90. # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  91. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  92. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  93. class Meta:
  94. model = DocumentAnnotation
  95. fields = ('id', 'prob', 'label', 'user', 'document')
  96. read_only_fields = ('user', )
  97. class SequenceAnnotationSerializer(serializers.ModelSerializer):
  98. #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  99. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  100. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  101. class Meta:
  102. model = SequenceAnnotation
  103. fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
  104. read_only_fields = ('user',)
  105. class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
  106. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  107. class Meta:
  108. model = Seq2seqAnnotation
  109. fields = ('id', 'text', 'user', 'document')
  110. read_only_fields = ('user',)