schema.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from typing import Annotated, Literal
  2. from ninja import Field, ModelSchema
  3. from pydantic import HttpUrl
  4. from glitchtip.schema import CamelSchema
  5. from .constants import RecipientType
  6. from .models import AlertRecipient, ProjectAlert
  7. class EmailAlertRecipientIn(CamelSchema):
  8. recipient_type: Literal[RecipientType.EMAIL]
  9. url: HttpUrl | Literal[""] | None = Field(default="")
  10. class WebhookAlertRecipientIn(CamelSchema):
  11. recipient_type: Literal[
  12. RecipientType.DISCORD,
  13. RecipientType.GENERAL_WEBHOOK,
  14. RecipientType.GOOGLE_CHAT,
  15. ]
  16. url: HttpUrl
  17. AlertRecipientIn = Annotated[
  18. EmailAlertRecipientIn | WebhookAlertRecipientIn,
  19. Field(discriminator="recipient_type"),
  20. ]
  21. class AlertRecipientSchema(CamelSchema, ModelSchema):
  22. class Meta:
  23. model = AlertRecipient
  24. fields = ["id", "recipient_type", "url"]
  25. class ProjectAlertIn(CamelSchema, ModelSchema):
  26. name: str = Field(default="")
  27. alert_recipients: list[AlertRecipientIn] | None = Field(default_factory=list)
  28. class Meta:
  29. model = ProjectAlert
  30. fields = [
  31. "name",
  32. "timespan_minutes",
  33. "quantity",
  34. "uptime",
  35. ]
  36. class ProjectAlertSchema(CamelSchema, ModelSchema):
  37. alert_recipients: list[AlertRecipientSchema] = Field(
  38. validation_alias="alertrecipient_set"
  39. )
  40. class Meta(ProjectAlertIn.Meta):
  41. fields = ["id"] + ProjectAlertIn.Meta.fields