From 8b0f94f001e81a38409c5f92aadd789b244f65ca Mon Sep 17 00:00:00 2001 From: andre9836 Date: Fri, 26 Apr 2024 15:13:37 -0400 Subject: [PATCH 1/2] Attempt To Add Categories to Product WIP to create a category to added products from the Nutrition Plan --- wger/nutrition/off.py | 20 ++++++ wger/nutrition/tests/test_off_cat.py | 100 +++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 wger/nutrition/tests/test_off_cat.py diff --git a/wger/nutrition/off.py b/wger/nutrition/off.py index 9e99f141c..5af7cd9d5 100644 --- a/wger/nutrition/off.py +++ b/wger/nutrition/off.py @@ -67,6 +67,7 @@ def dict(self): def extract_info_from_off(product_data, language: int): + print(product_data) if not all(req in product_data for req in OFF_REQUIRED_TOP_LEVEL): raise KeyError('Missing required top-level key') @@ -99,6 +100,25 @@ def extract_info_from_off(product_data, language: int): fat = product_data['nutriments']['fat_100g'] saturated = product_data['nutriments'].get('saturated-fat_100g', 0) + # List of target categories to check against + target_categories = ["Meals", "Snacks", "Spreads", "Beverages", "Breads", "Vegetables", "Fruits", "Grains", "Meats", "Protein Foods", "Dairy"] + + # Retrieve the category string from product_data using product_data.get() + category_string = product_data.get('category') + + # Split the category string into a list of individual categories + if category_string: + categories_list = [category.strip() for category in category_string.split(",")] + else: + categories_list = [] + + # Check if any API categories match the target categories + category = None + for api_category in categories_list: + if api_category in target_categories: + category = api_category + break + # these are optional sodium = product_data['nutriments'].get('sodium_100g', None) fibre = product_data['nutriments'].get('fiber_100g', None) diff --git a/wger/nutrition/tests/test_off_cat.py b/wger/nutrition/tests/test_off_cat.py new file mode 100644 index 000000000..f5da26cc4 --- /dev/null +++ b/wger/nutrition/tests/test_off_cat.py @@ -0,0 +1,100 @@ +# This file is part of wger Workout Manager. +# +# wger Workout Manager is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# wger Workout Manager is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Workout Manager. If not, see . + +# Django +from django.test import SimpleTestCase + +# wger +from wger.nutrition.off import ( + IngredientData, + extract_info_from_off, +) +from wger.utils.constants import ODBL_LICENSE_ID +from wger.utils.models import AbstractSubmissionModel + + +class ExtractInfoFromOffTestCase(SimpleTestCase): + """ + Test the extract_info_from_off function + """ + + off_data1 = {} + + def setUp(self): + self.off_data1 = { + 'code': '1234', + 'lang': 'de', + 'product_name': 'Foo with chocolate', + 'generic_name': 'Foo with chocolate, 250g package', + 'brands': 'The bar company', + 'editors_tags': ['open food facts', 'MrX'], + 'nutriments': { + 'energy-kcal_100g': 120, + 'proteins_100g': 10, + 'carbohydrates_100g': 20, + 'sugars_100g': 30, + 'fat_100g': 40, + 'saturated-fat_100g': 11, + 'sodium_100g': 5, + 'fiber_100g': None, + 'other_stuff': 'is ignored', + }, + } + + def test_regular_response(self): + """ + Test that the function can read the regular case + """ + result = extract_info_from_off(self.off_data1, 1) + data = IngredientData( + name='Foo with chocolate', + language_id=1, + energy=120, + protein=10, + carbohydrates=20, + carbohydrates_sugar=30, + fat=40, + fat_saturated=11, + fibres=None, + sodium=5, + code='1234', + source_name='Open Food Facts', + source_url='https://world.openfoodfacts.org/api/v2/product/1234.json', + common_name='Foo with chocolate, 250g package', + # category='Snack, Cookies, Chocolate', + brand='The bar company', + status=AbstractSubmissionModel.STATUS_ACCEPTED, + license_id=ODBL_LICENSE_ID, + license_author='open food facts, MrX', + license_title='Foo with chocolate', + license_object_url='https://world.openfoodfacts.org/product/1234/', + ) + + # data['category'] = 'Snacks, Chocolate, Cookies' + + self.assertEqual(result, data) + + def test_convert_kj(self): + """ + Is the category available? + """ + del self.off_data1['nutriments']['energy-kcal_100g'] + self.off_data1['nutriments']['energy-kj_100g'] = 120 + + result = extract_info_from_off(self.off_data1, 1) + + # 120 / KJ_PER_KCAL + self.assertAlmostEqual(result.energy, 28.6806, 3) + From bad8857a0bce8e0f7117a2d6321706d4745fc07f Mon Sep 17 00:00:00 2001 From: andre9836 Date: Fri, 26 Apr 2024 15:19:59 -0400 Subject: [PATCH 2/2] Added me :) --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index fc437ed7f..e040c4271 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -76,6 +76,7 @@ Developers * Alexandra Rhodes - https://github.com/arhodes130 * Jayanth Bontha - https://github.com/JayanthBontha * Ethan Winters - https://github.com/ebwinters +* Andre Dyke - https://github.com/andre9836 Translators -----------