From 278173077812ea634ffbdca68c18c4b002fc7e21 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sun, 19 Feb 2023 21:09:06 +0100 Subject: [PATCH] fixed mess --- cookbook/helper/shopping_helper.py | 36 +++++++++++------------- cookbook/signals.py | 33 +++++++++++++++++++++- vue/src/components/MealPlanEditModal.vue | 9 +++--- 3 files changed, 53 insertions(+), 25 deletions(-) diff --git a/cookbook/helper/shopping_helper.py b/cookbook/helper/shopping_helper.py index c994cf667..c4a8b0796 100644 --- a/cookbook/helper/shopping_helper.py +++ b/cookbook/helper/shopping_helper.py @@ -44,11 +44,11 @@ class RecipeShoppingEditor(): self.space = space self._kwargs = {**kwargs} - mealplan = self._kwargs.get('mealplan', None) - if type(mealplan) in [int, float]: - self.mealplan = MealPlan.objects.filter(id=self.mealplan, space=self.space).first() - if type(mealplan) == dict: - self.mealplan = MealPlan.objects.filter(id=mealplan['id'], space=self.space).first() + self.mealplan = self._kwargs.get('mealplan', None) + if type(self.mealplan) in [int, float]: + self.mealplan = MealPlan.objects.filter(id=self.mealplan, space=self.space) + if type(self.mealplan) == dict: + self.mealplan = MealPlan.objects.filter(id=self.mealplan['id'], space=self.space).first() self.id = self._kwargs.get('id', None) self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space) @@ -65,13 +65,7 @@ class RecipeShoppingEditor(): try: self.servings = float(self._kwargs.get('servings', None)) except (ValueError, TypeError): - self.servings = getattr(self.recipe, 'servings', None) - - if hasattr(self,'mealplan') and getattr(self.mealplan, 'servings', None): - self.servings = getattr(self.mealplan, 'servings', None) - if hasattr(self, '_shopping_list_recipe') and getattr(self._shopping_list_recipe, 'servings', None): - self.servings = getattr(self._shopping_list_recipe, 'servings', None) - + self.servings = getattr(self._shopping_list_recipe, 'servings', None) or getattr(self.mealplan, 'servings', None) or getattr(self.recipe, 'servings', None) @property def _recipe_servings(self): @@ -79,7 +73,7 @@ class RecipeShoppingEditor(): @property def _servings_factor(self): - return Decimal(self.servings) / Decimal(self._recipe_servings) + return Decimal(self.servings)/Decimal(self._recipe_servings) @property def _shared_users(self): @@ -96,9 +90,9 @@ class RecipeShoppingEditor(): def get_recipe_ingredients(self, id, exclude_onhand=False): if exclude_onhand: - return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space).exclude(food__onhand_users__id__in=[x.id for x in self._shared_users]) + return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space).exclude(food__onhand_users__id__in=[x.id for x in self._shared_users]) else: - return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space) + return Ingredient.objects.filter(step__recipe__id=id, food__ignore_shopping=False, space=self.space) @property def _include_related(self): @@ -114,9 +108,12 @@ class RecipeShoppingEditor(): if servings := kwargs.get('servings', None): self.servings = float(servings) - self.mealplan = None - if mealplan := kwargs.get('mealplan', None): # it appears this code is never called just init is used, no time to validate - self.mealplan = MealPlan.objects.filter(id=mealplan['id'], space=self.space).fist() + if mealplan := kwargs.get('mealplan', None): + if type(mealplan) == dict: + self.mealplan = MealPlan.objects.filter(id=mealplan['id'], space=self.space).first() + else: + self.mealplan = mealplan + self.recipe = mealplan.recipe elif recipe := kwargs.get('recipe', None): self.recipe = recipe @@ -203,6 +200,7 @@ class RecipeShoppingEditor(): ShoppingListEntry.objects.filter(id__in=to_delete).delete() self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space) + # # TODO refactor as class # def list_from_recipe(list_recipe=None, recipe=None, mealplan=None, servings=None, ingredients=None, created_by=None, space=None, append=False): # """ @@ -317,4 +315,4 @@ class RecipeShoppingEditor(): # ) # # return all shopping list items -# return list_recipe +# return list_recipe \ No newline at end of file diff --git a/cookbook/signals.py b/cookbook/signals.py index 1abb4d306..624ffa536 100644 --- a/cookbook/signals.py +++ b/cookbook/signals.py @@ -117,4 +117,35 @@ def update_food_inheritance(sender, instance=None, created=False, **kwargs): if instance.supermarket_category and 'supermarket_category' in inherit_fields: setattr(child, 'supermarket_category', getattr(instance, 'supermarket_category', None)) - child.save() \ No newline at end of file + child.save() + + +@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: + print("MEAL_AUTO_ADD Instance is none") + return + + 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 diff --git a/vue/src/components/MealPlanEditModal.vue b/vue/src/components/MealPlanEditModal.vue index c2200b799..e78e78005 100644 --- a/vue/src/components/MealPlanEditModal.vue +++ b/vue/src/components/MealPlanEditModal.vue @@ -93,7 +93,7 @@ $t("AddToShopping") }} - + {{ @@ -242,14 +242,13 @@ export default { //TODO properly validate this.$bvModal.hide(this.modal_id) - if ((this.mealplan_settings.addshopping || this.autoMealPlan) && !this.mealplan_settings.reviewshopping) { - this.$set(this.entryEditing, 'addshopping', true) - } + // only set addshopping if review is not enabled + this.$set(this.entryEditing, 'addshopping', (this.mealplan_settings.addshopping && !this.mealplan_settings.reviewshopping)) if (!('id' in this.entryEditing) || this.entryEditing.id === -1) { useMealPlanStore().createObject(this.entryEditing).then((r) => { this.last_created_plan = r.data - if (r.data.recipe && (this.mealplan_settings.addshopping || this.autoMealPlan) && this.mealplan_settings.reviewshopping) { + if (r.data.recipe && this.mealplan_settings.addshopping && !this.autoMealPlan && this.mealplan_settings.reviewshopping) { this.$nextTick(function () { this.$bvModal.show(`shopping_999999`) })