mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-10-29 08:27:43 +08:00
Drop Pydantic v1 support
This commit is contained in:
@ -1,35 +1,15 @@
|
||||
from typing import Any, Generic, TypeVar
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr
|
||||
from pydantic.version import VERSION as PYDANTIC_VERSION
|
||||
|
||||
from fastapi_users import models
|
||||
|
||||
PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.")
|
||||
|
||||
SCHEMA = TypeVar("SCHEMA", bound=BaseModel)
|
||||
|
||||
if PYDANTIC_V2: # pragma: no cover
|
||||
|
||||
def model_dump(model: BaseModel, *args, **kwargs) -> dict[str, Any]:
|
||||
return model.model_dump(*args, **kwargs) # type: ignore
|
||||
|
||||
def model_validate(schema: type[SCHEMA], obj: Any, *args, **kwargs) -> SCHEMA:
|
||||
return schema.model_validate(obj, *args, **kwargs) # type: ignore
|
||||
|
||||
else: # pragma: no cover # type: ignore
|
||||
|
||||
def model_dump(model: BaseModel, *args, **kwargs) -> dict[str, Any]:
|
||||
return model.dict(*args, **kwargs) # type: ignore
|
||||
|
||||
def model_validate(schema: type[SCHEMA], obj: Any, *args, **kwargs) -> SCHEMA:
|
||||
return schema.from_orm(obj) # type: ignore
|
||||
|
||||
|
||||
class CreateUpdateDictModel(BaseModel):
|
||||
def create_update_dict(self):
|
||||
return model_dump(
|
||||
self,
|
||||
return self.model_dump(
|
||||
exclude_unset=True,
|
||||
exclude={
|
||||
"id",
|
||||
@ -41,7 +21,7 @@ class CreateUpdateDictModel(BaseModel):
|
||||
)
|
||||
|
||||
def create_update_dict_superuser(self):
|
||||
return model_dump(self, exclude_unset=True, exclude={"id"})
|
||||
return self.model_dump(exclude_unset=True, exclude={"id"})
|
||||
|
||||
|
||||
class BaseUser(CreateUpdateDictModel, Generic[models.ID]):
|
||||
@ -53,12 +33,7 @@ class BaseUser(CreateUpdateDictModel, Generic[models.ID]):
|
||||
is_superuser: bool = False
|
||||
is_verified: bool = False
|
||||
|
||||
if PYDANTIC_V2: # pragma: no cover
|
||||
model_config = ConfigDict(from_attributes=True) # type: ignore
|
||||
else: # pragma: no cover
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class BaseUserCreate(CreateUpdateDictModel):
|
||||
@ -93,12 +68,7 @@ class BaseOAuthAccount(BaseModel, Generic[models.ID]):
|
||||
account_id: str
|
||||
account_email: str
|
||||
|
||||
if PYDANTIC_V2: # pragma: no cover
|
||||
model_config = ConfigDict(from_attributes=True) # type: ignore
|
||||
else: # pragma: no cover
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
|
||||
class BaseOAuthAccountMixin(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user