mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2025-12-24 02:39:20 -05:00
fixed unit conversion division by 0
This commit is contained in:
@@ -135,8 +135,9 @@ class UnitConversionHelper:
|
||||
:param food: base food
|
||||
:return: converted ingredient object from base amount/unit/food
|
||||
"""
|
||||
if uc.food is None or uc.food == food:
|
||||
if (uc.food is None or uc.food == food) and uc.converted_amount > 0 and uc.base_amount > 0:
|
||||
if unit == uc.base_unit:
|
||||
return Ingredient(amount=amount * (uc.converted_amount / uc.base_amount), unit=uc.converted_unit, food=food, space=self.space)
|
||||
else:
|
||||
return Ingredient(amount=amount * (uc.base_amount / uc.converted_amount), unit=uc.base_unit, food=food, space=self.space)
|
||||
return None
|
||||
|
||||
@@ -185,3 +185,32 @@ def test_unit_conversions(space_1, space_2, u1_s1):
|
||||
assert next(x for x in conversions if x.unit == unit_kg_space_2) is not None
|
||||
assert abs(next(x for x in conversions if x.unit == unit_kg_space_2).amount - Decimal(0.1)) < 0.0001
|
||||
print(conversions)
|
||||
|
||||
def test_conversion_with_zero(space_1, space_2, u1_s1):
|
||||
with scopes_disabled():
|
||||
uch = UnitConversionHelper(space_1)
|
||||
|
||||
unit_gram = Unit.objects.create(name='gram', base_unit='g', space=space_1)
|
||||
unit_fantasy = Unit.objects.create(name='Fantasy Unit', base_unit=None, space=space_1)
|
||||
|
||||
food_1 = Food.objects.create(name='Test Food 1', space=space_1)
|
||||
|
||||
ingredient_food_1_gram = Ingredient.objects.create(
|
||||
food=food_1,
|
||||
unit=unit_gram,
|
||||
amount=100,
|
||||
space=space_1,
|
||||
)
|
||||
|
||||
print('\n----------- TEST BASE CUSTOM CONVERSION - TO CUSTOM CONVERSION ---------------')
|
||||
UnitConversion.objects.create(
|
||||
base_amount=0,
|
||||
base_unit=unit_gram,
|
||||
converted_amount=0,
|
||||
converted_unit=unit_fantasy,
|
||||
space=space_1,
|
||||
created_by=auth.get_user(u1_s1),
|
||||
)
|
||||
conversions = uch.get_conversions(ingredient_food_1_gram)
|
||||
|
||||
assert len(conversions) == 1 # conversion always includes the ingredient, if count is 1 no other conversion was found
|
||||
|
||||
@@ -19,11 +19,11 @@
|
||||
</v-row>
|
||||
<v-row>
|
||||
<v-col md="6">
|
||||
<v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.baseAmount" control-variant="stacked" :precision="3"></v-number-input>
|
||||
<v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.baseAmount" control-variant="stacked" :precision="3" :min="0.001"></v-number-input>
|
||||
</v-col>
|
||||
<v-col md="6">
|
||||
<!-- TODO fix card overflow invisible, overflow-visible class is not working -->
|
||||
<model-select :label="$t('Unit')" v-model="editingObj.baseUnit" model="Unit"></model-select>
|
||||
<model-select v-model="editingObj.baseUnit" model="Unit"></model-select>
|
||||
</v-col>
|
||||
</v-row>
|
||||
<v-row class="mt-0">
|
||||
@@ -33,11 +33,11 @@
|
||||
</v-row>
|
||||
<v-row>
|
||||
<v-col md="6">
|
||||
<v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.convertedAmount" control-variant="stacked" :precision="3"></v-number-input>
|
||||
<v-number-input :label="$t('Amount')" :step="10" v-model="editingObj.convertedAmount" control-variant="stacked" :precision="3" :min="0.001"></v-number-input>
|
||||
</v-col>
|
||||
<v-col md="6">
|
||||
<!-- TODO fix card overflow invisible, overflow-visible class is not working -->
|
||||
<model-select :label="$t('Unit')" v-model="editingObj.convertedUnit" model="Unit"></model-select>
|
||||
<model-select v-model="editingObj.convertedUnit" model="Unit"></model-select>
|
||||
</v-col>
|
||||
</v-row>
|
||||
<v-row>
|
||||
|
||||
@@ -115,6 +115,7 @@
|
||||
"Fats": "",
|
||||
"File": "",
|
||||
"Files": "",
|
||||
"Finish": "",
|
||||
"Food": "",
|
||||
"FoodInherit": "",
|
||||
"FoodNotOnHand": "",
|
||||
|
||||
@@ -112,6 +112,7 @@
|
||||
"Fats": "Мазнини",
|
||||
"File": "Файл",
|
||||
"Files": "Файлове",
|
||||
"Finish": "",
|
||||
"Food": "Храна",
|
||||
"FoodInherit": "Хранителни наследствени полета",
|
||||
"FoodNotOnHand": "Нямате {храна} под ръка.",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "Greixos",
|
||||
"File": "Arxiu",
|
||||
"Files": "Arxius",
|
||||
"Finish": "",
|
||||
"First_name": "Nom",
|
||||
"Food": "Aliment",
|
||||
"FoodInherit": "Camps Heretats",
|
||||
|
||||
@@ -153,6 +153,7 @@
|
||||
"Fats": "Tuky",
|
||||
"File": "Soubor",
|
||||
"Files": "Soubory",
|
||||
"Finish": "",
|
||||
"First_name": "Jméno",
|
||||
"Food": "Potravina",
|
||||
"FoodInherit": "Propisovatelná pole potraviny",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "Fedtstoffer",
|
||||
"File": "Fil",
|
||||
"Files": "Filer",
|
||||
"Finish": "",
|
||||
"First_name": "Fornavn",
|
||||
"Food": "Mad",
|
||||
"FoodInherit": "Nedarvelige mad felter",
|
||||
|
||||
@@ -212,6 +212,7 @@
|
||||
"Fats": "Fette",
|
||||
"File": "Datei",
|
||||
"Files": "Dateien",
|
||||
"Finish": "Fertigstellen",
|
||||
"FinishedAt": "Fertig um",
|
||||
"First": "Erstes",
|
||||
"First_name": "Vorname",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "Λιπαρά",
|
||||
"File": "Αρχείο",
|
||||
"Files": "Αρχεία",
|
||||
"Finish": "",
|
||||
"First_name": "Όνομα",
|
||||
"Food": "Φαγητό",
|
||||
"FoodInherit": "Πεδία φαγητών που κληρονομούνται",
|
||||
|
||||
@@ -210,6 +210,7 @@
|
||||
"Fats": "Fats",
|
||||
"File": "File",
|
||||
"Files": "Files",
|
||||
"Finish": "Finish",
|
||||
"FinishedAt": "Finished at",
|
||||
"First": "First",
|
||||
"First_name": "First Name",
|
||||
|
||||
@@ -207,6 +207,7 @@
|
||||
"Fats": "Grasas",
|
||||
"File": "Archivo",
|
||||
"Files": "Archivos",
|
||||
"Finish": "",
|
||||
"FinishedAt": "Finaliza a las",
|
||||
"First": "Primero",
|
||||
"First_name": "Nombre",
|
||||
|
||||
@@ -151,6 +151,7 @@
|
||||
"Fats": "Rasvat",
|
||||
"File": "Tiedosto",
|
||||
"Files": "Tiedostot",
|
||||
"Finish": "",
|
||||
"First_name": "Etunimi",
|
||||
"Food": "Ruoka",
|
||||
"FoodInherit": "Ruoan perinnölliset kentät",
|
||||
|
||||
@@ -210,6 +210,7 @@
|
||||
"Fats": "Matières grasses",
|
||||
"File": "Fichier",
|
||||
"Files": "Fichiers",
|
||||
"Finish": "",
|
||||
"FinishedAt": "Terminé à",
|
||||
"First": "Premier",
|
||||
"First_name": "Prénom",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "שומנים",
|
||||
"File": "קובץ",
|
||||
"Files": "קבצים",
|
||||
"Finish": "",
|
||||
"First_name": "שם פרטי",
|
||||
"Food": "אוכל",
|
||||
"FoodInherit": "ערכי מזון",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "Masti",
|
||||
"File": "Datoteka",
|
||||
"Files": "Datoteke",
|
||||
"Finish": "",
|
||||
"First_name": "Ime",
|
||||
"Food": "Namirnica",
|
||||
"FoodInherit": "Nasljedna polja namirnice",
|
||||
|
||||
@@ -137,6 +137,7 @@
|
||||
"Fats": "Zsírok",
|
||||
"File": "Fájl",
|
||||
"Files": "Fájlok",
|
||||
"Finish": "",
|
||||
"First_name": "Keresztnév",
|
||||
"Food": "Alapanyag",
|
||||
"FoodInherit": "",
|
||||
|
||||
@@ -69,6 +69,7 @@
|
||||
"Fats": "",
|
||||
"File": "",
|
||||
"Files": "",
|
||||
"Finish": "",
|
||||
"Food": "Սննդամթերք",
|
||||
"FromBalance": "",
|
||||
"Fulltext": "",
|
||||
|
||||
@@ -126,6 +126,7 @@
|
||||
"Fats": "Lemak",
|
||||
"File": "Berkas",
|
||||
"Files": "File",
|
||||
"Finish": "",
|
||||
"First_name": "",
|
||||
"Food": "",
|
||||
"FoodInherit": "",
|
||||
|
||||
@@ -153,6 +153,7 @@
|
||||
"Fats": "",
|
||||
"File": "",
|
||||
"Files": "",
|
||||
"Finish": "",
|
||||
"First_name": "",
|
||||
"Food": "",
|
||||
"FoodInherit": "",
|
||||
|
||||
@@ -211,6 +211,7 @@
|
||||
"Fats": "Grassi",
|
||||
"File": "File",
|
||||
"Files": "File",
|
||||
"Finish": "",
|
||||
"FinishedAt": "Finito alle",
|
||||
"First": "Primo",
|
||||
"First_name": "Nome",
|
||||
|
||||
@@ -139,6 +139,7 @@
|
||||
"Fats": "",
|
||||
"File": "",
|
||||
"Files": "",
|
||||
"Finish": "",
|
||||
"First_name": "",
|
||||
"Food": "",
|
||||
"FoodInherit": "",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "",
|
||||
"File": "",
|
||||
"Files": "",
|
||||
"Finish": "",
|
||||
"First_name": "",
|
||||
"Food": "",
|
||||
"FoodInherit": "",
|
||||
|
||||
@@ -146,6 +146,7 @@
|
||||
"Fats": "Fett",
|
||||
"File": "Fil",
|
||||
"Files": "Filer",
|
||||
"Finish": "",
|
||||
"First_name": "Fornavn",
|
||||
"Food": "Matretter",
|
||||
"FoodInherit": "Arvbare felt for matvarer",
|
||||
|
||||
@@ -211,6 +211,7 @@
|
||||
"Fats": "Vetten",
|
||||
"File": "Bestand",
|
||||
"Files": "Bestanden",
|
||||
"Finish": "",
|
||||
"FinishedAt": "Afgerond op",
|
||||
"First": "Eerste",
|
||||
"First_name": "Voornaam",
|
||||
|
||||
@@ -180,6 +180,7 @@
|
||||
"Fats": "Tłuszcze",
|
||||
"File": "Plik",
|
||||
"Files": "Pliki",
|
||||
"Finish": "",
|
||||
"First_name": "Imię",
|
||||
"Food": "Żywność",
|
||||
"FoodInherit": "Pola dziedziczone w żywności",
|
||||
|
||||
@@ -103,6 +103,7 @@
|
||||
"Fats": "Gorduras",
|
||||
"File": "Ficheiro",
|
||||
"Files": "Ficheiros",
|
||||
"Finish": "",
|
||||
"Food": "Comida",
|
||||
"FoodInherit": "Campos herdados por comida",
|
||||
"FoodNotOnHand": "Não têm {food} disponível.",
|
||||
|
||||
@@ -209,6 +209,7 @@
|
||||
"Fats": "Gorduras",
|
||||
"File": "Arquivo",
|
||||
"Files": "Arquivos",
|
||||
"Finish": "",
|
||||
"FinishedAt": "Finalizado em",
|
||||
"First": "Primeiro",
|
||||
"First_name": "Primeiro Nome",
|
||||
|
||||
@@ -133,6 +133,7 @@
|
||||
"Fats": "Grăsimi",
|
||||
"File": "Fișier",
|
||||
"Files": "Fișiere",
|
||||
"Finish": "",
|
||||
"First_name": "Prenume",
|
||||
"Food": "Mâncare",
|
||||
"FoodInherit": "Câmpuri moștenite de alimente",
|
||||
|
||||
@@ -210,6 +210,7 @@
|
||||
"Fats": "Жиры",
|
||||
"File": "Файл",
|
||||
"Files": "Файлы",
|
||||
"Finish": "",
|
||||
"FinishedAt": "Завершено в",
|
||||
"First": "Первый",
|
||||
"First_name": "Имя",
|
||||
|
||||
@@ -210,6 +210,7 @@
|
||||
"Fats": "Maščobe",
|
||||
"File": "Datoteka",
|
||||
"Files": "Datoteke",
|
||||
"Finish": "",
|
||||
"FinishedAt": "Končano ob",
|
||||
"First": "Prvi",
|
||||
"First_name": "Ime",
|
||||
|
||||
@@ -191,6 +191,7 @@
|
||||
"Fats": "Fett",
|
||||
"File": "Fil",
|
||||
"Files": "Filer",
|
||||
"Finish": "",
|
||||
"First_name": "Förnamn",
|
||||
"Food": "Livsmedel",
|
||||
"FoodInherit": "Ärftliga livsmedels fält",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "Yağlar",
|
||||
"File": "Dosya",
|
||||
"Files": "Dosyalar",
|
||||
"Finish": "",
|
||||
"First_name": "İsim",
|
||||
"Food": "Yiyecek",
|
||||
"FoodInherit": "Yiyeceğin Devralınabileceği Alanlar",
|
||||
|
||||
@@ -137,6 +137,7 @@
|
||||
"Fats": "Жири",
|
||||
"File": "Файл",
|
||||
"Files": "Файли",
|
||||
"Finish": "",
|
||||
"Food": "Їжа",
|
||||
"FoodInherit": "Пола Успадкованої Їжі",
|
||||
"FoodNotOnHand": "У вас немає {food} на руках.",
|
||||
|
||||
@@ -154,6 +154,7 @@
|
||||
"Fats": "脂肪",
|
||||
"File": "文件",
|
||||
"Files": "文件",
|
||||
"Finish": "",
|
||||
"First_name": "名",
|
||||
"Food": "食物",
|
||||
"FoodInherit": "食物可继承的字段",
|
||||
|
||||
@@ -209,6 +209,7 @@
|
||||
"Fats": "脂肪",
|
||||
"File": "檔案",
|
||||
"Files": "檔案",
|
||||
"Finish": "",
|
||||
"FinishedAt": "完成於",
|
||||
"First": "第一個",
|
||||
"First_name": "名字",
|
||||
|
||||
Reference in New Issue
Block a user