schema.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from datetime import datetime
  2. from ninja import Field, ModelSchema, Schema
  3. from apps.organizations_ext.schema import OrganizationSchema
  4. from apps.projects.schema import ProjectSchema
  5. from apps.shared.schema.fields import SlugStr
  6. from glitchtip.schema import CamelSchema
  7. from .models import Team
  8. class TeamIn(Schema):
  9. slug: SlugStr
  10. class TeamSlugSchema(CamelSchema, ModelSchema):
  11. """Used in relations including organization projects"""
  12. class Meta:
  13. model = Team
  14. fields = ["id", "slug"]
  15. class TeamSchema(TeamSlugSchema):
  16. id: str
  17. created: datetime = Field(serialization_alias="dateCreated")
  18. is_member: bool
  19. member_count: int
  20. slug: SlugStr
  21. class Meta(TeamSlugSchema.Meta):
  22. fields = ["id", "slug"]
  23. class Config(CamelSchema.Config):
  24. coerce_numbers_to_str = True
  25. class TeamProjectSchema(TeamSchema):
  26. """TeamSchema with related projects"""
  27. projects: list[ProjectSchema] = []
  28. class ProjectTeamSchema(ProjectSchema):
  29. """Project Schema with related teams"""
  30. teams: list[TeamSlugSchema]
  31. # Depends on teams, thus part of the teams app
  32. class OrganizationDetailSchema(OrganizationSchema, ModelSchema):
  33. projects: list[ProjectTeamSchema]
  34. teams: list[TeamSchema]
  35. class Meta(OrganizationSchema.Meta):
  36. fields = OrganizationSchema.Meta.fields + ["open_membership"]