mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-11-02 12:21:53 +08:00
Native model and generic ID (#971)
* Use a generic Protocol model for User instead of Pydantic
* Remove UserDB Pydantic schema
* Harmonize schema variable naming to avoid confusions
* Revamp OAuth account model management
* Revamp AccessToken DB strategy to adopt generic model approach
* Make ID a generic instead of forcing UUIDs
* Improve generic typing
* Improve Strategy typing
* Tweak base DB typing
* Don't set Pydantic schemas on FastAPIUsers class: pass it directly on router creation
* Add IntegerIdMixin and export related classes
* Start to revamp doc for V10
* Revamp OAuth documentation
* Fix code highlights
* Write the 9.x.x ➡️ 10.x.x migration doc
* Fix pyproject.toml
This commit is contained in:
74
fastapi_users/schemas.py
Normal file
74
fastapi_users/schemas.py
Normal file
@ -0,0 +1,74 @@
|
||||
from typing import Generic, List, Optional, TypeVar
|
||||
|
||||
from pydantic import BaseModel, EmailStr
|
||||
|
||||
from fastapi_users import models
|
||||
|
||||
|
||||
class CreateUpdateDictModel(BaseModel):
|
||||
def create_update_dict(self):
|
||||
return self.dict(
|
||||
exclude_unset=True,
|
||||
exclude={
|
||||
"id",
|
||||
"is_superuser",
|
||||
"is_active",
|
||||
"is_verified",
|
||||
"oauth_accounts",
|
||||
},
|
||||
)
|
||||
|
||||
def create_update_dict_superuser(self):
|
||||
return self.dict(exclude_unset=True, exclude={"id"})
|
||||
|
||||
|
||||
class BaseUser(Generic[models.ID], CreateUpdateDictModel):
|
||||
"""Base User model."""
|
||||
|
||||
id: models.ID
|
||||
email: EmailStr
|
||||
is_active: bool = True
|
||||
is_superuser: bool = False
|
||||
is_verified: bool = False
|
||||
|
||||
|
||||
class BaseUserCreate(CreateUpdateDictModel):
|
||||
email: EmailStr
|
||||
password: str
|
||||
is_active: Optional[bool] = True
|
||||
is_superuser: Optional[bool] = False
|
||||
is_verified: Optional[bool] = False
|
||||
|
||||
|
||||
class BaseUserUpdate(CreateUpdateDictModel):
|
||||
password: Optional[str]
|
||||
email: Optional[EmailStr]
|
||||
is_active: Optional[bool]
|
||||
is_superuser: Optional[bool]
|
||||
is_verified: Optional[bool]
|
||||
|
||||
|
||||
U = TypeVar("U", bound=BaseUser)
|
||||
UC = TypeVar("UC", bound=BaseUserCreate)
|
||||
UU = TypeVar("UU", bound=BaseUserUpdate)
|
||||
|
||||
|
||||
class BaseOAuthAccount(Generic[models.ID], BaseModel):
|
||||
"""Base OAuth account model."""
|
||||
|
||||
id: models.ID
|
||||
oauth_name: str
|
||||
access_token: str
|
||||
expires_at: Optional[int] = None
|
||||
refresh_token: Optional[str] = None
|
||||
account_id: str
|
||||
account_email: str
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class BaseOAuthAccountMixin(BaseModel):
|
||||
"""Adds OAuth accounts list to a User model."""
|
||||
|
||||
oauth_accounts: List[BaseOAuthAccount] = []
|
||||
Reference in New Issue
Block a user