From aeb944b281d90e697668c6d859866cb59543272b Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Fri, 26 May 2023 15:32:55 +0200 Subject: [PATCH] dont show properties if no reference amout is given --- cookbook/helper/property_helper.py | 18 +++++++++++------- cookbook/tests/other/test_food_property.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/cookbook/helper/property_helper.py b/cookbook/helper/property_helper.py index d34a306bf..1dce5400e 100644 --- a/cookbook/helper/property_helper.py +++ b/cookbook/helper/property_helper.py @@ -42,13 +42,17 @@ class FoodPropertyHelper: conversions = uch.get_conversions(i) for pt in property_types: found_property = False - for p in i.food.properties.all(): - if p.property_type == pt: - for c in conversions: - if c.unit == i.food.properties_food_unit: - found_property = True - computed_properties[pt.id]['total_value'] += (c.amount / i.food.properties_food_amount) * p.property_amount - computed_properties[pt.id]['food_values'] = self.add_or_create(computed_properties[p.property_type.id]['food_values'], c.food.id, (c.amount / i.food.properties_food_amount) * p.property_amount, c.food) + if i.food.properties_food_amount == 0 or i.food.properties_food_unit is None: + computed_properties[pt.id]['missing_value'] = True + computed_properties[pt.id]['food_values'][i.food.id] = {'id': i.food.id, 'food': i.food.name, 'value': 0} + else: + for p in i.food.properties.all(): + if p.property_type == pt: + for c in conversions: + if c.unit == i.food.properties_food_unit: + found_property = True + computed_properties[pt.id]['total_value'] += (c.amount / i.food.properties_food_amount) * p.property_amount + computed_properties[pt.id]['food_values'] = self.add_or_create(computed_properties[p.property_type.id]['food_values'], c.food.id, (c.amount / i.food.properties_food_amount) * p.property_amount, c.food) if not found_property: computed_properties[pt.id]['missing_value'] = True computed_properties[pt.id]['food_values'][i.food.id] = {'id': i.food.id, 'food': i.food.name, 'value': 0} diff --git a/cookbook/tests/other/test_food_property.py b/cookbook/tests/other/test_food_property.py index aa3bd1145..65c5af63a 100644 --- a/cookbook/tests/other/test_food_property.py +++ b/cookbook/tests/other/test_food_property.py @@ -104,6 +104,17 @@ def test_food_property(space_1, space_2, u1_s1): assert abs(property_values[property_fat.id]['food_values'][food_1.id]['value'] - Decimal(250)) < 0.0001 assert abs(property_values[property_fat.id]['food_values'][food_2.id]['value'] - Decimal(250)) < 0.0001 + print('\n----------- TEST PROPERTY - MISSING FOOD REFERENCE AMOUNT ---------------') + food_1.properties_food_unit = None + food_1.save() + food_2.properties_food_amount = 0 + food_2.save() + + property_values = FoodPropertyHelper(space_1).calculate_recipe_properties(recipe_1) + + assert property_values[property_fat.id]['name'] == property_fat.name + assert property_values[property_fat.id]['total_value'] == 0 + print('\n----------- TEST PROPERTY - SPACE SEPARATION ---------------') property_fat.space = space_2 @@ -114,3 +125,5 @@ def test_food_property(space_1, space_2, u1_s1): property_values = FoodPropertyHelper(space_1).calculate_recipe_properties(recipe_2) assert property_fat.id not in property_values + +