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.

116 lines
3.1 KiB

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. Tag,
  5. Project,
  6. TextClassificationProject,
  7. SequenceLabelingProject,
  8. Seq2seqProject,
  9. IntentDetectionAndSlotFillingProject,
  10. Speech2textProject,
  11. ImageClassificationProject,
  12. )
  13. from .models import Member
  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. "tags",
  67. )
  68. class TextClassificationProjectSerializer(ProjectSerializer):
  69. class Meta(ProjectSerializer.Meta):
  70. model = TextClassificationProject
  71. class SequenceLabelingProjectSerializer(ProjectSerializer):
  72. class Meta(ProjectSerializer.Meta):
  73. model = SequenceLabelingProject
  74. fields = ProjectSerializer.Meta.fields + ["allow_overlapping", "grapheme_mode"]
  75. class Seq2seqProjectSerializer(ProjectSerializer):
  76. class Meta(ProjectSerializer.Meta):
  77. model = Seq2seqProject
  78. class IntentDetectionAndSlotFillingProjectSerializer(ProjectSerializer):
  79. class Meta(ProjectSerializer.Meta):
  80. model = IntentDetectionAndSlotFillingProject
  81. class Speech2textProjectSerializer(ProjectSerializer):
  82. class Meta(ProjectSerializer.Meta):
  83. model = Speech2textProject
  84. class ImageClassificationProjectSerializer(ProjectSerializer):
  85. class Meta(ProjectSerializer.Meta):
  86. model = ImageClassificationProject
  87. class ProjectPolymorphicSerializer(PolymorphicSerializer):
  88. model_serializer_mapping = {
  89. Project: ProjectSerializer,
  90. **{cls.Meta.model: cls for cls in ProjectSerializer.__subclasses__()},
  91. }