schema.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from typing import Annotated, Literal, Optional, Union
  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: Optional[Union[HttpUrl, Literal[""]]] = Field(default="")
  10. class WebhookAlertRecipientIn(CamelSchema):
  11. recipient_type: Union[
  12. Literal[
  13. RecipientType.DISCORD,
  14. RecipientType.GENERAL_WEBHOOK,
  15. RecipientType.GOOGLE_CHAT,
  16. ]
  17. ]
  18. url: HttpUrl
  19. AlertRecipientIn = Annotated[
  20. Union[EmailAlertRecipientIn, WebhookAlertRecipientIn],
  21. Field(discriminator="recipient_type"),
  22. ]
  23. class AlertRecipientSchema(CamelSchema, ModelSchema):
  24. class Meta:
  25. model = AlertRecipient
  26. fields = ["id", "recipient_type", "url"]
  27. class ProjectAlertIn(CamelSchema, ModelSchema):
  28. name: str = Field(default="")
  29. alert_recipients: Optional[list[AlertRecipientIn]] = Field(default_factory=list)
  30. class Meta:
  31. model = ProjectAlert
  32. fields = [
  33. "name",
  34. "timespan_minutes",
  35. "quantity",
  36. "uptime",
  37. ]
  38. class ProjectAlertSchema(CamelSchema, ModelSchema):
  39. alert_recipients: list[AlertRecipientSchema] = Field(
  40. validation_alias="alertrecipient_set"
  41. )
  42. class Meta(ProjectAlertIn.Meta):
  43. fields = ["id"] + ProjectAlertIn.Meta.fields