schema.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import hashlib
  2. import hmac
  3. from datetime import datetime
  4. from allauth.account.models import EmailAddress
  5. from allauth.socialaccount.models import SocialAccount
  6. from django.conf import settings
  7. from ninja import Field, ModelSchema
  8. from pydantic import EmailStr
  9. from glitchtip.schema import CamelSchema
  10. from .models import User
  11. class SocialAccountSchema(CamelSchema, ModelSchema):
  12. email: EmailStr | None
  13. username: str | None
  14. class Meta:
  15. model = SocialAccount
  16. fields = (
  17. "id",
  18. "provider",
  19. "uid",
  20. "last_login",
  21. "date_joined",
  22. )
  23. @staticmethod
  24. def resolve_email(obj):
  25. if data := obj.extra_data:
  26. # MS oauth uses both principal name and mail
  27. return (
  28. data.get("email") or data.get("userPrincipalName") or data.get("mail")
  29. )
  30. @staticmethod
  31. def resolve_username(obj):
  32. if data := obj.extra_data:
  33. return data.get("username")
  34. class UserOptions(CamelSchema):
  35. timezone: str | None = None
  36. stacktrace_order: int | None = None
  37. language: str | None = None
  38. clock24_hours: bool | None = None
  39. preferred_theme: str | None = None
  40. class UserIn(CamelSchema, ModelSchema):
  41. options: UserOptions
  42. class Meta:
  43. model = User
  44. fields = [
  45. "name",
  46. "options",
  47. ]
  48. class UserSchema(CamelSchema, ModelSchema):
  49. id: str
  50. options: UserOptions
  51. username: EmailStr = Field(validation_alias="email")
  52. created: datetime = Field(alias="dateJoined")
  53. email: EmailStr
  54. has_usable_password: bool = Field(alias="hasPasswordAuth")
  55. socialaccount_set: list[SocialAccountSchema] = Field(alias="identities")
  56. class Meta(UserIn.Meta):
  57. fields = [
  58. "last_login",
  59. "is_superuser",
  60. # "emails",
  61. "id",
  62. "is_active",
  63. "name",
  64. "email",
  65. "options",
  66. ]
  67. @staticmethod
  68. def resolve_id(obj):
  69. return str(obj.id)
  70. class UserDetailSchema(UserSchema):
  71. chatwoot_identifier_hash: str | None = None
  72. @staticmethod
  73. def resolve_chatwoot_identifier_hash(obj):
  74. if settings.CHATWOOT_WEBSITE_TOKEN and settings.CHATWOOT_IDENTITY_TOKEN:
  75. secret = bytes(settings.CHATWOOT_IDENTITY_TOKEN, "utf-8")
  76. message = bytes(str(obj.id), "utf-8")
  77. hash = hmac.new(secret, message, hashlib.sha256)
  78. return hash.hexdigest()
  79. class EmailAddressIn(CamelSchema, ModelSchema):
  80. email: EmailStr
  81. class Meta:
  82. model = EmailAddress
  83. fields = ["email"]
  84. class EmailAddressSchema(CamelSchema, ModelSchema):
  85. isPrimary: bool = Field(validation_alias="primary")
  86. isVerified: bool = Field(validation_alias="verified")
  87. class Meta(EmailAddressIn.Meta):
  88. pass
  89. class UserNotificationsSchema(CamelSchema, ModelSchema):
  90. class Meta:
  91. model = User
  92. fields = ("subscribe_by_default",)
  93. class RecoveryCodesSchema(CamelSchema):
  94. codes: list[str]
  95. class RecoveryCodeSchema(CamelSchema):
  96. code: str