base_serializers.py 960 B

12345678910111213141516171819202122232425262728293031
  1. from rest_framework import serializers
  2. from ..models import Project
  3. class ProjectReferenceSerializer(serializers.ModelSerializer):
  4. """
  5. Non-detailed view used in:
  6. - /api/0/projects/<org-slug>/project-slug>/issues/
  7. """
  8. class Meta:
  9. model = Project
  10. fields = ("platform", "slug", "id", "name")
  11. class ProjectReferenceWithMemberSerializer(ProjectReferenceSerializer):
  12. isMember = serializers.SerializerMethodField()
  13. class Meta(ProjectReferenceSerializer.Meta):
  14. fields = ProjectReferenceSerializer.Meta.fields + ("isMember",)
  15. def get_isMember(self, obj):
  16. user_id = self.context["request"].user.id
  17. teams = obj.team_set.all()
  18. # This is actually more performant than:
  19. # return obj.team_set.filter(members=user).exists()
  20. for team in teams:
  21. if user_id in team.members.all().values_list("user_id", flat=True):
  22. return True
  23. return False