From 484da2200e4d23ac2b69d3ce4274aef94609a2e6 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Fri, 6 May 2022 15:34:28 +0200 Subject: [PATCH] added some debug to auto add shopping signal --- cookbook/signals.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/cookbook/signals.py b/cookbook/signals.py index 32ff71400..8032ada9f 100644 --- a/cookbook/signals.py +++ b/cookbook/signals.py @@ -104,21 +104,32 @@ def update_food_inheritance(sender, instance=None, created=False, **kwargs): @receiver(post_save, sender=MealPlan) def auto_add_shopping(sender, instance=None, created=False, weak=False, **kwargs): + print("MEAL_AUTO_ADD Signal trying to auto add to shopping") if not instance: - return - user = instance.get_owner() - with scope(space=instance.space): - slr_exists = instance.shoppinglistrecipe_set.exists() - - if not created and slr_exists: - for x in instance.shoppinglistrecipe_set.all(): - # assuming that permissions checks for the MealPlan have happened upstream - if instance.servings != x.servings: - SLR = RecipeShoppingEditor(id=x.id, user=user, space=instance.space) - SLR.edit_servings(servings=instance.servings) - elif not user.userpreference.mealplan_autoadd_shopping or not instance.recipe: + print("MEAL_AUTO_ADD Instance is none") return - if created: - SLR = RecipeShoppingEditor(user=user, space=instance.space) - SLR.create(mealplan=instance, servings=instance.servings) + try: + space = instance.get_space() + user = instance.get_owner() + with scope(space=space): + slr_exists = instance.shoppinglistrecipe_set.exists() + + if not created and slr_exists: + for x in instance.shoppinglistrecipe_set.all(): + # assuming that permissions checks for the MealPlan have happened upstream + if instance.servings != x.servings: + SLR = RecipeShoppingEditor(id=x.id, user=user, space=instance.space) + SLR.edit_servings(servings=instance.servings) + elif not user.userpreference.mealplan_autoadd_shopping or not instance.recipe: + print("MEAL_AUTO_ADD No recipe or no setting") + return + + if created: + SLR = RecipeShoppingEditor(user=user, space=space) + SLR.create(mealplan=instance, servings=instance.servings) + print("MEAL_AUTO_ADD Created SLR") + except AttributeError: + pass + +