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._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