schema.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from datetime import datetime
  2. from typing import Literal, Optional
  3. from ninja import Field, ModelSchema
  4. from pydantic import EmailStr
  5. from apps.users.schema import UserSchema
  6. from glitchtip.schema import CamelSchema
  7. from .models import (
  8. Organization,
  9. OrganizationUser,
  10. )
  11. class OrganizationInSchema(CamelSchema, ModelSchema):
  12. class Meta:
  13. model = Organization
  14. fields = [
  15. "name",
  16. ]
  17. class OrganizationSchema(OrganizationInSchema, ModelSchema):
  18. date_created: datetime = Field(validation_alias="created")
  19. status: dict[str, str] = {"id": "active", "name": "active"}
  20. avatar: dict[str, Optional[str]] = {"avatarType": "", "avatarUuid": None}
  21. is_early_adopter: bool = False
  22. require2fa: bool = False
  23. class Meta(OrganizationInSchema.Meta):
  24. fields = [
  25. "id",
  26. "name",
  27. "slug",
  28. "is_accepting_events",
  29. ]
  30. OrgRole = Literal["member", "admin", "manager", "owner"]
  31. class TeamRole(CamelSchema):
  32. team_slug: str
  33. role: str = ""
  34. """Does nothing at this time"""
  35. class OrganizationUserUpdateSchema(CamelSchema):
  36. org_role: OrgRole
  37. team_roles: list[TeamRole] = Field(default_factory=list)
  38. class OrganizationUserIn(OrganizationUserUpdateSchema):
  39. email: EmailStr
  40. send_invite: bool = True
  41. reinvite: bool = True
  42. class OrganizationUserSchema(CamelSchema, ModelSchema):
  43. id: str
  44. role: str = Field(validation_alias="get_role")
  45. role_name: str = Field(validation_alias="get_role_display")
  46. date_created: datetime = Field(validation_alias="created")
  47. email: str = Field(validation_alias="get_email")
  48. user: Optional[UserSchema] = None
  49. pending: bool
  50. class Meta:
  51. model = OrganizationUser
  52. fields = ["id"]
  53. class Config(CamelSchema.Config):
  54. coerce_numbers_to_str = True
  55. class OrganizationUserDetailSchema(OrganizationUserSchema):
  56. teams: list[str]
  57. isOwner: bool
  58. @staticmethod
  59. def resolve_teams(obj):
  60. return [team.slug for team in obj.teams.all()]
  61. @staticmethod
  62. def resolve_isOwner(obj):
  63. if owner := obj.organization.owner:
  64. return owner.organization_user_id == obj.id
  65. return False
  66. class AcceptInviteIn(CamelSchema):
  67. accept_invite: bool
  68. class OrganizationUserOrganizationSchema(OrganizationUserSchema):
  69. """Organization User Serializer with Organization info"""
  70. organization: OrganizationSchema
  71. class AcceptInviteSchema(AcceptInviteIn):
  72. org_user: OrganizationUserOrganizationSchema