schema.py 3.3 KB

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