improved property calculator

This commit is contained in:
vabene1111
2023-03-25 07:59:07 +01:00
parent 6d5592c1be
commit 7e350b2f90
3 changed files with 18 additions and 8 deletions

View File

@@ -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