mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2025-12-24 02:39:20 -05:00
many more unit conversions
This commit is contained in:
@@ -19,10 +19,13 @@ def base_conversions(ingredient_list):
|
||||
conversion_unit = i.unit.base_unit
|
||||
quantitiy = ureg.Quantity(f'{i.amount} {conversion_unit}')
|
||||
for u in CONVERT_TO_UNITS['metric'] + CONVERT_TO_UNITS['us'] + CONVERT_TO_UNITS['uk']:
|
||||
converted = quantitiy.to(u)
|
||||
ingredient = Ingredient(amount=converted.m, unit=Unit(name=str(converted.units)), food=ingredient_list[0].food, )
|
||||
if not any(x.unit.name == ingredient.unit.name for x in pint_converted_list):
|
||||
pint_converted_list.append(ingredient)
|
||||
try:
|
||||
converted = quantitiy.to(u)
|
||||
ingredient = Ingredient(amount=converted.m, unit=Unit(name=str(converted.units)), 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:
|
||||
pass
|
||||
except PintError:
|
||||
pass
|
||||
|
||||
@@ -34,14 +37,14 @@ def get_conversions(ingredient):
|
||||
if ingredient.unit:
|
||||
for c in ingredient.unit.unit_conversion_base_relation.all():
|
||||
r = _uc_convert(c, ingredient.amount, ingredient.unit, ingredient.food)
|
||||
if r not in conversions:
|
||||
if r and r not in conversions:
|
||||
conversions.append(r)
|
||||
for c in ingredient.unit.unit_conversion_converted_relation.all():
|
||||
r = _uc_convert(c, ingredient.amount, ingredient.unit, ingredient.food)
|
||||
if r not in conversions:
|
||||
if r and r not in conversions:
|
||||
conversions.append(r)
|
||||
|
||||
conversions += base_conversions(conversions)
|
||||
conversions = base_conversions(conversions)
|
||||
|
||||
return conversions
|
||||
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
from django.contrib import auth
|
||||
from django_scopes import scopes_disabled
|
||||
|
||||
from cookbook.helper.unit_conversion_helper import get_conversions
|
||||
from cookbook.models import Unit, Food, Ingredient
|
||||
from cookbook.models import Unit, Food, Ingredient, UnitConversion
|
||||
|
||||
|
||||
def test_unit_conversions(space_1):
|
||||
def test_unit_conversions(space_1, u1_s1):
|
||||
with scopes_disabled():
|
||||
unit_gram = Unit.objects.create(name='gram', base_unit='g', space=space_1)
|
||||
unit_pcs = Unit.objects.create(name='pcs', base_unit='', space=space_1)
|
||||
unit_floz1 = Unit.objects.create(name='fl. oz 1', base_unit='imperial_fluid_ounce', space=space_1) # US and UK use different volume systems (US vs imperial)
|
||||
unit_floz1 = Unit.objects.create(name='fl. oz 1', base_unit='imperial_fluid_ounce', space=space_1) # US and UK use different volume systems (US vs imperial)
|
||||
unit_floz2 = Unit.objects.create(name='fl. oz 2', base_unit='fluid_ounce', space=space_1)
|
||||
unit_fantasy = Unit.objects.create(name='Fantasy Unit', base_unit='', space=space_1)
|
||||
|
||||
food_1 = Food.objects.create(name='Test Food 1', space=space_1)
|
||||
food_2 = Food.objects.create(name='Test Food 2', space=space_1)
|
||||
|
||||
print('\n----------- TEST BASE CONVERSIONS - GRAM ---------------')
|
||||
ingredient_food_1_gram = Ingredient.objects.create(
|
||||
food=food_1,
|
||||
unit=unit_gram,
|
||||
@@ -20,6 +24,74 @@ def test_unit_conversions(space_1):
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
print('\n----------- TEST ---------------')
|
||||
print(get_conversions(ingredient_food_1_gram))
|
||||
|
||||
print('\n----------- TEST BASE CONVERSIONS - VOLUMES ---------------')
|
||||
|
||||
ingredient_food_1_floz1 = Ingredient.objects.create(
|
||||
food=food_1,
|
||||
unit=unit_floz1,
|
||||
amount=100,
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_floz1))
|
||||
|
||||
print('\n----------- TEST BASE CUSTOM CONVERSION - TO CUSTOM CONVERSION ---------------')
|
||||
UnitConversion.objects.create(
|
||||
base_amount=1000,
|
||||
base_unit=unit_gram,
|
||||
converted_amount=1337,
|
||||
converted_unit=unit_fantasy,
|
||||
space=space_1,
|
||||
created_by=auth.get_user(u1_s1),
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_gram))
|
||||
|
||||
print('\n----------- TEST CUSTOM CONVERSION - NO PCS ---------------')
|
||||
ingredient_food_1_pcs = Ingredient.objects.create(
|
||||
food=food_1,
|
||||
unit=unit_pcs,
|
||||
amount=5,
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
ingredient_food_2_pcs = Ingredient.objects.create(
|
||||
food=food_2,
|
||||
unit=unit_pcs,
|
||||
amount=5,
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_pcs))
|
||||
print(get_conversions(ingredient_food_2_pcs))
|
||||
|
||||
print('\n----------- TEST CUSTOM CONVERSION - PCS TO MULTIPLE BASE ---------------')
|
||||
uc1 = UnitConversion.objects.create(
|
||||
base_amount=1,
|
||||
base_unit=unit_pcs,
|
||||
converted_amount=200,
|
||||
converted_unit=unit_gram,
|
||||
food=food_1,
|
||||
space=space_1,
|
||||
created_by=auth.get_user(u1_s1),
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_pcs))
|
||||
print(get_conversions(ingredient_food_2_pcs))
|
||||
|
||||
print('\n----------- TEST CUSTOM CONVERSION - REVERSE CONVERSION ---------------')
|
||||
uc2 = UnitConversion.objects.create(
|
||||
base_amount=200,
|
||||
base_unit=unit_gram,
|
||||
converted_amount=1,
|
||||
converted_unit=unit_pcs,
|
||||
food=food_2,
|
||||
space=space_1,
|
||||
created_by=auth.get_user(u1_s1),
|
||||
)
|
||||
|
||||
print(get_conversions(ingredient_food_1_pcs))
|
||||
print(get_conversions(ingredient_food_2_pcs))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user