user.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python
  2. """
  3. @Contact : liuyuqi.gov@msn.cn
  4. @Time : 2024/03/22 18:06:30
  5. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  6. @Desc : user model
  7. """
  8. from sqlmodel import Field, Relationship, SQLModel
  9. class UserBase(SQLModel):
  10. email: str = Field(unique=True, index=True)
  11. is_active: bool = True
  12. is_superuser: bool = False
  13. full_name: str | None = None
  14. class User(UserBase, table=True):
  15. id: int | None = Field(default=None, primary_key=True)
  16. hashed_password: str
  17. items: list["Item"] = Relationship(back_populates="owner")
  18. # Properties to receive via API on creation
  19. class UserCreate(UserBase):
  20. password: str
  21. # TODO replace email str with EmailStr when sqlmodel supports it
  22. class UserCreateOpen(SQLModel):
  23. email: str
  24. password: str
  25. full_name: str | None = None
  26. # Properties to receive via API on update, all are optional
  27. # TODO replace email str with EmailStr when sqlmodel supports it
  28. class UserUpdate(UserBase):
  29. email: str | None = None # type: ignore
  30. password: str | None = None
  31. # TODO replace email str with EmailStr when sqlmodel supports it
  32. class UserUpdateMe(SQLModel):
  33. full_name: str | None = None
  34. email: str | None = None
  35. class UpdatePassword(SQLModel):
  36. current_password: str
  37. new_password: str
  38. # Properties to return via API, id is always required
  39. class UserOut(UserBase):
  40. id: int
  41. class UsersOut(SQLModel):
  42. data: list[UserOut]
  43. count: int