From 8e9babd1dcaf018e40e0bc8c02f36829cfc1f113 Mon Sep 17 00:00:00 2001 From: tanshu Date: Mon, 15 Nov 2021 21:46:29 +0530 Subject: [PATCH] Fix: Recipe recursion and updating of product price on updating recipe --- brewman/brewman/routers/maintenance.py | 4 +++- brewman/brewman/routers/recipe.py | 24 +++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/brewman/brewman/routers/maintenance.py b/brewman/brewman/routers/maintenance.py index 3d606275..cecb364e 100644 --- a/brewman/brewman/routers/maintenance.py +++ b/brewman/brewman/routers/maintenance.py @@ -30,7 +30,9 @@ def set_maintenance( user: UserToken = Security(get_user, scopes=["maintenance"]), ) -> Maintenance: with SessionFuture() as db: - maintenance: Optional[DbSetting] = db.execute(select(DbSetting).where(DbSetting.name == "Maintenance")).scalar_one_or_none() + maintenance: Optional[DbSetting] = db.execute( + select(DbSetting).where(DbSetting.name == "Maintenance") + ).scalar_one_or_none() if data.enabled is False and maintenance is not None: db.delete(maintenance) maintenance = None diff --git a/brewman/brewman/routers/recipe.py b/brewman/brewman/routers/recipe.py index ce1ae886..08b55430 100644 --- a/brewman/brewman/routers/recipe.py +++ b/brewman/brewman/routers/recipe.py @@ -99,8 +99,7 @@ def save( r_item.recipe_id = recipe.id db.add(r_item) - seen_recipes: Set[uuid.UUID] = set() - check_recursion(recipe, seen_recipes, db) + check_recursion(recipe_sku.product_id, recipe, db) db.commit() set_period(data.valid_from, data.valid_till, request.session) return recipe_info(recipe) @@ -189,8 +188,8 @@ async def update_route( recipe.items.append(RecipeItem(None, product.id, quantity, ingredient_cost)) recipe.cost_price = round(recipe_cost / recipe.recipe_yield, 2) - seen_recipes: Set[uuid.UUID] = set() - check_recursion(recipe, seen_recipes, db) + sku.cost_price = round(recipe.cost_price / recipe.recipe_yield, 2) + check_recursion(sku.product_id, recipe, db) db.commit() set_period(data.valid_from, data.valid_till, request.session) return recipe_info(recipe) @@ -201,13 +200,7 @@ async def update_route( ) -def check_recursion(recipe: Recipe, seen_recipes: Set[uuid.UUID], db: Session) -> None: - if recipe.sku.product_id in seen_recipes: - raise HTTPException( - status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, - detail="Recipe recursion. Some ingredient recipe contains parent recipe.", - ) - seen_recipes.add(recipe.sku.product_id) +def check_recursion(product_id: uuid.UUID, recipe: Recipe, db: Session) -> None: for item in recipe.items: item_recipes: List[Recipe] = ( db.execute( @@ -226,7 +219,12 @@ def check_recursion(recipe: Recipe, seen_recipes: Set[uuid.UUID], db: Session) - .all() ) for sub_recipe in item_recipes: - check_recursion(sub_recipe, seen_recipes, db) + if sub_recipe.sku.product_id == product_id: + raise HTTPException( + status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, + detail="Recipe recursion. Some ingredient recipe contains parent recipe.", + ) + check_recursion(product_id, sub_recipe, db) def get_purchased_product_cost(product: Product, start_date: date, finish_date: date, db: Session) -> Decimal: @@ -272,7 +270,7 @@ def get_sub_product_cost(product: Product, start_date: date, finish_date: date, detail=f"Ingredient {product.name} is not bought and also does not have a recipe for the period.", ) - return round(recipe.cost_price / recipe.sku.fraction / recipe.sku.product_yield, 5) + return round(recipe.cost_price / recipe.sku.fraction / recipe.sku.product_yield, 5) # type: ignore[no-any-return] def update_old_rows(sku_id: uuid.UUID, valid_from: date, valid_till: date, db: Session) -> None: