schema.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import uuid
  2. from datetime import datetime
  3. from typing import Optional
  4. from ninja import Field, ModelSchema
  5. from pydantic import RootModel
  6. from apps.organizations_ext.schema import OrganizationSchema
  7. from glitchtip.schema import CamelSchema
  8. from .models import Project, ProjectKey
  9. class NameSlugProjectSchema(CamelSchema, ModelSchema):
  10. class Meta:
  11. model = Project
  12. fields = [
  13. "name",
  14. "slug",
  15. ]
  16. class ProjectIn(NameSlugProjectSchema):
  17. platform: Optional[str] = None # This shouldn't be needed, but is.
  18. event_throttle_rate: int = 0 # This shouldn't be needed, but is.
  19. class Meta(NameSlugProjectSchema.Meta):
  20. model = Project
  21. fields = [
  22. "name",
  23. "slug",
  24. "platform",
  25. "event_throttle_rate", # Not in Sentry OSS
  26. # "default_rules",
  27. ]
  28. class ProjectSchema(NameSlugProjectSchema, ModelSchema):
  29. """
  30. A project is an organizational unit for GlitchTip events. It may contain
  31. DSN keys, be connected to exactly one organization, and provide user permissions
  32. through teams.
  33. """
  34. id: str
  35. avatar: dict[str, Optional[str]] = {"avatarType": "", "avatarUuid": None}
  36. color: str = ""
  37. features: list = []
  38. has_access: bool = True
  39. is_bookmarked: bool = False
  40. is_internal: bool = False
  41. is_member: bool
  42. is_public: bool = False
  43. scrub_ip_addresses: bool = Field(serialization_alias="scrubIPAddresses")
  44. created: datetime = Field(serialization_alias="dateCreated")
  45. platform: Optional[str] = None
  46. class Meta(NameSlugProjectSchema.Meta):
  47. fields = [
  48. "first_event",
  49. "id",
  50. "name",
  51. "scrub_ip_addresses",
  52. "slug",
  53. "created",
  54. "platform",
  55. "event_throttle_rate", # Not in Sentry OSS
  56. ]
  57. @staticmethod
  58. def resolve_id(obj):
  59. return str(obj.id)
  60. class Config(CamelSchema.Config):
  61. pass
  62. class KeyRateLimit(CamelSchema):
  63. window: int
  64. count: int
  65. class ProjectKeyIn(CamelSchema, ModelSchema):
  66. name: Optional[str] = None
  67. rate_limit: Optional[KeyRateLimit] = None
  68. class Meta:
  69. model = ProjectKey
  70. fields = ["name"]
  71. class ProjectKeyUpdate(ProjectKeyIn):
  72. rate_limit: Optional[KeyRateLimit] = None
  73. class Meta(ProjectKeyIn.Meta):
  74. fields = ["name", "is_active"]
  75. class ProjectKeySchema(ProjectKeyUpdate):
  76. """
  77. A project key (DSN) provides a public authentication string used for event
  78. ingestion.
  79. """
  80. date_created: datetime = Field(validation_alias="created")
  81. id: uuid.UUID = Field(validation_alias="public_key")
  82. dsn: dict[str, str]
  83. label: Optional[str] = Field(validation_alias="name")
  84. public: uuid.UUID = Field(validation_alias="public_key")
  85. project_id: int = Field(validation_alias="project_id")
  86. class Meta(ProjectKeyUpdate.Meta):
  87. pass
  88. @staticmethod
  89. def resolve_dsn(obj):
  90. return {
  91. "public": obj.get_dsn(),
  92. "secret": obj.get_dsn(), # Deprecated but required for @sentry/wizard
  93. "security": obj.get_dsn_security(),
  94. }
  95. @staticmethod
  96. def resolve_rate_limit(obj):
  97. if count := obj.rate_limit_count:
  98. return {"window": obj.rate_limit_window, "count": count}
  99. class ProjectOrganizationSchema(ProjectSchema):
  100. organization: OrganizationSchema
  101. class Meta(ProjectSchema.Meta):
  102. pass
  103. class ProjectWithKeysSchema(ProjectOrganizationSchema):
  104. keys: list[ProjectKeySchema] = Field(validation_alias="projectkey_set")
  105. class StrKeyIntValue(RootModel):
  106. root: dict[str, int]