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.

115 lines
4.2 KiB

6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
  1. from rest_framework import serializers
  2. from rest_polymorphic.serializers import PolymorphicSerializer
  3. from .models import Label, Project, Document
  4. from .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject
  5. from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
  6. class LabelSerializer(serializers.ModelSerializer):
  7. class Meta:
  8. model = Label
  9. fields = ('id', 'text', 'shortcut', 'background_color', 'text_color')
  10. class DocumentSerializer(serializers.ModelSerializer):
  11. annotations = serializers.SerializerMethodField()
  12. def get_annotations(self, instance):
  13. request = self.context.get('request')
  14. project = instance.project
  15. model = project.get_annotation_class()
  16. serializer = project.get_annotation_serializer()
  17. annotations = model.objects.filter(document=instance.id)
  18. if request:
  19. annotations = annotations.filter(user=request.user)
  20. serializer = serializer(annotations, many=True)
  21. return serializer.data
  22. class Meta:
  23. model = Document
  24. fields = ('id', 'text', 'annotations', 'meta')
  25. class ProjectSerializer(serializers.ModelSerializer):
  26. class Meta:
  27. model = Project
  28. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  29. read_only_fields = ('image', 'updated_at')
  30. class TextClassificationProjectSerializer(serializers.ModelSerializer):
  31. class Meta:
  32. model = TextClassificationProject
  33. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  34. read_only_fields = ('image', 'updated_at', 'users')
  35. class SequenceLabelingProjectSerializer(serializers.ModelSerializer):
  36. class Meta:
  37. model = SequenceLabelingProject
  38. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  39. read_only_fields = ('image', 'updated_at', 'users')
  40. class Seq2seqProjectSerializer(serializers.ModelSerializer):
  41. class Meta:
  42. model = Seq2seqProject
  43. fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  44. read_only_fields = ('image', 'updated_at', 'users')
  45. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  46. model_serializer_mapping = {
  47. Project: ProjectSerializer,
  48. TextClassificationProject: TextClassificationProjectSerializer,
  49. SequenceLabelingProject: SequenceLabelingProjectSerializer,
  50. Seq2seqProject: Seq2seqProjectSerializer
  51. }
  52. class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
  53. def get_queryset(self):
  54. view = self.context.get('view', None)
  55. request = self.context.get('request', None)
  56. queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
  57. if not request or not queryset or not view:
  58. return None
  59. return queryset.filter(project=view.kwargs['project_id'])
  60. class DocumentAnnotationSerializer(serializers.ModelSerializer):
  61. # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  62. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  63. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  64. class Meta:
  65. model = DocumentAnnotation
  66. fields = ('id', 'prob', 'label', 'user', 'document')
  67. read_only_fields = ('user', )
  68. class SequenceAnnotationSerializer(serializers.ModelSerializer):
  69. #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  70. label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  71. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  72. class Meta:
  73. model = SequenceAnnotation
  74. fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
  75. read_only_fields = ('user',)
  76. class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
  77. document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  78. class Meta:
  79. model = Seq2seqAnnotation
  80. fields = ('id', 'text', 'user', 'document')
  81. read_only_fields = ('user',)