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.

56 lines
1.6 KiB

2 years ago
2 years ago
2 years ago
  1. from rest_framework import serializers
  2. from .models import 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 ExampleSerializer(serializers.ModelSerializer):
  16. annotation_approver = serializers.SerializerMethodField()
  17. is_confirmed = serializers.SerializerMethodField()
  18. @classmethod
  19. def get_annotation_approver(cls, instance):
  20. approver = instance.annotations_approved_by
  21. return approver.username if approver else None
  22. def get_is_confirmed(self, instance):
  23. user = self.context.get("request").user
  24. if instance.project.collaborative_annotation:
  25. states = instance.states.all()
  26. else:
  27. states = instance.states.filter(confirmed_by_id=user.id)
  28. return states.count() > 0
  29. class Meta:
  30. model = Example
  31. fields = [
  32. "id",
  33. "filename",
  34. "meta",
  35. "annotation_approver",
  36. "comment_count",
  37. "text",
  38. "is_confirmed",
  39. "upload_name",
  40. ]
  41. read_only_fields = ["filename", "is_confirmed", "upload_name"]
  42. class ExampleStateSerializer(serializers.ModelSerializer):
  43. class Meta:
  44. model = ExampleState
  45. fields = ("id", "example", "confirmed_by", "confirmed_at")
  46. read_only_fields = ("id", "example", "confirmed_by", "confirmed_at")