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.

76 lines
2.3 KiB

2 years ago
2 years ago
  1. from rest_framework import serializers
  2. from .models import Assignment, Comment, Example, ExampleState
  3. class CommentSerializer(serializers.ModelSerializer):
  4. class Meta:
  5. model = Comment
  6. fields = (
  7. "id",
  8. "user",
  9. "username",
  10. "example",
  11. "text",
  12. "created_at",
  13. )
  14. read_only_fields = ("user", "example")
  15. class AssignmentSerializer(serializers.ModelSerializer):
  16. class Meta:
  17. model = Assignment
  18. fields = ("id", "assignee", "example", "created_at", "updated_at")
  19. read_only_fields = ("id", "created_at", "updated_at")
  20. class ExampleSerializer(serializers.ModelSerializer):
  21. annotation_approver = serializers.SerializerMethodField()
  22. is_confirmed = serializers.SerializerMethodField()
  23. assignments = serializers.SerializerMethodField()
  24. @classmethod
  25. def get_annotation_approver(cls, instance):
  26. approver = instance.annotations_approved_by
  27. return approver.username if approver else None
  28. def get_is_confirmed(self, instance):
  29. user = self.context.get("request").user
  30. if instance.project.collaborative_annotation:
  31. states = instance.states.all()
  32. else:
  33. states = instance.states.filter(confirmed_by_id=user.id)
  34. return states.count() > 0
  35. def get_assignments(self, instance):
  36. return [
  37. {
  38. "id": assignment.id,
  39. "assignee": assignment.assignee.username,
  40. "assignee_id": assignment.assignee.id,
  41. }
  42. for assignment in instance.assignments.all()
  43. ]
  44. class Meta:
  45. model = Example
  46. fields = [
  47. "id",
  48. "filename",
  49. "meta",
  50. "annotation_approver",
  51. "comment_count",
  52. "text",
  53. "is_confirmed",
  54. "upload_name",
  55. "score",
  56. "assignments",
  57. ]
  58. read_only_fields = ["filename", "is_confirmed", "upload_name", "assignments"]
  59. class ExampleStateSerializer(serializers.ModelSerializer):
  60. class Meta:
  61. model = ExampleState
  62. fields = ("id", "example", "confirmed_by", "confirmed_at")
  63. read_only_fields = ("id", "example", "confirmed_by", "confirmed_at")