serializers.py 3.9 KB

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