serializers.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from rest_framework import serializers
  2. from organizations_ext.serializers.base_serializers import (
  3. OrganizationReferenceSerializer,
  4. )
  5. from teams.serializers import RelatedTeamSerializer
  6. from ..models import ProjectKey
  7. from .base_serializers import ProjectReferenceSerializer
  8. class ProjectKeySerializer(serializers.ModelSerializer):
  9. dateCreated = serializers.DateTimeField(source="created", read_only=True)
  10. id = serializers.CharField(source="public_key_hex", read_only=True)
  11. dsn = serializers.SerializerMethodField(read_only=True)
  12. public = serializers.CharField(source="public_key_hex", read_only=True)
  13. projectId = serializers.PrimaryKeyRelatedField(source="project", read_only=True)
  14. class Meta:
  15. model = ProjectKey
  16. fields = ("dateCreated", "dsn", "id", "label", "public", "projectId")
  17. def get_dsn(self, obj):
  18. return {
  19. "public": obj.get_dsn(),
  20. "secret": obj.get_dsn(), # Deprecated but required for @sentry/wizard
  21. "security": obj.get_dsn_security(),
  22. }
  23. class BaseProjectSerializer(ProjectReferenceSerializer):
  24. avatar = serializers.SerializerMethodField()
  25. color = serializers.SerializerMethodField()
  26. dateCreated = serializers.DateTimeField(source="created", read_only=True)
  27. features = serializers.SerializerMethodField()
  28. firstEvent = serializers.DateTimeField(source="first_event", read_only=True)
  29. hasAccess = serializers.SerializerMethodField()
  30. id = serializers.CharField(read_only=True)
  31. isBookmarked = serializers.SerializerMethodField()
  32. isInternal = serializers.SerializerMethodField()
  33. isMember = serializers.SerializerMethodField()
  34. isPublic = serializers.SerializerMethodField()
  35. scrubIPAddresses = serializers.BooleanField(
  36. source="scrub_ip_addresses", required=False
  37. )
  38. class Meta(ProjectReferenceSerializer.Meta):
  39. fields = (
  40. "avatar",
  41. "color",
  42. "features",
  43. "firstEvent",
  44. "hasAccess",
  45. "id",
  46. "isBookmarked",
  47. "isInternal",
  48. "isMember",
  49. "isPublic",
  50. "name",
  51. "scrubIPAddresses",
  52. "slug",
  53. "dateCreated",
  54. "platform",
  55. )
  56. read_only_fields = ("slug", "date_added")
  57. def get_avatar(self, obj):
  58. return {"avatarType": "", "avatarUuid": None}
  59. def get_color(self, obj):
  60. return ""
  61. def get_features(self, obj):
  62. return []
  63. def get_hasAccess(self, obj):
  64. return True
  65. def get_isBookmarked(self, obj):
  66. return False
  67. def get_isInternal(self, obj):
  68. return False
  69. def get_isMember(self, obj):
  70. user_id = self.context["request"].user.id
  71. teams = obj.team_set.all()
  72. # This is actually more performant than:
  73. # return obj.team_set.filter(members=user).exists()
  74. for team in teams:
  75. if user_id in team.members.all().values_list("user_id", flat=True):
  76. return True
  77. return False
  78. def get_isPublic(self, obj):
  79. return False
  80. class ProjectSerializer(BaseProjectSerializer):
  81. organization = OrganizationReferenceSerializer(read_only=True)
  82. class Meta(BaseProjectSerializer.Meta):
  83. fields = BaseProjectSerializer.Meta.fields + ("organization",)
  84. class ProjectDetailSerializer(ProjectSerializer):
  85. teams = RelatedTeamSerializer(source="team_set", read_only=True, many=True)
  86. class Meta(ProjectSerializer.Meta):
  87. fields = ProjectSerializer.Meta.fields + ("teams",)
  88. class OrganizationProjectSerializer(BaseProjectSerializer):
  89. teams = RelatedTeamSerializer(source="team_set", read_only=True, many=True)
  90. class Meta(BaseProjectSerializer.Meta):
  91. fields = BaseProjectSerializer.Meta.fields + ("teams",)
  92. class ProjectWithKeysSerializer(ProjectSerializer):
  93. keys = ProjectKeySerializer(many=True, source="projectkey_set")
  94. class Meta(ProjectSerializer.Meta):
  95. fields = ProjectSerializer.Meta.fields + ("keys",)