From 7e350b2f907882732c63cbed7938f7c07cd839db Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sat, 25 Mar 2023 07:59:07 +0100 Subject: [PATCH] improved property calculator --- cookbook/helper/food_property_helper.py | 13 +++++++++---- cookbook/models.py | 4 +++- cookbook/tests/other/test_food_property.py | 9 ++++++--- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/cookbook/helper/food_property_helper.py b/cookbook/helper/food_property_helper.py index 5cbe5f501..58f08af8f 100644 --- a/cookbook/helper/food_property_helper.py +++ b/cookbook/helper/food_property_helper.py @@ -4,19 +4,24 @@ from cookbook.models import FoodPropertyType def calculate_recipe_properties(recipe): ingredients = [] computed_properties = {} + property_types = FoodPropertyType.objects.filter(space=recipe.space).all() for s in recipe.steps.all(): ingredients += s.ingredients.all() - for fpt in FoodPropertyType.objects.filter(space=recipe.space).all(): # TODO is this safe or should I require the request context? + for fpt in property_types: # TODO is this safe or should I require the request context? computed_properties[fpt.id] = {'name': fpt.name, 'food_values': {}, 'total_value': 0} # TODO unit conversion support for i in ingredients: - for p in i.food.foodproperty_set.all(): - computed_properties[p.property_type.id]['total_value'] += (i.amount / p.food_amount) * p.property_amount - computed_properties[p.property_type.id]['food_values'][i.food.id] = add_or_create(computed_properties[p.property_type.id]['food_values'], i.food.id, (i.amount / p.food_amount) * p.property_amount) + for pt in property_types: + p = i.food.foodproperty_set.filter(property_type=pt).first() + if p: + computed_properties[p.property_type.id]['total_value'] += (i.amount / p.food_amount) * p.property_amount + computed_properties[p.property_type.id]['food_values'] = add_or_create(computed_properties[p.property_type.id]['food_values'], i.food.id, (i.amount / p.food_amount) * p.property_amount) + else: + computed_properties[pt.id]['food_values'][i.food.id] = None return computed_properties diff --git a/cookbook/models.py b/cookbook/models.py index 198a0834e..8da3b7600 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -742,12 +742,14 @@ class FoodPropertyType(models.Model, PermissionModelMixin): NUTRITION = 'NUTRITION' ALLERGEN = 'ALLERGEN' PRICE = 'PRICE' + GOAL = 'GOAL' + OTHER = 'OTHER' name = models.CharField(max_length=128) unit = models.CharField(max_length=64, blank=True, null=True) icon = models.CharField(max_length=16, blank=True, null=True) description = models.CharField(max_length=512, blank=True, null=True) - category = models.CharField(max_length=64, choices=((NUTRITION, _('Nutrition')), (ALLERGEN, _('Allergen')), (PRICE, _('PRICE'))), null=True, blank=True) + category = models.CharField(max_length=64, choices=((NUTRITION, _('Nutrition')), (ALLERGEN, _('Allergen')), (PRICE, _('Price')), (GOAL, _('Goal')), (OTHER, _('Other'))), null=True, blank=True) # TODO show if empty property? # TODO formatting property? diff --git a/cookbook/tests/other/test_food_property.py b/cookbook/tests/other/test_food_property.py index ebca1b32d..103bb7741 100644 --- a/cookbook/tests/other/test_food_property.py +++ b/cookbook/tests/other/test_food_property.py @@ -31,12 +31,15 @@ def test_food_property(space_1, u1_s1): food_2_property_price = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_2, property_amount=2.50, property_type=property_price, space=space_1) recipe_1 = Recipe.objects.create(name='recipe_1', waiting_time=0, working_time=0, space=space_1, created_by=auth.get_user(u1_s1)) + step_1 = Step.objects.create(instruction='instruction_step_1', space=space_1) step_1.ingredients.create(amount=500, unit=unit_gram, food=food_1, space=space_1) step_1.ingredients.create(amount=1000, unit=unit_gram, food=food_2, space=space_1) recipe_1.steps.add(step_1) - recipe_1.save() + + step_2 = Step.objects.create(instruction='instruction_step_1', space=space_1) + step_2.ingredients.create(amount=50, unit=unit_gram, food=food_1, space=space_1) + recipe_1.steps.add(step_2) property_values = calculate_recipe_properties(recipe_1) - for p in property_values: - print(p) + print(property_values)