schema.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. projects: list[ProjectTeamSchema]
  37. teams: list[TeamSchema]
  38. class Meta(OrganizationSchema.Meta):
  39. fields = OrganizationSchema.Meta.fields + ["open_membership"]