fix: sub-recipes in multi group setup (#6652) (#6663)

This commit is contained in:
Imanuel
2026-01-30 19:50:08 +01:00
committed by GitHub
parent c7ae67e7cd
commit 731ee8ae3d
3 changed files with 145 additions and 3 deletions

View File

@@ -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:

View File

@@ -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