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.

47 lines
1.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. from rest_framework import serializers
  2. from .models import Example, ExampleState, Comment
  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 = ["id", "filename", "meta", "annotation_approver", "comment_count", "text", "is_confirmed"]
  32. read_only_fields = ["filename", "is_confirmed"]
  33. class ExampleStateSerializer(serializers.ModelSerializer):
  34. class Meta:
  35. model = ExampleState
  36. fields = ("id", "example", "confirmed_by")
  37. read_only_fields = ("id", "example", "confirmed_by")