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.

50 lines
1.4 KiB

  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 = ('id', 'user', 'username', 'example', 'text', 'created_at', )
  7. read_only_fields = ('user', 'example')
  8. class ExampleSerializer(serializers.ModelSerializer):
  9. annotation_approver = serializers.SerializerMethodField()
  10. is_confirmed = serializers.SerializerMethodField()
  11. @classmethod
  12. def get_annotation_approver(cls, instance):
  13. approver = instance.annotations_approved_by
  14. return approver.username if approver else None
  15. def get_is_confirmed(self, instance):
  16. user = self.context.get('request').user
  17. if instance.project.collaborative_annotation:
  18. states = instance.states.all()
  19. else:
  20. states = instance.states.filter(confirmed_by_id=user.id)
  21. return states.count() > 0
  22. class Meta:
  23. model = Example
  24. fields = [
  25. 'id',
  26. 'filename',
  27. 'meta',
  28. 'annotation_approver',
  29. 'comment_count',
  30. 'text',
  31. 'is_confirmed'
  32. ]
  33. read_only_fields = ['filename', 'is_confirmed']
  34. class ExampleStateSerializer(serializers.ModelSerializer):
  35. class Meta:
  36. model = ExampleState
  37. fields = ('id', 'example', 'confirmed_by')
  38. read_only_fields = ('id', 'example', 'confirmed_by')