Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fix ingredient sync
Roland Geiderac570fda97a6
diff --git a/wger/core/management/commands/dummy-generator.py b/wger/core/management/commands/dummy-generator.pyindex a1776baca..b318e4bb3 100644--- a/wger/core/management/commands/dummy-generator.py+++ b/wger/core/management/commands/dummy-generator.py@@ -40,5 +40,5 @@ class Command(BaseCommand):call_command('dummy-generator-nutrition')call_command('dummy-generator-measurement-categories')call_command('dummy-generator-measurements')- call_command('dummy-generator-workout-plans')+ call_command('dummy-generator-routines')call_command('dummy-generator-workout-diary')diff --git a/wger/nutrition/dataclasses.py b/wger/nutrition/dataclasses.pyindex 741dbbf1f..483eaa21e 100644--- a/wger/nutrition/dataclasses.py+++ b/wger/nutrition/dataclasses.py@@ -49,8 +49,8 @@ class IngredientData:code: Optional[str]source_name: strsource_url: str- common_name: str- brand: str+ common_name: str | None+ brand: str | Nonelicense_id: intlicense_author: strlicense_title: str@@ -68,8 +68,8 @@ class IngredientData:if not self.name:raise ValueError('Name is empty!')self.name = self.name[:200]- self.brand = self.brand[:200]- self.common_name = self.common_name[:200]+ self.brand = self.brand[:200] if self.brand is not None else None+ self.common_name = self.common_name[:200] if self.common_name is not None else Noneself.license_title = self.license_title[:200]# Mass checks (not more than 100g of something per 100g of product etc)diff --git a/wger/nutrition/extract_info/wger.py b/wger/nutrition/extract_info/wger.pyindex af914bbbe..65b7f4907 100644--- a/wger/nutrition/extract_info/wger.py+++ b/wger/nutrition/extract_info/wger.py@@ -46,13 +46,13 @@ def extract_weight_unit_info_from_wger_api(product_data: dict) -> list[WeightUnidef extract_info_from_wger_api(product_data: dict) -> IngredientData:# Basicsname = product_data.get('name', '')- common_name = product_data.get('common_name', '')+ common_name = product_data.get('common_name')code = product_data['code']energy = float(product_data['energy'])protein = float(product_data['protein'])carbs = float(product_data['carbohydrates'])fat = float(product_data['fat'])- language = product_data['language']+ language = product_data['language']['id']# Optionalsodium = product_data.get('sodium', None)@@ -67,7 +67,7 @@ def extract_info_from_wger_api(product_data: dict) -> IngredientData:fiber = product_data.get('fiber', None)fiber = float(fiber) if fiber is not None else None- brand = product_data.get('brand', '')+ brand = product_data.get('brand')# Dietary propertiesis_vegan = product_data.get('is_vegan', None)@@ -78,7 +78,7 @@ def extract_info_from_wger_api(product_data: dict) -> IngredientData:source_name = product_data.get('source_name', '')source_url = product_data.get('source_url', '')- license_id = product_data.get('license', ODBL_LICENSE_ID)+ license_id = product_data.get('license', {}).get('id', ODBL_LICENSE_ID)license_title = product_data.get('license_title', '')license_object_url = product_data.get('license_object_url', '')license_authors = product_data.get('license_author', '')diff --git a/wger/nutrition/tests/test_sync.py b/wger/nutrition/tests/test_sync.pyindex 2ed5230ac..6b8908aec 100644--- a/wger/nutrition/tests/test_sync.py+++ b/wger/nutrition/tests/test_sync.py@@ -67,13 +67,19 @@ class MockIngredientResponse:'fat_saturated': '4.500','fiber': '2.000','sodium': '0.356',- 'license': 5,+ 'license': {+ 'id': 5,+ 'name': 'Test license'+ },'license_title': ' Gâteau double chocolat ','license_object_url': '','license_author': 'Open Food Facts','license_author_url': '','license_derivative_source_url': '',- 'language': 2,+ 'language': {+ 'id': 2,+ 'short_name': 'de'+ },'is_vegan': True,'is_vegetarian': True,'nutriscore': 'd',@@ -107,13 +113,18 @@ class MockIngredientResponse:'fat_saturated': '4.600','fiber': None,'sodium': '0.820',- 'license': 5,+ 'license': {+ 'id': 5,+ 'name': 'Test license'+ },'license_title': ' Maxi Hot Dog New York Style','license_object_url': '','license_author': 'Open Food Facts','license_author_url': '','license_derivative_source_url': '',- 'language': 3,+ 'language': {+ 'id': 3+ },'is_vegan': None,'is_vegetarian': None,'nutriscore': None,@@ -215,8 +226,12 @@ class TestSyncMethods(WgerTestCase):'protein': '150.000','carbohydrates': '10.000','fat': '10.000',- 'language': 2,- 'license': 5,+ 'language': {+ 'id': 2+ },+ 'license': {+ 'id': 5+ },'license_author': 'Open Food Facts','weight_units': [],},@@ -231,8 +246,12 @@ class TestSyncMethods(WgerTestCase):'protein': '11.000','carbohydrates': '27.000','fat': '11.000',- 'language': 3,- 'license': 5,+ 'language': {+ 'id': 3+ },+ 'license':{+ 'id': 5+ },'license_author': 'Open Food Facts','weight_units': [],},@@ -243,7 +262,7 @@ class TestSyncMethods(WgerTestCase):count_before = Ingredient.objects.count()# A single malformed record must not abort the whole run- sync_ingredients(lambda x: x)+ sync_ingredients(print)# The valid ingredient was synced, the malformed one was skippedself.assertTrue(