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.

147 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
  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. "is_text_project",
  63. "tags",
  64. ]
  65. read_only_fields = (
  66. "created_at",
  67. "updated_at",
  68. "author",
  69. "is_text_project",
  70. )
  71. def create(self, validated_data):
  72. tags = TagSerializer(data=validated_data.pop("tags", []), many=True)
  73. project = self.Meta.model.objects.create(**validated_data)
  74. tags.is_valid()
  75. tags.save(project=project)
  76. return project
  77. def update(self, instance, validated_data):
  78. # Don't update tags. Please use TagAPI.
  79. validated_data.pop("tags", None)
  80. return super().update(instance, validated_data)
  81. class TextClassificationProjectSerializer(ProjectSerializer):
  82. class Meta(ProjectSerializer.Meta):
  83. model = TextClassificationProject
  84. class SequenceLabelingProjectSerializer(ProjectSerializer):
  85. class Meta(ProjectSerializer.Meta):
  86. model = SequenceLabelingProject
  87. fields = ProjectSerializer.Meta.fields + ["allow_overlapping", "grapheme_mode", "use_relation"]
  88. class Seq2seqProjectSerializer(ProjectSerializer):
  89. class Meta(ProjectSerializer.Meta):
  90. model = Seq2seqProject
  91. class IntentDetectionAndSlotFillingProjectSerializer(ProjectSerializer):
  92. class Meta(ProjectSerializer.Meta):
  93. model = IntentDetectionAndSlotFillingProject
  94. class Speech2textProjectSerializer(ProjectSerializer):
  95. class Meta(ProjectSerializer.Meta):
  96. model = Speech2textProject
  97. class ImageClassificationProjectSerializer(ProjectSerializer):
  98. class Meta(ProjectSerializer.Meta):
  99. model = ImageClassificationProject
  100. class BoundingBoxProjectSerializer(ProjectSerializer):
  101. class Meta(ProjectSerializer.Meta):
  102. model = BoundingBoxProject
  103. class SegmentationProjectSerializer(ProjectSerializer):
  104. class Meta(ProjectSerializer.Meta):
  105. model = SegmentationProject
  106. class ImageCaptioningProjectSerializer(ProjectSerializer):
  107. class Meta(ProjectSerializer.Meta):
  108. model = ImageCaptioningProject
  109. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  110. model_serializer_mapping = {
  111. Project: ProjectSerializer,
  112. **{cls.Meta.model: cls for cls in ProjectSerializer.__subclasses__()},
  113. }