basic food property viewer in recipe view

This commit is contained in:
vabene1111
2023-04-11 16:48:38 +02:00
parent ec083214ef
commit 7d9fcac0c7
5 changed files with 156 additions and 28 deletions

View File

@@ -1,4 +1,4 @@
from cookbook.models import FoodPropertyType
from cookbook.models import FoodPropertyType, Unit, Food, FoodProperty, Recipe, Step
class FoodPropertyHelper:
@@ -25,7 +25,7 @@ class FoodPropertyHelper:
ingredients += s.ingredients.all()
for fpt in property_types: # TODO is this safe or should I require the request context?
computed_properties[fpt.id] = {'id': fpt.id, 'name': fpt.name, 'icon': fpt.icon, 'description': fpt.description, 'unit': fpt.unit, 'food_values': {}, 'total_value': 0}
computed_properties[fpt.id] = {'id': fpt.id, 'name': fpt.name, 'icon': fpt.icon, 'description': fpt.description, 'unit': fpt.unit, 'food_values': {}, 'total_value': 0, 'missing_value': False}
# TODO unit conversion support
@@ -33,9 +33,10 @@ class FoodPropertyHelper:
for pt in property_types:
p = i.food.foodproperty_set.filter(space=self.space, 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'] = 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)
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:
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}
return computed_properties
@@ -49,3 +50,40 @@ 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 = FoodPropertyType.objects.create(name='Fat', unit='g', space=self.space)
property_calories = FoodPropertyType.objects.create(name='Calories', unit='kcal', space=self.space)
property_nuts = FoodPropertyType.objects.create(name='Nuts', space=self.space)
property_price = FoodPropertyType.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)

View File

@@ -23,6 +23,7 @@ from oauth2_provider.models import AccessToken
from cookbook.forms import (CommentForm, Recipe, SearchPreferenceForm, ShoppingPreferenceForm,
SpaceCreateForm, SpaceJoinForm, User,
UserCreateForm, UserNameForm, UserPreference, UserPreferenceForm)
from cookbook.helper.food_property_helper import FoodPropertyHelper
from cookbook.helper.permission_helper import group_required, has_group_permission, share_link_valid, switch_user_active_space
from cookbook.models import (Comment, CookLog, InviteLink, SearchFields, SearchPreference, ShareLink,
Space, ViewLog, UserSpace)
@@ -451,3 +452,4 @@ def test(request):
def test2(request):
if not settings.DEBUG:
return HttpResponseRedirect(reverse('index'))
FoodPropertyHelper(request.space).generate_debug_recipe()