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.

143 lines
4.0 KiB

6 years ago
5 years ago
3 years ago
  1. from rest_framework import serializers
  2. from rest_polymorphic.serializers import PolymorphicSerializer
  3. from .models import (Comment, Example, ExampleState,
  4. ImageClassificationProject,
  5. IntentDetectionAndSlotFillingProject, Project,
  6. Seq2seqProject, SequenceLabelingProject,
  7. Speech2textProject, Tag, TextClassificationProject)
  8. class CommentSerializer(serializers.ModelSerializer):
  9. class Meta:
  10. model = Comment
  11. fields = ('id', 'user', 'username', 'example', 'text', 'created_at', )
  12. read_only_fields = ('user', 'example')
  13. class TagSerializer(serializers.ModelSerializer):
  14. class Meta:
  15. model = Tag
  16. fields = ('id', 'project', 'text', )
  17. read_only_fields = ('id', 'project')
  18. class ExampleSerializer(serializers.ModelSerializer):
  19. annotation_approver = serializers.SerializerMethodField()
  20. is_confirmed = serializers.SerializerMethodField()
  21. @classmethod
  22. def get_annotation_approver(cls, instance):
  23. approver = instance.annotations_approved_by
  24. return approver.username if approver else None
  25. def get_is_confirmed(self, instance):
  26. user = self.context.get('request').user
  27. if instance.project.collaborative_annotation:
  28. states = instance.states.all()
  29. else:
  30. states = instance.states.filter(confirmed_by_id=user.id)
  31. return states.count() > 0
  32. class Meta:
  33. model = Example
  34. fields = [
  35. 'id',
  36. 'filename',
  37. 'meta',
  38. 'annotation_approver',
  39. 'comment_count',
  40. 'text',
  41. 'is_confirmed'
  42. ]
  43. read_only_fields = ['filename', 'is_confirmed']
  44. class ExampleStateSerializer(serializers.ModelSerializer):
  45. class Meta:
  46. model = ExampleState
  47. fields = ('id', 'example', 'confirmed_by')
  48. read_only_fields = ('id', 'example', 'confirmed_by')
  49. class ProjectSerializer(serializers.ModelSerializer):
  50. tags = TagSerializer(many=True, required=False)
  51. class Meta:
  52. model = Project
  53. fields = (
  54. 'id',
  55. 'name',
  56. 'description',
  57. 'guideline',
  58. 'project_type',
  59. 'updated_at',
  60. 'random_order',
  61. 'created_by',
  62. 'collaborative_annotation',
  63. 'single_class_classification',
  64. 'is_text_project',
  65. 'can_define_label',
  66. 'can_define_relation',
  67. 'can_define_category',
  68. 'can_define_span',
  69. 'tags'
  70. )
  71. read_only_fields = (
  72. 'updated_at',
  73. 'is_text_project',
  74. 'can_define_label',
  75. 'can_define_relation',
  76. 'can_define_category',
  77. 'can_define_span',
  78. 'tags'
  79. )
  80. class TextClassificationProjectSerializer(ProjectSerializer):
  81. class Meta(ProjectSerializer.Meta):
  82. model = TextClassificationProject
  83. class SequenceLabelingProjectSerializer(ProjectSerializer):
  84. class Meta(ProjectSerializer.Meta):
  85. model = SequenceLabelingProject
  86. fields = ProjectSerializer.Meta.fields + ('allow_overlapping', 'grapheme_mode')
  87. class Seq2seqProjectSerializer(ProjectSerializer):
  88. class Meta(ProjectSerializer.Meta):
  89. model = Seq2seqProject
  90. class IntentDetectionAndSlotFillingProjectSerializer(ProjectSerializer):
  91. class Meta(ProjectSerializer.Meta):
  92. model = IntentDetectionAndSlotFillingProject
  93. class Speech2textProjectSerializer(ProjectSerializer):
  94. class Meta(ProjectSerializer.Meta):
  95. model = Speech2textProject
  96. class ImageClassificationProjectSerializer(ProjectSerializer):
  97. class Meta(ProjectSerializer.Meta):
  98. model = ImageClassificationProject
  99. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  100. model_serializer_mapping = {
  101. Project: ProjectSerializer,
  102. **{
  103. cls.Meta.model: cls for cls in ProjectSerializer.__subclasses__()
  104. }
  105. }