serializers.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 .base_serializers import ProjectReferenceWithMemberSerializer
  7. from ..models import ProjectKey
  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 ProjectSerializer(ProjectReferenceWithMemberSerializer):
  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. isPublic = serializers.SerializerMethodField()
  34. organization = OrganizationReferenceSerializer(read_only=True)
  35. teams = RelatedTeamSerializer(source="team_set", read_only=True, many=True)
  36. scrubIPAddresses = serializers.BooleanField(
  37. source="scrub_ip_addresses", required=False
  38. )
  39. class Meta(ProjectReferenceWithMemberSerializer.Meta):
  40. fields = (
  41. "avatar",
  42. "color",
  43. "features",
  44. "firstEvent",
  45. "hasAccess",
  46. "id",
  47. "isBookmarked",
  48. "isInternal",
  49. "isMember",
  50. "isPublic",
  51. "name",
  52. "organization",
  53. "teams",
  54. "scrubIPAddresses",
  55. "slug",
  56. "dateCreated",
  57. "platform",
  58. )
  59. read_only_fields = ("slug", "date_added")
  60. def get_avatar(self, obj):
  61. return {"avatarType": "", "avatarUuid": None}
  62. def get_color(self, obj):
  63. return ""
  64. def get_features(self, obj):
  65. return []
  66. def get_hasAccess(self, obj):
  67. return True
  68. def get_isBookmarked(self, obj):
  69. return False
  70. def get_isInternal(self, obj):
  71. return False
  72. def get_isPublic(self, obj):
  73. return False
  74. class ProjectWithKeysSerializer(ProjectSerializer):
  75. keys = ProjectKeySerializer(many=True, source="projectkey_set")
  76. class Meta(ProjectSerializer.Meta):
  77. fields = ProjectSerializer.Meta.fields + ("keys",)