fix: Exporting using ZIP file results in empty fields (#8083)
This commit is contained in:
1 parent
2bcba88a97
commit
ba61719d09
3 files changed
+248
-4
No files matched your search
@@ -567,6 +567,7 @@ const { locales: LOCALES, locale: currentLocale } = useLocales();
|
||||
|
||||
onMounted(() => {
|
||||
locale.value = currentLocale.value;
|
||||
foodStore.actions.refresh();
|
||||
});
|
||||
|
||||
const locales = LOCALES.filter(locale =>
|
||||
|
||||
@@ -19,9 +19,16 @@ from mealie.pkgs import cache
|
||||
from mealie.repos.all_repositories import get_repositories
|
||||
from mealie.repos.repository_factory import AllRepositories
|
||||
from mealie.repos.repository_generic import RepositoryGeneric
|
||||
from mealie.schema import mapper
|
||||
from mealie.schema.household.household import HouseholdInDB, HouseholdRecipeUpdate
|
||||
from mealie.schema.recipe.recipe import CreateRecipe, Recipe, create_recipe_slug
|
||||
from mealie.schema.recipe.recipe_ingredient import RecipeIngredient
|
||||
from mealie.schema.recipe.recipe_ingredient import (
|
||||
CreateIngredientFood,
|
||||
CreateIngredientUnit,
|
||||
RecipeIngredient,
|
||||
SaveIngredientFood,
|
||||
SaveIngredientUnit,
|
||||
)
|
||||
from mealie.schema.recipe.recipe_settings import RecipeSettings
|
||||
from mealie.schema.recipe.recipe_step import RecipeStep
|
||||
from mealie.schema.recipe.recipe_timeline_events import RecipeTimelineEventCreate, TimelineEventType
|
||||
@@ -29,6 +36,7 @@ from mealie.schema.recipe.request_helpers import RecipeDuplicate
|
||||
from mealie.schema.user.user import PrivateUser, UserRatingCreate
|
||||
from mealie.services._base_service import BaseService
|
||||
from mealie.services.household_services.household_service import HouseholdService
|
||||
from mealie.services.parser_services._base import DataMatcher
|
||||
from mealie.services.recipe.recipe_data_service import RecipeDataService
|
||||
|
||||
from .template_service import TemplateService
|
||||
@@ -47,6 +55,7 @@ class RecipeServiceBase(BaseService):
|
||||
if repos.household_id != user.household_id != household.id:
|
||||
raise Exception("household ids do not match")
|
||||
|
||||
self._data_matcher: DataMatcher | None = None
|
||||
self.group_recipes = get_repositories(repos.session, group_id=repos.group_id, household_id=None).recipes
|
||||
"""Recipes repo without a Household filter"""
|
||||
|
||||
@@ -262,6 +271,96 @@ class RecipeService(RecipeServiceBase):
|
||||
new_item = repo.create(data)
|
||||
return new_item.model_dump()
|
||||
|
||||
def _get_data_matcher(self) -> DataMatcher:
|
||||
if self._data_matcher is None:
|
||||
# Exact matches only: zip imports should rematch name/plural/alias/abbreviation,
|
||||
# not fuzzy-merge distinct foods or units from another server.
|
||||
self._data_matcher = DataMatcher(
|
||||
self.repos,
|
||||
food_fuzzy_match_threshold=100,
|
||||
unit_fuzzy_match_threshold=100,
|
||||
)
|
||||
return self._data_matcher
|
||||
|
||||
def _reset_data_matcher(self) -> None:
|
||||
self._data_matcher = None
|
||||
|
||||
@staticmethod
|
||||
def _non_empty_str(value: Any) -> str | None:
|
||||
if isinstance(value, str) and value:
|
||||
return value
|
||||
return None
|
||||
|
||||
def _transform_food(self, data: dict[str, Any] | Any) -> dict[str, Any] | None:
|
||||
# Ensures the food from the source instance exists in this group, creating it if necessary.
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
|
||||
name = self._non_empty_str(data.get("name"))
|
||||
if not name:
|
||||
return None
|
||||
|
||||
if data.get("id") and self.repos.ingredient_foods.get_one(data["id"]):
|
||||
return data
|
||||
|
||||
existing_food = self._get_data_matcher().find_food_match(name)
|
||||
if existing_food:
|
||||
data["id"] = existing_food.id
|
||||
return data
|
||||
|
||||
create_data = CreateIngredientFood(
|
||||
name=name,
|
||||
plural_name=data.get("plural_name"),
|
||||
description=data.get("description") or "",
|
||||
aliases=data.get("aliases") or [],
|
||||
label_id=None,
|
||||
)
|
||||
save_data = mapper.cast(create_data, SaveIngredientFood, group_id=self.user.group_id)
|
||||
new_food = self.repos.ingredient_foods.create(save_data)
|
||||
data["id"] = new_food.id
|
||||
self._reset_data_matcher()
|
||||
return data
|
||||
|
||||
def _transform_unit(self, data: dict[str, Any] | Any) -> dict[str, Any] | None:
|
||||
# Ensures the unit from the source instance exists in this group, creating it if necessary.
|
||||
if not isinstance(data, dict):
|
||||
return None
|
||||
|
||||
name = self._non_empty_str(data.get("name"))
|
||||
abbreviation = self._non_empty_str(data.get("abbreviation"))
|
||||
match_value = name or abbreviation
|
||||
if not match_value:
|
||||
return None
|
||||
|
||||
if data.get("id") and self.repos.ingredient_units.get_one(data["id"]):
|
||||
return data
|
||||
|
||||
matcher = self._get_data_matcher()
|
||||
existing_unit = matcher.find_unit_match(match_value)
|
||||
if existing_unit is None and abbreviation and abbreviation != match_value:
|
||||
existing_unit = matcher.find_unit_match(abbreviation)
|
||||
if existing_unit:
|
||||
data["id"] = existing_unit.id
|
||||
return data
|
||||
|
||||
create_data = CreateIngredientUnit(
|
||||
name=match_value,
|
||||
plural_name=data.get("plural_name"),
|
||||
description=data.get("description") or "",
|
||||
abbreviation=abbreviation or "",
|
||||
plural_abbreviation=data.get("plural_abbreviation") or "",
|
||||
fraction=data.get("fraction", True),
|
||||
use_abbreviation=data.get("use_abbreviation", False),
|
||||
aliases=data.get("aliases") or [],
|
||||
standard_quantity=data.get("standard_quantity"),
|
||||
standard_unit=data.get("standard_unit"),
|
||||
)
|
||||
save_data = mapper.cast(create_data, SaveIngredientUnit, group_id=self.user.group_id)
|
||||
new_unit = self.repos.ingredient_units.create(save_data)
|
||||
data["id"] = new_unit.id
|
||||
self._reset_data_matcher()
|
||||
return data
|
||||
|
||||
def _process_recipe_data(self, key: str, data: list | dict | Any):
|
||||
if isinstance(data, list):
|
||||
return [self._process_recipe_data(key, item) for item in data]
|
||||
@@ -280,12 +379,15 @@ class RecipeService(RecipeServiceBase):
|
||||
data["group_id"] = str(self.user.group_id)
|
||||
data["household_id"] = str(self.user.household_id)
|
||||
|
||||
# make sure categories and tags are valid
|
||||
# make sure categories and tags and food/unit are valid
|
||||
if key == "recipe_category":
|
||||
return self._transform_category_or_tag(data, self.repos.categories)
|
||||
elif key == "tags":
|
||||
return self._transform_category_or_tag(data, self.repos.tags)
|
||||
|
||||
elif key == "food":
|
||||
return self._transform_food(data)
|
||||
elif key == "unit":
|
||||
return self._transform_unit(data)
|
||||
# recursively process other objects
|
||||
for k, v in data.items():
|
||||
data[k] = self._process_recipe_data(k, v)
|
||||
|
||||
@@ -25,7 +25,14 @@ from mealie.schema.cookbook.cookbook import SaveCookBook
|
||||
from mealie.schema.labels.multi_purpose_label import MultiPurposeLabelSave
|
||||
from mealie.schema.recipe.recipe import Recipe, RecipeCategory, RecipeSummary, RecipeTag
|
||||
from mealie.schema.recipe.recipe_category import CategorySave, TagSave
|
||||
from mealie.schema.recipe.recipe_ingredient import RecipeIngredient, SaveIngredientFood
|
||||
from mealie.schema.recipe.recipe_ingredient import (
|
||||
CreateIngredientFoodAlias,
|
||||
IngredientFood,
|
||||
IngredientUnit,
|
||||
RecipeIngredient,
|
||||
SaveIngredientFood,
|
||||
SaveIngredientUnit,
|
||||
)
|
||||
from mealie.schema.recipe.recipe_notes import RecipeNote
|
||||
from mealie.schema.recipe.recipe_tool import RecipeToolSave
|
||||
from mealie.services.recipe.recipe_data_service import RecipeDataService
|
||||
@@ -615,6 +622,140 @@ def test_create_recipe_from_zip_invalid_tag(api_client: TestClient, unique_user:
|
||||
assert fetched_recipe.tags[0].slug == invalid_name
|
||||
|
||||
|
||||
def test_create_recipe_from_zip_existing_food_and_unit_wrong_ids(
|
||||
api_client: TestClient, unique_user: TestUser, tempdir: str
|
||||
):
|
||||
database = unique_user.repos
|
||||
food = database.ingredient_foods.create(SaveIngredientFood(name=random_string(), group_id=unique_user.group_id))
|
||||
unit = database.ingredient_units.create(SaveIngredientUnit(name=random_string(), group_id=unique_user.group_id))
|
||||
invalid_food = IngredientFood(id=uuid4(), name=food.name)
|
||||
invalid_unit = IngredientUnit(id=uuid4(), name=unit.name)
|
||||
|
||||
recipe_name = random_string()
|
||||
recipe = Recipe(
|
||||
id=uuid4(),
|
||||
user_id=unique_user.user_id,
|
||||
group_id=unique_user.group_id,
|
||||
name=recipe_name,
|
||||
slug=recipe_name,
|
||||
recipe_ingredient=[RecipeIngredient(note="", food=invalid_food, unit=invalid_unit, quantity=1)],
|
||||
)
|
||||
|
||||
r = api_client.post(api_routes.recipes_create_zip, files=zip_recipe(tempdir, recipe), headers=unique_user.token)
|
||||
assert r.status_code == 201
|
||||
|
||||
fetched_recipe = database.recipes.get_by_slug(unique_user.group_id, recipe.slug)
|
||||
assert fetched_recipe
|
||||
assert fetched_recipe.recipe_ingredient
|
||||
assert len(fetched_recipe.recipe_ingredient) == 1
|
||||
assert fetched_recipe.recipe_ingredient[0].food
|
||||
assert fetched_recipe.recipe_ingredient[0].unit
|
||||
assert str(fetched_recipe.recipe_ingredient[0].food.id) == str(food.id)
|
||||
assert str(fetched_recipe.recipe_ingredient[0].unit.id) == str(unit.id)
|
||||
|
||||
|
||||
def test_create_recipe_from_zip_existing_food_alias(api_client: TestClient, unique_user: TestUser, tempdir: str):
|
||||
database = unique_user.repos
|
||||
alias_name = random_string()
|
||||
food = database.ingredient_foods.create(
|
||||
SaveIngredientFood(
|
||||
name=random_string(),
|
||||
group_id=unique_user.group_id,
|
||||
aliases=[CreateIngredientFoodAlias(name=alias_name)],
|
||||
)
|
||||
)
|
||||
invalid_food = IngredientFood(id=uuid4(), name=alias_name)
|
||||
|
||||
recipe_name = random_string()
|
||||
recipe = Recipe(
|
||||
id=uuid4(),
|
||||
user_id=unique_user.user_id,
|
||||
group_id=unique_user.group_id,
|
||||
name=recipe_name,
|
||||
slug=recipe_name,
|
||||
recipe_ingredient=[RecipeIngredient(note="", food=invalid_food, quantity=1)],
|
||||
)
|
||||
|
||||
r = api_client.post(api_routes.recipes_create_zip, files=zip_recipe(tempdir, recipe), headers=unique_user.token)
|
||||
assert r.status_code == 201
|
||||
|
||||
fetched_recipe = database.recipes.get_by_slug(unique_user.group_id, recipe.slug)
|
||||
assert fetched_recipe
|
||||
assert fetched_recipe.recipe_ingredient
|
||||
assert fetched_recipe.recipe_ingredient[0].food
|
||||
assert str(fetched_recipe.recipe_ingredient[0].food.id) == str(food.id)
|
||||
|
||||
|
||||
def test_create_recipe_from_zip_existing_unit_abbreviation(api_client: TestClient, unique_user: TestUser, tempdir: str):
|
||||
database = unique_user.repos
|
||||
abbreviation = random_string(5)
|
||||
unit = database.ingredient_units.create(
|
||||
SaveIngredientUnit(
|
||||
name=random_string(),
|
||||
abbreviation=abbreviation,
|
||||
group_id=unique_user.group_id,
|
||||
)
|
||||
)
|
||||
invalid_unit = IngredientUnit(id=uuid4(), name=abbreviation)
|
||||
|
||||
recipe_name = random_string()
|
||||
recipe = Recipe(
|
||||
id=uuid4(),
|
||||
user_id=unique_user.user_id,
|
||||
group_id=unique_user.group_id,
|
||||
name=recipe_name,
|
||||
slug=recipe_name,
|
||||
recipe_ingredient=[RecipeIngredient(note="", unit=invalid_unit, quantity=1)],
|
||||
)
|
||||
|
||||
r = api_client.post(api_routes.recipes_create_zip, files=zip_recipe(tempdir, recipe), headers=unique_user.token)
|
||||
assert r.status_code == 201
|
||||
|
||||
fetched_recipe = database.recipes.get_by_slug(unique_user.group_id, recipe.slug)
|
||||
assert fetched_recipe
|
||||
assert fetched_recipe.recipe_ingredient
|
||||
assert fetched_recipe.recipe_ingredient[0].unit
|
||||
assert str(fetched_recipe.recipe_ingredient[0].unit.id) == str(unit.id)
|
||||
|
||||
|
||||
def test_create_recipe_from_zip_invalid_food_and_unit(api_client: TestClient, unique_user: TestUser, tempdir: str):
|
||||
database = unique_user.repos
|
||||
food_name = random_string()
|
||||
unit_name = random_string()
|
||||
invalid_food = IngredientFood(id=uuid4(), name=food_name)
|
||||
invalid_unit = IngredientUnit(id=uuid4(), name=unit_name)
|
||||
|
||||
recipe_name = random_string()
|
||||
recipe = Recipe(
|
||||
id=uuid4(),
|
||||
user_id=unique_user.user_id,
|
||||
group_id=unique_user.group_id,
|
||||
name=recipe_name,
|
||||
slug=recipe_name,
|
||||
recipe_ingredient=[
|
||||
RecipeIngredient(note="", food=invalid_food, unit=invalid_unit, quantity=1),
|
||||
RecipeIngredient(note="", food=IngredientFood(id=uuid4(), name=food_name), quantity=2),
|
||||
],
|
||||
)
|
||||
|
||||
r = api_client.post(api_routes.recipes_create_zip, files=zip_recipe(tempdir, recipe), headers=unique_user.token)
|
||||
assert r.status_code == 201
|
||||
|
||||
fetched_recipe = database.recipes.get_by_slug(unique_user.group_id, recipe.slug)
|
||||
assert fetched_recipe
|
||||
assert fetched_recipe.recipe_ingredient
|
||||
assert len(fetched_recipe.recipe_ingredient) == 2
|
||||
assert fetched_recipe.recipe_ingredient[0].food
|
||||
assert fetched_recipe.recipe_ingredient[0].unit
|
||||
assert fetched_recipe.recipe_ingredient[0].food.name == food_name
|
||||
assert fetched_recipe.recipe_ingredient[0].unit.name == unit_name
|
||||
assert str(fetched_recipe.recipe_ingredient[0].food.id) != str(invalid_food.id)
|
||||
assert str(fetched_recipe.recipe_ingredient[0].unit.id) != str(invalid_unit.id)
|
||||
# the same new food should be reused for the second ingredient
|
||||
assert fetched_recipe.recipe_ingredient[1].food
|
||||
assert str(fetched_recipe.recipe_ingredient[1].food.id) == str(fetched_recipe.recipe_ingredient[0].food.id)
|
||||
|
||||
|
||||
def test_read_update(
|
||||
api_client: TestClient,
|
||||
unique_user: TestUser,
|
||||
|
||||
Reference in new issue
Block a user