serializers.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. eventThrottleRate = serializers.IntegerField(
  39. source="event_throttle_rate",
  40. min_value=0,
  41. max_value=100,
  42. required=False,
  43. ) # GlitchTip field, not in Sentry OSS
  44. class Meta(ProjectReferenceSerializer.Meta):
  45. fields = (
  46. "avatar",
  47. "color",
  48. "features",
  49. "firstEvent",
  50. "hasAccess",
  51. "id",
  52. "isBookmarked",
  53. "isInternal",
  54. "isMember",
  55. "isPublic",
  56. "name",
  57. "scrubIPAddresses",
  58. "slug",
  59. "dateCreated",
  60. "platform",
  61. "eventThrottleRate",
  62. )
  63. read_only_fields = ("slug", "date_added")
  64. def get_avatar(self, obj):
  65. return {"avatarType": "", "avatarUuid": None}
  66. def get_color(self, obj):
  67. return ""
  68. def get_features(self, obj):
  69. return []
  70. def get_hasAccess(self, obj):
  71. return True
  72. def get_isBookmarked(self, obj):
  73. return False
  74. def get_isInternal(self, obj):
  75. return False
  76. def get_isMember(self, obj):
  77. user_id = self.context["request"].user.id
  78. teams = obj.team_set.all()
  79. # This is actually more performant than:
  80. # return obj.team_set.filter(members=user).exists()
  81. for team in teams:
  82. if user_id in team.members.all().values_list("user_id", flat=True):
  83. return True
  84. return False
  85. def get_isPublic(self, obj):
  86. return False
  87. class ProjectSerializer(BaseProjectSerializer):
  88. organization = OrganizationReferenceSerializer(read_only=True)
  89. class Meta(BaseProjectSerializer.Meta):
  90. fields = BaseProjectSerializer.Meta.fields + ("organization",)
  91. class ProjectDetailSerializer(ProjectSerializer):
  92. teams = RelatedTeamSerializer(source="team_set", read_only=True, many=True)
  93. class Meta(ProjectSerializer.Meta):
  94. fields = ProjectSerializer.Meta.fields + ("teams",)
  95. class OrganizationProjectSerializer(BaseProjectSerializer):
  96. teams = RelatedTeamSerializer(source="team_set", read_only=True, many=True)
  97. class Meta(BaseProjectSerializer.Meta):
  98. fields = BaseProjectSerializer.Meta.fields + ("teams",)
  99. class ProjectWithKeysSerializer(ProjectSerializer):
  100. keys = ProjectKeySerializer(many=True, source="projectkey_set")
  101. class Meta(ProjectSerializer.Meta):
  102. fields = ProjectSerializer.Meta.fields + ("keys",)