schema.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. id: str
  13. class Meta:
  14. model = Team
  15. fields = ["id", "slug"]
  16. @staticmethod
  17. def resolve_id(obj):
  18. return str(obj.id)
  19. class TeamSchema(TeamSlugSchema):
  20. created: datetime = Field(serialization_alias="dateCreated")
  21. is_member: bool
  22. member_count: int
  23. slug: SlugStr
  24. class Meta(TeamSlugSchema.Meta):
  25. fields = ["id", "slug"]
  26. class Config(CamelSchema.Config):
  27. coerce_numbers_to_str = True
  28. class TeamProjectSchema(TeamSchema):
  29. """TeamSchema with related projects"""
  30. projects: list[ProjectSchema] = []
  31. class ProjectTeamSchema(ProjectSchema):
  32. """Project Schema with related teams"""
  33. teams: list[TeamSlugSchema]
  34. # Depends on teams, thus part of the teams app
  35. class OrganizationDetailSchema(OrganizationSchema, ModelSchema):
  36. id: int
  37. projects: list[ProjectTeamSchema]
  38. teams: list[TeamSchema]
  39. class Meta(OrganizationSchema.Meta):
  40. fields = OrganizationSchema.Meta.fields + ["open_membership"]