mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-14 18:58:10 +08:00

* fix: Change on the inheritance model, according to the documentation for the request body parameters. #171 * fix: Changes on the documentation examples to fix the inheritance problem when passing the User class (for create or update only passing the pydantic one) #171 * fix: Changes on the documentation examples to fix the inheritance problem when passing the User class (for create or update only passing the pydantic one) #171 * Put back inheritance on update model and factorize create_update_dict methods Co-authored-by: François Voron <fvoron@gmail.com>
74 lines
1.6 KiB
Python
74 lines
1.6 KiB
Python
import uuid
|
|
from typing import List, Optional, TypeVar
|
|
|
|
from pydantic import UUID4, BaseModel, EmailStr, validator
|
|
|
|
|
|
class CreateUpdateDictModel(BaseModel):
|
|
def create_update_dict(self):
|
|
return self.dict(
|
|
exclude_unset=True,
|
|
exclude={"id", "is_superuser", "is_active", "oauth_accounts"},
|
|
)
|
|
|
|
def create_update_dict_superuser(self):
|
|
return self.dict(exclude_unset=True, exclude={"id"})
|
|
|
|
|
|
class BaseUser(CreateUpdateDictModel):
|
|
"""Base User model."""
|
|
|
|
id: Optional[UUID4] = None
|
|
email: Optional[EmailStr] = None
|
|
is_active: Optional[bool] = True
|
|
is_superuser: Optional[bool] = False
|
|
|
|
@validator("id", pre=True, always=True)
|
|
def default_id(cls, v):
|
|
return v or uuid.uuid4()
|
|
|
|
|
|
class BaseUserCreate(CreateUpdateDictModel):
|
|
email: EmailStr
|
|
password: str
|
|
|
|
|
|
class BaseUserUpdate(BaseUser):
|
|
password: Optional[str]
|
|
|
|
|
|
class BaseUserDB(BaseUser):
|
|
id: UUID4
|
|
hashed_password: str
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
UD = TypeVar("UD", bound=BaseUserDB)
|
|
|
|
|
|
class BaseOAuthAccount(BaseModel):
|
|
"""Base OAuth account model."""
|
|
|
|
id: Optional[UUID4] = None
|
|
oauth_name: str
|
|
access_token: str
|
|
expires_at: int
|
|
refresh_token: Optional[str] = None
|
|
account_id: str
|
|
account_email: str
|
|
|
|
@validator("id", pre=True, always=True)
|
|
def default_id(cls, v):
|
|
return v or uuid.uuid4()
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
|
|
class BaseOAuthAccountMixin(BaseModel):
|
|
"""Adds OAuth accounts list to a User model."""
|
|
|
|
oauth_accounts: List[BaseOAuthAccount] = []
|