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.

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