extremly innefficent WIP

This commit is contained in:
vabene1111 2023-05-06 20:54:36 +02:00
parent 60f31608b9
commit 2cc7278865
4 changed files with 18 additions and 66 deletions

View File

@ -1,3 +1,4 @@
from cookbook.helper.unit_conversion_helper import UnitConversionHelper
from cookbook.models import PropertyType, Unit, Food, FoodProperty, Recipe, Step
@ -29,13 +30,20 @@ class FoodPropertyHelper:
# TODO unit conversion support
uch = UnitConversionHelper(self.space)
for i in ingredients:
conversions = [i] # uch.get_conversions(i)
for pt in property_types:
p = i.food.foodproperty_set.filter(space=self.space, property_type=pt).first()
if p:
computed_properties[pt.id]['total_value'] += (i.amount / p.food_amount) * p.property_amount
computed_properties[pt.id]['food_values'] = self.add_or_create(computed_properties[p.property_type.id]['food_values'], i.food.id, (i.amount / p.food_amount) * p.property_amount, i.food)
else:
found_property = False
for p in i.food.foodproperty_set.all():
if p.property_type == pt:
for c in conversions:
if c.unit == p.food_unit:
found_property = True
computed_properties[pt.id]['total_value'] += (c.amount / p.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 / p.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}
@ -50,55 +58,3 @@ class FoodPropertyHelper:
else:
d[key] = {'id': food.id, 'food': food.name, 'value': value}
return d
def generate_debug_recipe(self):
"""
DEBUG FUNCTION ONLY, generates a test recipe
"""
unit_gram = Unit.objects.create(name='gram', base_unit='g', space=self.space)
unit_pcs = Unit.objects.create(name='pcs', base_unit='', space=self.space)
unit_floz1 = Unit.objects.create(name='fl. oz 1', base_unit='imperial_fluid_ounce', space=self.space) # US and UK use different volume systems (US vs imperial)
unit_floz2 = Unit.objects.create(name='fl. oz 2', base_unit='fluid_ounce', space=self.space)
unit_fantasy = Unit.objects.create(name='Fantasy Unit', base_unit='', space=self.space)
food_1 = Food.objects.create(name='Food 1', space=self.space)
food_2 = Food.objects.create(name='Food 2', space=self.space)
property_fat = PropertyType.objects.create(name='Fat', unit='g', space=self.space)
property_calories = PropertyType.objects.create(name='Calories', unit='kcal', space=self.space)
property_nuts = PropertyType.objects.create(name='Nuts', space=self.space)
property_price = PropertyType.objects.create(name='Price', unit='', space=self.space)
food_1_property_fat = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_1, property_amount=50, property_type=property_fat, space=self.space)
food_1_property_nuts = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_1, property_amount=1, property_type=property_nuts, space=self.space)
food_1_property_price = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_1, property_amount=7.50, property_type=property_price, space=self.space)
food_2_property_fat = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_2, property_amount=25, property_type=property_fat, space=self.space)
food_2_property_nuts = FoodProperty.objects.create(food_amount=100, food_unit=unit_gram, food=food_2, property_amount=0, property_type=property_nuts, space=self.space)
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=self.space)
recipe_1 = Recipe.objects.create(name='recipe_1', waiting_time=0, working_time=0, space=self.space, created_by=self.space.created_by)
step_1 = Step.objects.create(instruction='instruction_step_1', space=self.space)
step_1.ingredients.create(amount=500, unit=unit_gram, food=food_1, space=self.space)
step_1.ingredients.create(amount=1000, unit=unit_gram, food=food_2, space=self.space)
recipe_1.steps.add(step_1)
step_2 = Step.objects.create(instruction='instruction_step_1', space=self.space)
step_2.ingredients.create(amount=50, unit=unit_gram, food=food_1, space=self.space)
recipe_1.steps.add(step_2)
class RecipePropertyHelper:
space = None
def __init__(self, space):
"""
Helper to perform recipe property operations
:param space: space to limit scope to
"""
self.space = space
def parse_properties_from_schema(self, schema):
pass

View File

@ -1,6 +1,6 @@
from django.core.cache import caches
from pint import UnitRegistry, UndefinedUnitError, PintError
from decimal import Decimal
from cookbook.helper.cache_helper import CacheHelper
from cookbook.models import Ingredient, Unit
@ -49,7 +49,7 @@ class UnitConversionHelper:
for u in units:
try:
converted = quantitiy.to(u.base_unit)
ingredient = Ingredient(amount=converted.m, unit=u, food=ingredient_list[0].food, )
ingredient = Ingredient(amount=Decimal(converted.m), unit=u, food=ingredient_list[0].food, )
if not any((x.unit.name == ingredient.unit.name or x.unit.base_unit == ingredient.unit.name) for x in pint_converted_list):
pint_converted_list.append(ingredient)
except PintError:
@ -68,11 +68,11 @@ class UnitConversionHelper:
"""
conversions = [ingredient]
if ingredient.unit:
for c in ingredient.unit.unit_conversion_base_relation.filter(space=self.space).all():
for c in ingredient.unit.unit_conversion_base_relation.all():
r = self._uc_convert(c, ingredient.amount, ingredient.unit, ingredient.food)
if r and r not in conversions:
conversions.append(r)
for c in ingredient.unit.unit_conversion_converted_relation.filter(space=self.space).all():
for c in ingredient.unit.unit_conversion_converted_relation.all():
r = self._uc_convert(c, ingredient.amount, ingredient.unit, ingredient.food)
if r and r not in conversions:
conversions.append(r)

View File

@ -826,10 +826,7 @@ class RecipeViewSet(viewsets.ModelViewSet):
'steps__ingredients__food__onhand_users',
'steps__ingredients__food__substitute',
'steps__ingredients__food__child_inherit_fields',
# 'steps__ingredients__food__foodnutrition_set',
# 'steps__ingredients__food__foodnutrition_set__food',
# 'steps__ingredients__food__foodnutrition_set__food_unit',
# 'steps__ingredients__food__foodnutrition_set__nutrition_type',
'steps__ingredients__unit',
'steps__ingredients__unit__unit_conversion_base_relation',
'steps__ingredients__unit__unit_conversion_base_relation__base_unit',

View File

@ -452,4 +452,3 @@ def test(request):
def test2(request):
if not settings.DEBUG:
return HttpResponseRedirect(reverse('index'))
FoodPropertyHelper(request.space).generate_debug_recipe()