fixed property calculation issues

- don't count ingredients without amounts as missing properties
- don't fail when property_amount is null
This commit is contained in:
vabene1111 2024-02-18 17:45:56 +01:00
parent 28479e96e9
commit 9e63c5321e

View File

@ -45,12 +45,12 @@ class FoodPropertyHelper:
conversions = uch.get_conversions(i) conversions = uch.get_conversions(i)
for pt in property_types: for pt in property_types:
found_property = False found_property = False
if i.food.properties_food_amount == 0 or i.food.properties_food_unit is None: if i.food.properties_food_amount == 0 or i.food.properties_food_unit is None: # if food is configured incorrectly
computed_properties[pt.id]['food_values'][i.food.id] = {'id': i.food.id, 'food': i.food.name, 'value': None} computed_properties[pt.id]['food_values'][i.food.id] = {'id': i.food.id, 'food': i.food.name, 'value': None}
computed_properties[pt.id]['missing_value'] = i.food.properties_food_unit is None computed_properties[pt.id]['missing_value'] = True
else: else:
for p in i.food.properties.all(): for p in i.food.properties.all():
if p.property_type == pt: if p.property_type == pt and p.property_amount is not None:
for c in conversions: for c in conversions:
if c.unit == i.food.properties_food_unit: if c.unit == i.food.properties_food_unit:
found_property = True found_property = True
@ -58,13 +58,17 @@ class FoodPropertyHelper:
computed_properties[pt.id]['food_values'] = self.add_or_create( 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) 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: if not found_property:
computed_properties[pt.id]['missing_value'] = True if i.amount == 0: # don't count ingredients without an amount as missing
computed_properties[pt.id]['food_values'][i.food.id] = {'id': i.food.id, 'food': i.food.name, 'value': None} computed_properties[pt.id]['missing_value'] = computed_properties[pt.id]['missing_value'] or False # don't override if another food was already missing
computed_properties[pt.id]['food_values'][i.food.id] = {'id': i.food.id, 'food': i.food.name, 'value': 0}
else:
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': None}
return computed_properties return computed_properties
# small dict helper to add to existing key or create new, probably a better way of doing this # small dict helper to add to existing key or create new, probably a better way of doing this
# TODO move to central helper ? # TODO move to central helper ? --> use defaultdict
@staticmethod @staticmethod
def add_or_create(d, key, value, food): def add_or_create(d, key, value, food):
if key in d: if key in d: