mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-14 18:58:10 +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:
@ -1,81 +1,51 @@
|
||||
import uuid
|
||||
from typing import List, Optional, TypeVar
|
||||
import sys
|
||||
from typing import Generic, List, Optional, TypeVar
|
||||
|
||||
from pydantic import UUID4, BaseModel, EmailStr, Field
|
||||
if sys.version_info < (3, 8):
|
||||
from typing_extensions import Protocol # pragma: no cover
|
||||
else:
|
||||
from typing import Protocol # pragma: no cover
|
||||
|
||||
ID = TypeVar("ID")
|
||||
|
||||
|
||||
class CreateUpdateDictModel(BaseModel):
|
||||
def create_update_dict(self):
|
||||
return self.dict(
|
||||
exclude_unset=True,
|
||||
exclude={
|
||||
"id",
|
||||
"is_superuser",
|
||||
"is_active",
|
||||
"is_verified",
|
||||
"oauth_accounts",
|
||||
},
|
||||
)
|
||||
class UserProtocol(Protocol[ID]):
|
||||
"""User protocol that ORM model should follow."""
|
||||
|
||||
def create_update_dict_superuser(self):
|
||||
return self.dict(exclude_unset=True, exclude={"id"})
|
||||
|
||||
|
||||
class BaseUser(CreateUpdateDictModel):
|
||||
"""Base User model."""
|
||||
|
||||
id: UUID4 = Field(default_factory=uuid.uuid4)
|
||||
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]
|
||||
|
||||
|
||||
class BaseUserDB(BaseUser):
|
||||
id: ID
|
||||
email: str
|
||||
hashed_password: str
|
||||
is_active: bool
|
||||
is_superuser: bool
|
||||
is_verified: bool
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
... # pragma: no cover
|
||||
|
||||
|
||||
U = TypeVar("U", bound=BaseUser)
|
||||
UC = TypeVar("UC", bound=BaseUserCreate)
|
||||
UU = TypeVar("UU", bound=BaseUserUpdate)
|
||||
UD = TypeVar("UD", bound=BaseUserDB)
|
||||
class OAuthAccountProtocol(Protocol[ID]):
|
||||
"""OAuth account protocol that ORM model should follow."""
|
||||
|
||||
|
||||
class BaseOAuthAccount(BaseModel):
|
||||
"""Base OAuth account model."""
|
||||
|
||||
id: UUID4 = Field(default_factory=uuid.uuid4)
|
||||
id: ID
|
||||
oauth_name: str
|
||||
access_token: str
|
||||
expires_at: Optional[int] = None
|
||||
refresh_token: Optional[str] = None
|
||||
expires_at: Optional[int]
|
||||
refresh_token: Optional[str]
|
||||
account_id: str
|
||||
account_email: str
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
def __init__(self, *args, **kwargs) -> None:
|
||||
... # pragma: no cover
|
||||
|
||||
|
||||
class BaseOAuthAccountMixin(BaseModel):
|
||||
"""Adds OAuth accounts list to a User model."""
|
||||
UP = TypeVar("UP", bound=UserProtocol)
|
||||
OAP = TypeVar("OAP", bound=OAuthAccountProtocol)
|
||||
|
||||
oauth_accounts: List[BaseOAuthAccount] = []
|
||||
|
||||
class UserOAuthProtocol(UserProtocol[ID], Generic[ID, OAP]):
|
||||
"""User protocol including a list of OAuth accounts."""
|
||||
|
||||
oauth_accounts: List[OAP]
|
||||
|
||||
|
||||
UOAP = TypeVar("UOAP", bound=UserOAuthProtocol)
|
||||
|
Reference in New Issue
Block a user