Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fetch product from OFF if scan doesn't find ingredient locally

Roland Geiderb5dc140b0507
diff --git a/extras/scripts/create_ingredients_from_foodfacts.py b/extras/scripts/create_ingredients_from_foodfacts.py
index 94fb0edb2..00c17ddea 100644
--- a/extras/scripts/create_ingredients_from_foodfacts.py
+++ b/extras/scripts/create_ingredients_from_foodfacts.py
@@ -20,6 +20,8 @@ import os
import django
import sys
+from wger.nutrition.models.off import extract_info_from_off
+
sys.path.insert(0, os.path.join('..', '..'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
django.setup()
@@ -138,76 +140,45 @@ print('***********************************')
print(languages.keys())
print('***********************************')
-for product in db.products.find({'lang': {"$in": list(languages.keys())}, 'complete': 1}):
- lang = product['lang']
+required_top_level = [
+ 'product_name',
+ 'code',
+ 'nutriments'
+]
+required_nutriments = [
+ 'energy-kcal_100g',
+ 'proteins_100g',
+ 'carbohydrates_100g',
+ 'sugars_100g',
+ 'fat_100g',
+ 'saturated-fat_100g'
+]
- main_details = ['product_name', 'code']
- if all(req in product for req in main_details):
- name = product['product_name']
- code = product['code']
+for product in db.products.find({'lang': {"$in": list(languages.keys())}, 'complete': 1}):
- # Some products have no name or name is too long, skipping
- if not name or len(name) > 200:
- # print(f'-> skipping due to name requirements')
+ if not all(req in product for req in required_top_level):
counter['skipped'] += 1
continue
- # print(f'Processing "{name}"...')
-
- required = ['energy-kcal_100g',
- 'proteins_100g',
- 'carbohydrates_100g',
- 'sugars_100g',
- 'fat_100g',
- 'saturated-fat_100g']
- if 'nutriments' in product and all(req in product['nutriments'] for req in required):
- energy = product['nutriments']['energy-kcal_100g']
- protein = product['nutriments']['proteins_100g']
- carbs = product['nutriments']['carbohydrates_100g']
- sugars = product['nutriments']['sugars_100g']
- fat = product['nutriments']['fat_100g']
- saturated = product['nutriments']['saturated-fat_100g']
- else:
+ if not all(req in product['nutriments'] for req in required_nutriments):
# print(f'-> skipping due to required nutriments')
counter['skipped'] += 1
continue
- # these are optional
- sodium = product['nutriments'].get('sodium_100g', None)
- fibre = product['nutriments'].get('fiber_100g', None)
- common_name = product.get('generic_name', None)
- brand = product.get('brands', None)
+ ingredient_data = extract_info_from_off(product, languages[product['lang']])
- if common_name and len(common_name) > 200:
+ # Some products have no name or name is too long, skipping
+ name = ingredient_data['name']
+ if not name or len(name) > 200:
+ counter['skipped'] += 1
continue
- source_name = Source.OPEN_FOOD_FACTS.value
- source_url = f'https://world.openfoodfacts.org/api/v2/product/{code}.json'
- authors = ', '.join(product.get('editors_tags', ['open food facts']))
-
- ingredient_data = {
- 'language': languages[lang],
- 'name': name,
- 'energy': energy,
- 'protein': protein,
- 'carbohydrates': carbs,
- 'carbohydrates_sugar': sugars,
- 'fat': fat,
- 'fat_saturated': saturated,
- 'fibres': fibre,
- 'sodium': sodium,
- 'code': code,
- 'source_name': source_name,
- 'source_url': source_url,
- 'common_name': common_name,
- 'brand': brand,
- 'status': 2,
- 'license_id': 5,
- 'license_author': authors,
- 'license_title': name,
- 'license_object_url': f'https://world.openfoodfacts.org/product/{code}/'
- }
+ common_name = ingredient_data['common_name']
+ if not common_name or len(common_name) > 200:
+ counter['skipped'] += 1
+ continue
+ #
# Add entries as new products
if MODE == Mode.INSERT:
bulk_update_bucket.append(Ingredient(**ingredient_data))
@@ -239,7 +210,10 @@ for product in db.products.find({'lang': {"$in": list(languages.keys())}, 'compl
# Update an existing product (look-up key is the code) or create a new
# one. While this might not be the most efficient query (there will always
# be a SELECT first), it's ok because this script is run very rarely.
- obj, created = Ingredient.objects.update_or_create(code=code, defaults=ingredient_data)
+ obj, created = Ingredient.objects.update_or_create(
+ code=ingredient_data['code'],
+ defaults=ingredient_data
+ )
if created:
counter['new'] += 1
diff --git a/requirements.txt b/requirements.txt
index 0591876a1..132fde84b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -27,6 +27,7 @@ easy-thumbnails==2.8.5
fontawesomefree~=6.4.0
icalendar==5.0.7
invoke==2.1.2
+openfoodfacts==0.1.3
pillow==9.5.0
reportlab==4.0.4
requests==2.31.0
diff --git a/wger/core/templates/rest_framework/api.html b/wger/core/templates/rest_framework/api.html
index 2912ce266..0b74c5706 100644
--- a/wger/core/templates/rest_framework/api.html
+++ b/wger/core/templates/rest_framework/api.html
@@ -34,11 +34,11 @@
{# #}
<div>
- <div class="request-info" style="clear: both" aria-label="{% trans "request info" %}">
+ <div class="request-info" style="clear: both">
<pre class="prettyprint"><b>{{ request.method }}</b> {{ request.get_full_path }}</pre>
</div>
- <div class="response-info" aria-label="{% trans "response info" %}">
+ <div class="response-info">
<pre class="prettyprint">
<span class="meta nocode"><b>HTTP {{ response.status_code }} {{ response.status_text }}</b>
{% for key, val in response_headers|items %}
diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.py
index 5a11a16b4..3a9f333ad 100644
--- a/wger/nutrition/api/views.py
+++ b/wger/nutrition/api/views.py
@@ -17,6 +17,7 @@
# Standard Library
import datetime
+import logging
# Django
from django.conf import settings
@@ -73,12 +74,14 @@ from wger.utils.language import load_language
from wger.utils.viewsets import WgerOwnerObjectModelViewSet
+logger = logging.getLogger(__name__)
+
+
class IngredientViewSet(viewsets.ReadOnlyModelViewSet):
"""
API endpoint for ingredient objects. For a read-only endpoint with all
the information of an ingredient, see /api/v2/ingredientinfo/
"""
- queryset = Ingredient.objects.accepted()
serializer_class = IngredientSerializer
ordering_fields = '__all__'
filterset_fields = (
@@ -105,6 +108,21 @@ class IngredientViewSet(viewsets.ReadOnlyModelViewSet):
def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)
+ def get_queryset(self):
+ """H"""
+ qs = Ingredient.objects.accepted()
+
+ code = self.request.query_params.get('code')
+ if not code:
+ return qs
+
+ qs = qs.filter(code=code)
+ if qs.count() == 0:
+ logger.debug('code not found locally, fetching code from off')
+ Ingredient.fetch_ingredient_from_off(code)
+
+ return qs
+
@action(detail=True)
def get_values(self, request, pk):
"""
diff --git a/wger/nutrition/migrations/0015_alter_ingredient_creation_date_and_more.py b/wger/nutrition/migrations/0015_alter_ingredient_creation_date_and_more.py
new file mode 100644
index 000000000..bea952636
--- /dev/null
+++ b/wger/nutrition/migrations/0015_alter_ingredient_creation_date_and_more.py
@@ -0,0 +1,23 @@
+# Generated by Django 4.1.7 on 2023-06-08 11:20
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('nutrition', '0014_license_information'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='ingredient',
+ name='creation_date',
+ field=models.DateTimeField(auto_now_add=True, verbose_name='Date'),
+ ),
+ migrations.AlterField(
+ model_name='ingredient',
+ name='update_date',
+ field=models.DateTimeField(auto_now=True, verbose_name='Date'),
+ ),
+ ]
diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.py
index 711ec2f0e..bda4f0681 100644
--- a/wger/nutrition/models/ingredient.py
+++ b/wger/nutrition/models/ingredient.py
@@ -39,10 +39,19 @@ from django.utils import translation
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
+# Third Party
+import openfoodfacts as off
+
# wger
from wger.core.models import Language
+from wger.nutrition.consts import ENERGY_FACTOR
+from wger.nutrition.models.sources import Source
from wger.utils.cache import cache_mapper
-from wger.utils.constants import TWOPLACES
+from wger.utils.constants import (
+ OFF_SEARCH_PRODUCT_FOUND,
+ TWOPLACES,
+)
+from wger.utils.language import load_language
from wger.utils.managers import SubmissionManager
from wger.utils.models import (
AbstractLicenseModel,
@@ -50,9 +59,7 @@ from wger.utils.models import (
)
# Local
-from ..consts import ENERGY_FACTOR
from .ingredient_category import IngredientCategory
-from .sources import Source
logger = logging.getLogger(__name__)
@@ -85,13 +92,13 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
on_delete=models.CASCADE,
)
- creation_date = models.DateField(
+ creation_date = models.DateTimeField(
_('Date'),
auto_now_add=True,
)
"""Date when the ingredient was created"""
- update_date = models.DateField(
+ update_date = models.DateTimeField(
_('Date'),
auto_now=True,
blank=True,
@@ -445,3 +452,21 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
# wger
from wger.nutrition.tasks import fetch_ingredient_image_task
fetch_ingredient_image_task.delay(self.pk)
+
+ @classmethod
+ def fetch_ingredient_from_off(cls, code: str):
+ """
+ Searches OFF by barcode and creates a local ingredient from the result
+ """
+ # wger
+ from wger.nutrition.models.off import extract_info_from_off
+
+ result = off.products.get_product(code)
+ if result['status'] != OFF_SEARCH_PRODUCT_FOUND:
+ return None
+
+ product = result['product']
+ ingredient_data = extract_info_from_off(product, load_language(product['lang']))
+ ingredient = cls(**ingredient_data)
+ ingredient.save()
+ return ingredient
diff --git a/wger/nutrition/models/off.py b/wger/nutrition/models/off.py
new file mode 100644
index 000000000..f1082e808
--- /dev/null
+++ b/wger/nutrition/models/off.py
@@ -0,0 +1,66 @@
+# This file is part of wger Workout Manager <https://github.com/wger-project>.
+#
+# 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# wger
+from wger.core.models import Language
+from wger.nutrition.models import Source
+from wger.utils.constants import CC_ODBL_LICENSE_ID
+from wger.utils.models import AbstractSubmissionModel
+
+
+def extract_info_from_off(product, language):
+
+ # Basics
+ name = product['product_name']
+ code = product['code']
+ energy = product['nutriments']['energy-kcal_100g']
+ protein = product['nutriments']['proteins_100g']
+ carbs = product['nutriments']['carbohydrates_100g']
+ sugars = product['nutriments']['sugars_100g']
+ fat = product['nutriments']['fat_100g']
+ saturated = product['nutriments']['saturated-fat_100g']
+
+ # these are optional
+ sodium = product['nutriments'].get('sodium_100g', None)
+ fibre = product['nutriments'].get('fiber_100g', None)
+ common_name = product.get('generic_name', None)
+ brand = product.get('brands', None)
+
+ # License and author info
+ source_name = Source.OPEN_FOOD_FACTS.value
+ source_url = f'https://world.openfoodfacts.org/api/v2/product/{code}.json'
+ authors = ', '.join(product.get('editors_tags', ['open food facts']))
+
+ return {
+ 'name': name,
+ 'language': language,
+ 'energy': energy,
+ 'protein': protein,
+ 'carbohydrates': carbs,
+ 'carbohydrates_sugar': sugars,
+ 'fat': fat,
+ 'fat_saturated': saturated,
+ 'fibres': fibre,
+ 'sodium': sodium,
+ 'code': code,
+ 'source_name': source_name,
+ 'source_url': source_url,
+ 'common_name': common_name,
+ 'brand': brand,
+ 'status': AbstractSubmissionModel.STATUS_ACCEPTED,
+ 'license_id': CC_ODBL_LICENSE_ID,
+ 'license_author': authors,
+ 'license_title': name,
+ 'license_object_url': f'https://world.openfoodfacts.org/product/{code}/'
+ }
diff --git a/wger/utils/constants.py b/wger/utils/constants.py
index b2992c457..449831b03 100644
--- a/wger/utils/constants.py
+++ b/wger/utils/constants.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
@@ -50,18 +48,19 @@ PAGINATION_OBJECTS_PER_PAGE = 25
PAGINATION_MAX_TOTAL_PAGES = 10
PAGINATION_PAGES_AROUND_CURRENT = 5
-# Minimum edit distance from all existing exercises for a new exercise submission
-# to be accepted
-MIN_EDIT_DISTANCE_THRESHOLD = 2
-
# Important license IDs
CC_BY_SA_4_ID = 2
CC_BY_SA_3_LICENSE_ID = 1
+CC_ODBL_LICENSE_ID = 5
-# Default language
+# Default/fallback language
ENGLISH_SHORT_NAME = 'en'
# Possible values for ingredient image download
DOWNLOAD_INGREDIENT_WGER = 'WGER'
DOWNLOAD_INGREDIENT_OFF = 'OFF'
DOWNLOAD_INGREDIENT_OPTIONS = (DOWNLOAD_INGREDIENT_WGER, DOWNLOAD_INGREDIENT_OFF, None)
+
+# OFF Api
+OFF_SEARCH_PRODUCT_FOUND = 1
+OFF_SEARCH_PRODUCT_NOT_FOUND = 0