feat: Upgrade to Pydantic V2 (#3134)

* bumped pydantic
This commit is contained in:
Michael Genson
2024-02-11 10:47:37 -06:00
committed by GitHub
parent 248459671e
commit 7a107584c7
129 changed files with 1138 additions and 833 deletions

View File

@@ -1,10 +1,10 @@
import enum
from typing import Any, Generic, TypeVar
from typing import Annotated, Any, Generic, TypeVar
from urllib.parse import parse_qs, urlencode, urlsplit, urlunsplit
from humps import camelize
from pydantic import UUID4, BaseModel, validator
from pydantic.generics import GenericModel
from pydantic import UUID4, BaseModel, Field, field_validator
from pydantic_core.core_schema import ValidationInfo
from mealie.schema._mealie import MealieModel
@@ -22,12 +22,12 @@ class OrderByNullPosition(str, enum.Enum):
class RecipeSearchQuery(MealieModel):
cookbook: UUID4 | str | None
cookbook: UUID4 | str | None = None
require_all_categories: bool = False
require_all_tags: bool = False
require_all_tools: bool = False
require_all_foods: bool = False
search: str | None
search: str | None = None
_search_seed: str | None = None
@@ -38,23 +38,23 @@ class PaginationQuery(MealieModel):
order_by_null_position: OrderByNullPosition | None = None
order_direction: OrderDirection = OrderDirection.desc
query_filter: str | None = None
pagination_seed: str | None = None
pagination_seed: Annotated[str | None, Field(validate_default=True)] = None
@validator("pagination_seed", always=True, pre=True)
def validate_randseed(cls, pagination_seed, values):
if values.get("order_by") == "random" and not pagination_seed:
@field_validator("pagination_seed", mode="before")
def validate_randseed(cls, pagination_seed, info: ValidationInfo):
if info.data.get("order_by") == "random" and not pagination_seed:
raise ValueError("paginationSeed is required when orderBy is random")
return pagination_seed
class PaginationBase(GenericModel, Generic[DataT]):
class PaginationBase(BaseModel, Generic[DataT]):
page: int = 1
per_page: int = 10
total: int = 0
total_pages: int = 0
items: list[DataT]
next: str | None
previous: str | None
next: str | None = None
previous: str | None = None
def _set_next(self, route: str, query_params: dict[str, Any]) -> None:
if self.page >= self.total_pages:

View File

@@ -14,7 +14,7 @@ class ErrorResponse(BaseModel):
This method is an helper to create an object and convert to a dictionary
in the same call, for use while providing details to a HTTPException
"""
return cls(message=message, exception=exception).dict()
return cls(message=message, exception=exception).model_dump()
class SuccessResponse(BaseModel):
@@ -27,7 +27,7 @@ class SuccessResponse(BaseModel):
This method is an helper to create an object and convert to a dictionary
in the same call, for use while providing details to a HTTPException
"""
return cls(message=message).dict()
return cls(message=message).model_dump()
class FileTokenResponse(MealieModel):
@@ -39,4 +39,4 @@ class FileTokenResponse(MealieModel):
This method is an helper to create an object and convert to a dictionary
in the same call, for use while providing details to a HTTPException
"""
return cls(file_token=token).dict()
return cls(file_token=token).model_dump()