mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-09 17:33:12 -05:00
@@ -168,14 +168,20 @@ def auto_init(): # sourcery no-metrics
|
||||
setattr(self, key, instance)
|
||||
|
||||
elif relation_dir == MANYTOONE and not use_list:
|
||||
lookup_attr = get_attr
|
||||
if isinstance(val, dict):
|
||||
val = val.get(get_attr)
|
||||
# Prefer primary key when provided to avoid ambiguous alternate-key lookups
|
||||
if "id" in val and val["id"] is not None:
|
||||
lookup_attr = "id"
|
||||
val = val["id"]
|
||||
else:
|
||||
val = val.get(get_attr)
|
||||
|
||||
if val is None:
|
||||
raise ValueError(f"Expected 'id' to be provided for {key}")
|
||||
raise ValueError(f"Expected '{lookup_attr}' to be provided for {key}")
|
||||
|
||||
if isinstance(val, str | int | UUID):
|
||||
stmt = select(relation_cls).filter_by(**{get_attr: val})
|
||||
stmt = select(relation_cls).filter_by(**{lookup_attr: val})
|
||||
instance = session.execute(stmt).scalars().one_or_none()
|
||||
setattr(self, key, instance)
|
||||
else:
|
||||
|
||||
@@ -463,9 +463,34 @@ class RecipeService(RecipeServiceBase):
|
||||
|
||||
return recipe
|
||||
|
||||
def _resolve_ingredient_sub_recipes(self, update_data: Recipe) -> Recipe:
|
||||
"""Resolve all referenced_recipe slugs to IDs within the current group."""
|
||||
if not update_data.recipe_ingredient:
|
||||
return update_data
|
||||
|
||||
for ingredient in update_data.recipe_ingredient:
|
||||
if ingredient.referenced_recipe:
|
||||
ref = ingredient.referenced_recipe
|
||||
# If no id, resolve by slug
|
||||
if not ref.id and ref.slug:
|
||||
recipe = self.group_recipes.get_by_slug(self.user.group_id, ref.slug)
|
||||
if not recipe:
|
||||
raise exceptions.NoEntryFound(f"Referenced recipe '{ref.slug}' not found in this group")
|
||||
ref.id = recipe.id
|
||||
# If id is provided, verify it belongs to this group
|
||||
elif ref.id:
|
||||
recipe = self.group_recipes.get_one(ref.id, key="id")
|
||||
if not recipe:
|
||||
raise exceptions.NoEntryFound(f"Referenced recipe with id '{ref.id}' not found in this group")
|
||||
|
||||
return update_data
|
||||
|
||||
def update_one(self, slug_or_id: str | UUID, update_data: Recipe) -> Recipe:
|
||||
recipe = self._pre_update_check(slug_or_id, update_data)
|
||||
|
||||
# Resolve sub-recipe references before passing to repository
|
||||
update_data = self._resolve_ingredient_sub_recipes(update_data)
|
||||
|
||||
new_data = self.group_recipes.update(recipe.slug, update_data)
|
||||
self.check_assets(new_data, recipe.slug)
|
||||
return new_data
|
||||
|
||||
Reference in New Issue
Block a user