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.

148 lines
4.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. from rest_framework import serializers
  2. from rest_polymorphic.serializers import PolymorphicSerializer
  3. from .models import (
  4. BoundingBoxProject,
  5. ImageCaptioningProject,
  6. ImageClassificationProject,
  7. IntentDetectionAndSlotFillingProject,
  8. Member,
  9. Project,
  10. SegmentationProject,
  11. Seq2seqProject,
  12. SequenceLabelingProject,
  13. Speech2textProject,
  14. Tag,
  15. TextClassificationProject,
  16. )
  17. class MemberSerializer(serializers.ModelSerializer):
  18. username = serializers.SerializerMethodField()
  19. rolename = serializers.SerializerMethodField()
  20. @classmethod
  21. def get_username(cls, instance):
  22. user = instance.user
  23. return user.username if user else None
  24. @classmethod
  25. def get_rolename(cls, instance):
  26. role = instance.role
  27. return role.name if role else None
  28. class Meta:
  29. model = Member
  30. fields = ("id", "user", "role", "username", "rolename")
  31. class TagSerializer(serializers.ModelSerializer):
  32. class Meta:
  33. model = Tag
  34. fields = (
  35. "id",
  36. "project",
  37. "text",
  38. )
  39. read_only_fields = ("id", "project")
  40. class ProjectSerializer(serializers.ModelSerializer):
  41. tags = TagSerializer(many=True, required=False)
  42. author = serializers.SerializerMethodField()
  43. @classmethod
  44. def get_author(cls, instance):
  45. if instance.created_by:
  46. return instance.created_by.username
  47. return ""
  48. class Meta:
  49. model = Project
  50. fields = [
  51. "id",
  52. "name",
  53. "description",
  54. "guideline",
  55. "project_type",
  56. "created_at",
  57. "updated_at",
  58. "random_order",
  59. "author",
  60. "collaborative_annotation",
  61. "single_class_classification",
  62. "allow_member_to_create_label_type",
  63. "is_text_project",
  64. "tags",
  65. ]
  66. read_only_fields = (
  67. "created_at",
  68. "updated_at",
  69. "author",
  70. "is_text_project",
  71. )
  72. def create(self, validated_data):
  73. tags = TagSerializer(data=validated_data.pop("tags", []), many=True)
  74. project = self.Meta.model.objects.create(**validated_data)
  75. tags.is_valid()
  76. tags.save(project=project)
  77. return project
  78. def update(self, instance, validated_data):
  79. # Don't update tags. Please use TagAPI.
  80. validated_data.pop("tags", None)
  81. return super().update(instance, validated_data)
  82. class TextClassificationProjectSerializer(ProjectSerializer):
  83. class Meta(ProjectSerializer.Meta):
  84. model = TextClassificationProject
  85. class SequenceLabelingProjectSerializer(ProjectSerializer):
  86. class Meta(ProjectSerializer.Meta):
  87. model = SequenceLabelingProject
  88. fields = ProjectSerializer.Meta.fields + ["allow_overlapping", "grapheme_mode", "use_relation"]
  89. class Seq2seqProjectSerializer(ProjectSerializer):
  90. class Meta(ProjectSerializer.Meta):
  91. model = Seq2seqProject
  92. class IntentDetectionAndSlotFillingProjectSerializer(ProjectSerializer):
  93. class Meta(ProjectSerializer.Meta):
  94. model = IntentDetectionAndSlotFillingProject
  95. class Speech2textProjectSerializer(ProjectSerializer):
  96. class Meta(ProjectSerializer.Meta):
  97. model = Speech2textProject
  98. class ImageClassificationProjectSerializer(ProjectSerializer):
  99. class Meta(ProjectSerializer.Meta):
  100. model = ImageClassificationProject
  101. class BoundingBoxProjectSerializer(ProjectSerializer):
  102. class Meta(ProjectSerializer.Meta):
  103. model = BoundingBoxProject
  104. class SegmentationProjectSerializer(ProjectSerializer):
  105. class Meta(ProjectSerializer.Meta):
  106. model = SegmentationProject
  107. class ImageCaptioningProjectSerializer(ProjectSerializer):
  108. class Meta(ProjectSerializer.Meta):
  109. model = ImageCaptioningProject
  110. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  111. model_serializer_mapping = {
  112. Project: ProjectSerializer,
  113. **{cls.Meta.model: cls for cls in ProjectSerializer.__subclasses__()},
  114. }