Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move some common data checks to the IngredientData class
Roland Geidere3aa521f4c33
diff --git a/wger/__init__.py b/wger/__init__.pyindex 9fb6d18ba..e8fc70dc5 100644--- a/wger/__init__.py+++ b/wger/__init__.py@@ -11,7 +11,7 @@ from .celery_configuration import appMIN_APP_VERSION = (1, 6, 0, 'final', 1)-VERSION = (2, 3, 0, 'alpha', 1)+VERSION = (2, 3, 0, 'alpha', 2)RELEASE = Truediff --git a/wger/nutrition/dataclasses.py b/wger/nutrition/dataclasses.pyindex b190d5c6f..b1c2b0555 100644--- a/wger/nutrition/dataclasses.py+++ b/wger/nutrition/dataclasses.py@@ -44,5 +44,29 @@ class IngredientData:license_title: strlicense_object_url: str+ def sanity_checks(self):+ if not self.name:+ raise ValueError(f'Name is empty!')+ self.name = self.name[:200]+ self.brand = self.brand[:200]+ self.common_name = self.common_name[:200]++ macros = [+ 'protein',+ 'fat',+ 'fat_saturated',+ 'carbohydrates',+ 'carbohydrates_sugar',+ 'sodium',+ 'fibres',+ ]+ for macro in macros:+ value = getattr(self, macro)+ if value and value > 100:+ raise ValueError(f'Value for {macro} is greater than 100: {value}')++ if self.carbohydrates + self.protein + self.fat > 100:+ raise ValueError(f'Total of carbohydrates, protein and fat is greater than 100!')+def dict(self):return asdict(self)diff --git a/wger/nutrition/management/commands/import-off-products.py b/wger/nutrition/management/commands/import-off-products.pyindex a841a838c..3ffb6de67 100644--- a/wger/nutrition/management/commands/import-off-products.py+++ b/wger/nutrition/management/commands/import-off-products.py@@ -49,8 +49,8 @@ class Command(ImportProductCommand):action='store_true',default=False,dest='use_jsonl',- help='Use the JSONL dump of the Open Food Facts database.'- '(this option does not require mongo)',+ help=('Use the JSONL dump of the Open Food Facts database.'+ '(this option does not require mongo)'))parser.add_argument(diff --git a/wger/nutrition/off.py b/wger/nutrition/off.pyindex 2b367ee0d..7a52507b0 100644--- a/wger/nutrition/off.py+++ b/wger/nutrition/off.py@@ -44,14 +44,7 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:# Basicsname = product_data.get('product_name')- if not name:- raise KeyError('Product name is empty')- if len(name) > 200:- name = name[:200]-common_name = product_data.get('generic_name', '')- if len(common_name) > 200:- common_name = common_name[:200]# If the energy is not available in kcal, convert from kJif 'energy-kcal_100g' in product_data['nutriments']:@@ -79,7 +72,7 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:authors = ', '.join(product_data.get('editors_tags', ['open food facts']))object_url = f'https://world.openfoodfacts.org/product/{code}/'- return IngredientData(+ ingredient_data = IngredientData(remote_id=code,name=name,language_id=language,@@ -102,3 +95,5 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:license_title=name,license_object_url=object_url,)+ ingredient_data.sanity_checks()+ return ingredient_datadiff --git a/wger/nutrition/tests/test_usda.py b/wger/nutrition/tests/test_usda.pyindex 746228b0d..4fa0fc643 100644--- a/wger/nutrition/tests/test_usda.py+++ b/wger/nutrition/tests/test_usda.py@@ -19,7 +19,7 @@ from django.test import SimpleTestCase# wgerfrom wger.nutrition.dataclasses import IngredientDatafrom wger.nutrition.usda import (- convert_to_g_per_100g,+ convert_to_grams,extract_info_from_usda,)from wger.utils.constants import CC_0_LICENSE_ID@@ -255,7 +255,7 @@ class ExtractInfoFromUSDATestCase(SimpleTestCase):"""entry = {'nutrient': {'unitName': 'g'}, 'amount': '5.0'}- self.assertEqual(convert_to_g_per_100g(entry), 5.0)+ self.assertEqual(convert_to_grams(entry), 5.0)def test_converting_milligrams(self):"""@@ -263,7 +263,7 @@ class ExtractInfoFromUSDATestCase(SimpleTestCase):"""entry = {'nutrient': {'unitName': 'mg'}, 'amount': '5000'}- self.assertEqual(convert_to_g_per_100g(entry), 5.0)+ self.assertEqual(convert_to_grams(entry), 5.0)def test_converting_unknown_unit(self):"""@@ -271,7 +271,7 @@ class ExtractInfoFromUSDATestCase(SimpleTestCase):"""entry = {'nutrient': {'unitName': 'kg'}, 'amount': '5.0'}- self.assertRaises(ValueError, convert_to_g_per_100g, entry)+ self.assertRaises(ValueError, convert_to_grams, entry)def test_converting_invalid_amount(self):"""@@ -279,4 +279,4 @@ class ExtractInfoFromUSDATestCase(SimpleTestCase):"""entry = {'nutrient': {'unitName': 'g'}, 'amount': 'invalid'}- self.assertRaises(ValueError, convert_to_g_per_100g, entry)+ self.assertRaises(ValueError, convert_to_grams, entry)diff --git a/wger/nutrition/usda.py b/wger/nutrition/usda.pyindex 1683bdacb..ac0f0cb36 100644--- a/wger/nutrition/usda.py+++ b/wger/nutrition/usda.py@@ -20,9 +20,9 @@ from wger.utils.constants import CC_0_LICENSE_IDfrom wger.utils.models import AbstractSubmissionModel-def convert_to_g_per_100g(entry: dict) -> float:+def convert_to_grams(entry: dict) -> float:"""- Convert a nutrient entry to grams per 100g+ Convert a nutrient entry to grams"""nutrient = entry['nutrient']@@ -53,7 +53,7 @@ def extract_info_from_usda(product_data: dict, language: int) -> IngredientData:fats_saturated = Nonesodium = None- fibre = None+ fiber = Nonebrand = product_data.get('brandName', '')@@ -63,39 +63,32 @@ def extract_info_from_usda(product_data: dict, language: int) -> IngredientData:nutrient_id = nutrient['nutrient']['id']match nutrient_id:+ # in kcalcase 1008:- energy = float(nutrient.get('amount')) # in kcal+ energy = float(nutrient.get('amount'))case 1003:- protein = convert_to_g_per_100g(nutrient)+ protein = convert_to_grams(nutrient)case 1005:- carbs = convert_to_g_per_100g(nutrient)+ carbs = convert_to_grams(nutrient)# Total lipid (fat) | Total fat (NLEA)case 1004 | 1085:- fats = convert_to_g_per_100g(nutrient)+ fats = convert_to_grams(nutrient)case 1093:- sodium = convert_to_g_per_100g(nutrient)+ sodium = convert_to_grams(nutrient)case 1079:- fibre = convert_to_g_per_100g(nutrient)+ fiber = convert_to_grams(nutrient)macros = [energy, protein, carbs, fats]for value in macros:if value is None:raise KeyError(f'Could not extract all basic macros: {macros=} {remote_id=}')- for value in [protein, fats, fats_saturated, carbs, carbs_sugars, sodium, fibre]:- if value and value > 100:- raise ValueError(f'Value for macronutrient is greater than 100! {macros=} {remote_id=}')-name = product_data['description'].title()- if not name:- raise ValueError(f'Name is empty! {remote_id=}')- if len(name) > 200:- name = name[:200]# License and author infosource_name = Source.USDA.value@@ -106,7 +99,7 @@ def extract_info_from_usda(product_data: dict, language: int) -> IngredientData:)object_url = f'https://fdc.nal.usda.gov/fdc-app.html#/food-details/{remote_id}/nutrients'- return IngredientData(+ ingredient_data = IngredientData(code=barcode,remote_id=remote_id,name=name,@@ -118,7 +111,7 @@ def extract_info_from_usda(product_data: dict, language: int) -> IngredientData:carbohydrates_sugar=carbs_sugars,fat=fats,fat_saturated=fats_saturated,- fibres=fibre,+ fibres=fiber,sodium=sodium,source_name=source_name,source_url=source_url,@@ -129,3 +122,5 @@ def extract_info_from_usda(product_data: dict, language: int) -> IngredientData:license_title=name,license_object_url=object_url,)+ ingredient_data.sanity_checks()+ return ingredient_data