schema.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. avatar: dict[str, Optional[str]] = {"avatarType": "", "avatarUuid": None}
  35. color: str = ""
  36. features: list = []
  37. has_access: bool = True
  38. is_bookmarked: bool = False
  39. is_internal: bool = False
  40. is_member: bool
  41. is_public: bool = False
  42. scrub_ip_addresses: bool = Field(serialization_alias="scrubIPAddresses")
  43. created: datetime = Field(serialization_alias="dateCreated")
  44. platform: Optional[str] = None
  45. class Meta(NameSlugProjectSchema.Meta):
  46. fields = [
  47. "first_event",
  48. "id",
  49. "name",
  50. "scrub_ip_addresses",
  51. "slug",
  52. "created",
  53. "platform",
  54. "event_throttle_rate", # Not in Sentry OSS
  55. ]
  56. class Config(CamelSchema.Config):
  57. pass
  58. class KeyRateLimit(CamelSchema):
  59. window: int
  60. count: int
  61. class ProjectKeyIn(CamelSchema, ModelSchema):
  62. name: Optional[str] = None
  63. rate_limit: Optional[KeyRateLimit] = None
  64. class Meta:
  65. model = ProjectKey
  66. fields = ["name"]
  67. class ProjectKeyUpdate(ProjectKeyIn):
  68. rate_limit: Optional[KeyRateLimit] = None
  69. class Meta(ProjectKeyIn.Meta):
  70. fields = ["name", "is_active"]
  71. class ProjectKeySchema(ProjectKeyUpdate):
  72. """
  73. A project key (DSN) provides a public authentication string used for event
  74. ingestion.
  75. """
  76. date_created: datetime = Field(validation_alias="created")
  77. id: uuid.UUID = Field(validation_alias="public_key")
  78. dsn: dict[str, str]
  79. label: Optional[str] = Field(validation_alias="name")
  80. public: uuid.UUID = Field(validation_alias="public_key")
  81. project_id: int = Field(validation_alias="project_id")
  82. class Meta(ProjectKeyUpdate.Meta):
  83. pass
  84. @staticmethod
  85. def resolve_dsn(obj):
  86. return {
  87. "public": obj.get_dsn(),
  88. "secret": obj.get_dsn(), # Deprecated but required for @sentry/wizard
  89. "security": obj.get_dsn_security(),
  90. }
  91. @staticmethod
  92. def resolve_rate_limit(obj):
  93. if count := obj.rate_limit_count:
  94. return {"window": obj.rate_limit_window, "count": count}
  95. class ProjectOrganizationSchema(ProjectSchema):
  96. organization: OrganizationSchema
  97. class Meta(ProjectSchema.Meta):
  98. pass
  99. class ProjectWithKeysSchema(ProjectOrganizationSchema):
  100. keys: list[ProjectKeySchema] = Field(validation_alias="projectkey_set")
  101. class StrKeyIntValue(RootModel):
  102. root: dict[str, int]