Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update category to product data #1646

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------
Expand Down
20 changes: 20 additions & 0 deletions wger/nutrition/off.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
Expand Down
100 changes: 100 additions & 0 deletions wger/nutrition/tests/test_off_cat.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

# 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)