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

@@ -129,7 +129,7 @@ class RecipeService(BaseService):
data: Recipe = self._recipe_creation_factory(
self.user,
name=create_data.name,
additional_attrs=create_data.dict(),
additional_attrs=create_data.model_dump(),
)
if isinstance(create_data, CreateRecipe) or create_data.settings is None:
@@ -175,11 +175,11 @@ class RecipeService(BaseService):
# if the item exists, return the actual data
query = repo.get_one(slug, "slug")
if query:
return query.dict()
return query.model_dump()
# otherwise, create the item
new_item = repo.create(data)
return new_item.dict()
return new_item.model_dump()
def _process_recipe_data(self, key: str, data: list | dict | Any):
if isinstance(data, list):
@@ -250,20 +250,21 @@ class RecipeService(BaseService):
"""Duplicates a recipe and returns the new recipe."""
old_recipe = self._get_recipe(old_slug)
new_recipe = old_recipe.copy(exclude={"id", "name", "slug", "image", "comments"})
new_recipe_data = old_recipe.model_dump(exclude={"id", "name", "slug", "image", "comments"}, round_trip=True)
new_recipe = Recipe.model_validate(new_recipe_data)
# Asset images in steps directly link to the original recipe, so we
# need to update them to references to the assets we copy below
def replace_recipe_step(step: RecipeStep) -> RecipeStep:
new_step = step.copy(exclude={"id", "text"})
new_step.id = uuid4()
new_step.text = step.text.replace(str(old_recipe.id), str(new_recipe.id))
new_id = uuid4()
new_text = step.text.replace(str(old_recipe.id), str(new_recipe.id))
new_step = step.model_copy(update={"id": new_id, "text": new_text})
return new_step
# Copy ingredients to make them independent of the original
def copy_recipe_ingredient(ingredient: RecipeIngredient):
new_ingredient = ingredient.copy(exclude={"reference_id"})
new_ingredient.reference_id = uuid4()
new_reference_id = uuid4()
new_ingredient = ingredient.model_copy(update={"reference_id": new_reference_id})
return new_ingredient
new_name = dup_data.name if dup_data.name else old_recipe.name or ""
@@ -284,7 +285,7 @@ class RecipeService(BaseService):
new_recipe = self._recipe_creation_factory(
self.user,
new_name,
additional_attrs=new_recipe.dict(),
additional_attrs=new_recipe.model_dump(),
)
new_recipe = self.repos.recipes.create(new_recipe)
@@ -350,7 +351,9 @@ class RecipeService(BaseService):
if recipe is None:
raise exceptions.NoEntryFound("Recipe not found.")
new_data = self.repos.recipes.by_group(self.group.id).patch(recipe.slug, patch_data.dict(exclude_unset=True))
new_data = self.repos.recipes.by_group(self.group.id).patch(
recipe.slug, patch_data.model_dump(exclude_unset=True)
)
self.check_assets(new_data, recipe.slug)
return new_data

View File

@@ -92,7 +92,7 @@ class TemplateService(BaseService):
save_path = self.temp.joinpath(f"{recipe.slug}.json")
with open(save_path, "w") as f:
f.write(recipe.json(indent=4, by_alias=True))
f.write(recipe.model_dump_json(indent=4, by_alias=True))
return save_path
@@ -115,7 +115,7 @@ class TemplateService(BaseService):
template_text = f.read()
template = Template(template_text)
rendered_text = template.render(recipe=recipe.dict(by_alias=True))
rendered_text = template.render(recipe=recipe.model_dump(by_alias=True))
save_name = f"{recipe.slug}{j2_path.suffix}"
@@ -140,7 +140,7 @@ class TemplateService(BaseService):
zip_temp = self.temp.joinpath(f"{recipe.slug}.zip")
with ZipFile(zip_temp, "w") as myzip:
myzip.writestr(f"{recipe.slug}.json", recipe.json())
myzip.writestr(f"{recipe.slug}.json", recipe.model_dump_json())
if image_asset.is_file():
myzip.write(image_asset, arcname=image_asset.name)