fixed mess

This commit is contained in:
vabene1111
2023-02-19 21:09:06 +01:00
parent 985e98c699
commit 2781730778
3 changed files with 53 additions and 25 deletions

View File

@ -44,11 +44,11 @@ class RecipeShoppingEditor():
self.space = space self.space = space
self._kwargs = {**kwargs} self._kwargs = {**kwargs}
mealplan = self._kwargs.get('mealplan', None) self.mealplan = self._kwargs.get('mealplan', None)
if type(mealplan) in [int, float]: if type(self.mealplan) in [int, float]:
self.mealplan = MealPlan.objects.filter(id=self.mealplan, space=self.space).first() self.mealplan = MealPlan.objects.filter(id=self.mealplan, space=self.space)
if type(mealplan) == dict: if type(self.mealplan) == dict:
self.mealplan = MealPlan.objects.filter(id=mealplan['id'], space=self.space).first() self.mealplan = MealPlan.objects.filter(id=self.mealplan['id'], space=self.space).first()
self.id = self._kwargs.get('id', None) self.id = self._kwargs.get('id', None)
self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space) self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space)
@ -65,13 +65,7 @@ class RecipeShoppingEditor():
try: try:
self.servings = float(self._kwargs.get('servings', None)) self.servings = float(self._kwargs.get('servings', None))
except (ValueError, TypeError): except (ValueError, TypeError):
self.servings = getattr(self.recipe, 'servings', None) self.servings = getattr(self._shopping_list_recipe, 'servings', None) or getattr(self.mealplan, 'servings', None) or 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)
@property @property
def _recipe_servings(self): def _recipe_servings(self):
@ -79,7 +73,7 @@ class RecipeShoppingEditor():
@property @property
def _servings_factor(self): def _servings_factor(self):
return Decimal(self.servings) / Decimal(self._recipe_servings) return Decimal(self.servings)/Decimal(self._recipe_servings)
@property @property
def _shared_users(self): def _shared_users(self):
@ -114,9 +108,12 @@ class RecipeShoppingEditor():
if servings := kwargs.get('servings', None): if servings := kwargs.get('servings', None):
self.servings = float(servings) self.servings = float(servings)
self.mealplan = None if mealplan := kwargs.get('mealplan', None):
if mealplan := kwargs.get('mealplan', None): # it appears this code is never called just init is used, no time to validate if type(mealplan) == dict:
self.mealplan = MealPlan.objects.filter(id=mealplan['id'], space=self.space).fist() 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): elif recipe := kwargs.get('recipe', None):
self.recipe = recipe self.recipe = recipe
@ -203,6 +200,7 @@ class RecipeShoppingEditor():
ShoppingListEntry.objects.filter(id__in=to_delete).delete() ShoppingListEntry.objects.filter(id__in=to_delete).delete()
self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space) self._shopping_list_recipe = self.get_shopping_list_recipe(self.id, self.created_by, self.space)
# # TODO refactor as class # # 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): # def list_from_recipe(list_recipe=None, recipe=None, mealplan=None, servings=None, ingredients=None, created_by=None, space=None, append=False):
# """ # """

View File

@ -118,3 +118,34 @@ def update_food_inheritance(sender, instance=None, created=False, **kwargs):
setattr(child, 'supermarket_category', getattr(instance, 'supermarket_category', None)) setattr(child, 'supermarket_category', getattr(instance, 'supermarket_category', None))
child.save() 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

View File

@ -93,7 +93,7 @@
$t("AddToShopping") $t("AddToShopping")
}}</small> }}</small>
</b-input-group> </b-input-group>
<b-input-group v-if="mealplan_settings.addshopping"> <b-input-group v-if="mealplan_settings.addshopping && !autoMealPlan">
<b-form-checkbox id="reviewShopping" <b-form-checkbox id="reviewShopping"
v-model="mealplan_settings.reviewshopping"/> v-model="mealplan_settings.reviewshopping"/>
<small tabindex="-1" class="form-text text-muted">{{ <small tabindex="-1" class="form-text text-muted">{{
@ -242,14 +242,13 @@ export default {
//TODO properly validate //TODO properly validate
this.$bvModal.hide(this.modal_id) this.$bvModal.hide(this.modal_id)
if ((this.mealplan_settings.addshopping || this.autoMealPlan) && !this.mealplan_settings.reviewshopping) { // only set addshopping if review is not enabled
this.$set(this.entryEditing, 'addshopping', true) this.$set(this.entryEditing, 'addshopping', (this.mealplan_settings.addshopping && !this.mealplan_settings.reviewshopping))
}
if (!('id' in this.entryEditing) || this.entryEditing.id === -1) { if (!('id' in this.entryEditing) || this.entryEditing.id === -1) {
useMealPlanStore().createObject(this.entryEditing).then((r) => { useMealPlanStore().createObject(this.entryEditing).then((r) => {
this.last_created_plan = r.data 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.$nextTick(function () {
this.$bvModal.show(`shopping_999999`) this.$bvModal.show(`shopping_999999`)
}) })