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.

146 lines
5.3 KiB

6 years ago
5 years ago
6 years ago
6 years ago
6 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. if Label.objects.filter(suffix_key=suffix_key,
  26. prefix_key__isnull=True).exists():
  27. raise ValidationError('Duplicate key.')
  28. return super().validate(attrs)
  29. class Meta:
  30. model = Label
  31. fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')
  32. class DocumentSerializer(serializers.ModelSerializer):
  33. annotations = serializers.SerializerMethodField()
  34. def get_annotations(self, instance):
  35. request = self.context.get('request')
  36. project = instance.project
  37. model = project.get_annotation_class()
  38. serializer = project.get_annotation_serializer()
  39. annotations = model.objects.filter(document=instance.id)
  40. if request:
  41. annotations = annotations.filter(user=request.user)
  42. serializer = serializer(annotations, many=True)
  43. return serializer.data
  44. class Meta:
  45. model = Document
  46. fields = ('id', 'text', 'annotations', 'meta')
  47. class ProjectSerializer(serializers.ModelSerializer):
  48. class Meta:
  49. model = Project
  50. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  51. read_only_fields = ('image', 'updated_at')
  52. class TextClassificationProjectSerializer(serializers.ModelSerializer):
  53. class Meta:
  54. model = TextClassificationProject
  55. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  56. read_only_fields = ('image', 'updated_at', 'users')
  57. class SequenceLabelingProjectSerializer(serializers.ModelSerializer):
  58. class Meta:
  59. model = SequenceLabelingProject
  60. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  61. read_only_fields = ('image', 'updated_at', 'users')
  62. class Seq2seqProjectSerializer(serializers.ModelSerializer):
  63. class Meta:
  64. model = Seq2seqProject
  65. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  66. read_only_fields = ('image', 'updated_at', 'users')
  67. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  68. model_serializer_mapping = {
  69. Project: ProjectSerializer,
  70. TextClassificationProject: TextClassificationProjectSerializer,
  71. SequenceLabelingProject: SequenceLabelingProjectSerializer,
  72. Seq2seqProject: Seq2seqProjectSerializer
  73. }
  74. class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
  75. def get_queryset(self):
  76. view = self.context.get('view', None)
  77. request = self.context.get('request', None)
  78. queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
  79. if not request or not queryset or not view:
  80. return None
  81. return queryset.filter(project=view.kwargs['project_id'])
  82. class DocumentAnnotationSerializer(serializers.ModelSerializer):
  83. # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  84. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  85. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  86. class Meta:
  87. model = DocumentAnnotation
  88. fields = ('id', 'prob', 'label', 'user', 'document')
  89. read_only_fields = ('user', )
  90. class SequenceAnnotationSerializer(serializers.ModelSerializer):
  91. #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  92. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  93. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  94. class Meta:
  95. model = SequenceAnnotation
  96. fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
  97. read_only_fields = ('user',)
  98. class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
  99. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  100. class Meta:
  101. model = Seq2seqAnnotation
  102. fields = ('id', 'text', 'user', 'document')
  103. read_only_fields = ('user',)