schema.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from datetime import datetime
  2. from typing import Literal
  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, str | None] = {"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. "event_throttle_rate",
  30. ]
  31. OrgRole = Literal["member", "admin", "manager", "owner"]
  32. class TeamRole(CamelSchema):
  33. team_slug: str
  34. role: str = ""
  35. """Does nothing at this time"""
  36. class OrganizationUserUpdateSchema(CamelSchema):
  37. org_role: OrgRole
  38. team_roles: list[TeamRole] = Field(default_factory=list)
  39. class OrganizationUserIn(OrganizationUserUpdateSchema):
  40. email: EmailStr
  41. send_invite: bool = True
  42. reinvite: bool = True
  43. class OrganizationUserSchema(CamelSchema, ModelSchema):
  44. id: str
  45. role: OrgRole
  46. role_name: str
  47. created: datetime = Field(alias="dateCreated")
  48. email: str
  49. user: UserSchema | None = None
  50. pending: bool
  51. is_owner: bool
  52. class Meta:
  53. model = OrganizationUser
  54. fields = ["id"]
  55. class Config(CamelSchema.Config):
  56. coerce_numbers_to_str = True
  57. @staticmethod
  58. def resolve_email(obj):
  59. return obj.get_email()
  60. @staticmethod
  61. def resolve_role(obj):
  62. return obj.get_role()
  63. @staticmethod
  64. def resolve_role_name(obj):
  65. return obj.get_role_display()
  66. @staticmethod
  67. def resolve_is_owner(obj):
  68. if owner := obj.organization.owner:
  69. return owner.organization_user_id == obj.id
  70. return False
  71. class OrganizationUserDetailSchema(OrganizationUserSchema):
  72. teams: list[str]
  73. @staticmethod
  74. def resolve_teams(obj):
  75. return [team.slug for team in obj.teams.all()]
  76. class AcceptInviteIn(CamelSchema):
  77. accept_invite: bool
  78. class OrganizationUserOrganizationSchema(OrganizationUserSchema):
  79. """Organization User Serializer with Organization info"""
  80. organization: OrganizationSchema
  81. class AcceptInviteSchema(AcceptInviteIn):
  82. org_user: OrganizationUserOrganizationSchema