base_serializers.py 1008 B

123456789101112131415161718192021222324252627282930313233
  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. id = serializers.CharField(read_only=True)
  9. class Meta:
  10. model = Project
  11. fields = ("platform", "slug", "id", "name")
  12. class ProjectReferenceWithMemberSerializer(ProjectReferenceSerializer):
  13. isMember = serializers.SerializerMethodField()
  14. class Meta(ProjectReferenceSerializer.Meta):
  15. fields = ProjectReferenceSerializer.Meta.fields + ("isMember",)
  16. def get_isMember(self, obj):
  17. user_id = self.context["request"].user.id
  18. teams = obj.team_set.all()
  19. # This is actually more performant than:
  20. # return obj.team_set.filter(members=user).exists()
  21. for team in teams:
  22. if user_id in team.members.all().values_list("user_id", flat=True):
  23. return True
  24. return False