schema.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from datetime import datetime
  2. from typing import Optional
  3. from django.utils.timezone import now
  4. from ninja import Field, ModelSchema, Schema
  5. from apps.projects.schema import NameSlugProjectSchema
  6. from glitchtip.schema import CamelSchema
  7. from .models import Release, ReleaseFile
  8. class ReleaseUpdate(Schema):
  9. ref: Optional[str] = None
  10. released: Optional[datetime] = Field(alias="dateReleased", default_factory=now)
  11. class ReleaseBase(ReleaseUpdate):
  12. version: str = Field(serialization_alias="shortVersion")
  13. class ReleaseIn(ReleaseBase):
  14. projects: list[str]
  15. class ReleaseSchema(CamelSchema, ReleaseBase, ModelSchema):
  16. created: datetime = Field(serialization_alias="dateCreated")
  17. released: Optional[datetime] = Field(serialization_alias="dateReleased")
  18. short_version: str = Field(validation_alias="version")
  19. projects: list[NameSlugProjectSchema]
  20. class Meta:
  21. model = Release
  22. fields = [
  23. "url",
  24. "data",
  25. "deploy_count",
  26. "projects",
  27. "version",
  28. ]
  29. class ReleaseFileSchema(CamelSchema, ModelSchema):
  30. id: str
  31. created: datetime = Field(serialization_alias="dateCreated")
  32. sha1: Optional[str] = Field(validation_alias="file.checksum", default=None)
  33. headers: Optional[dict[str, str]] = Field(
  34. validation_alias="file.headers", default=None
  35. )
  36. size: int = Field(validation_alias="file.size")
  37. class Meta:
  38. model = ReleaseFile
  39. fields = ["name"]
  40. class Config(CamelSchema.Config):
  41. coerce_numbers_to_str = True
  42. class AssembleSchema(Schema):
  43. checksum: str
  44. chunks: list[str]