Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add nutriscore field imported from Open Food Facts

Imad Rahhalic5af811e4277
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.py
index 1a6c664c3..0962fffb6 100644
--- a/wger/nutrition/api/filtersets.py
+++ b/wger/nutrition/api/filtersets.py
@@ -113,6 +113,7 @@ class IngredientFilterSet(filters.FilterSet):
'sodium': ['exact'],
'is_vegan': ['exact'],
'is_vegetarian': ['exact'],
+ 'nutriscore': ['exact', 'in'],
'created': ['exact', 'gt', 'lt'],
'last_update': ['exact', 'gt', 'lt'],
'last_imported': ['exact', 'gt', 'lt'],
diff --git a/wger/nutrition/api/serializers.py b/wger/nutrition/api/serializers.py
index e5b616c28..10e6f9a54 100644
--- a/wger/nutrition/api/serializers.py
+++ b/wger/nutrition/api/serializers.py
@@ -138,6 +138,7 @@ class IngredientSerializer(serializers.ModelSerializer):
'sodium',
'is_vegan',
'is_vegetarian',
+ 'nutriscore',
'license',
'license_title',
'license_object_url',
@@ -183,6 +184,7 @@ class IngredientInfoSerializer(serializers.ModelSerializer):
'sodium',
'is_vegan',
'is_vegetarian',
+ 'nutriscore',
'weight_units',
'language',
'license',
diff --git a/wger/nutrition/dataclasses.py b/wger/nutrition/dataclasses.py
index e2e02635e..84ccc0c51 100644
--- a/wger/nutrition/dataclasses.py
+++ b/wger/nutrition/dataclasses.py
@@ -52,6 +52,7 @@ class IngredientData:
license_author_url: str = ''
is_vegan: Optional[bool] = None
is_vegetarian: Optional[bool] = None
+ nutriscore: Optional[str] = None
def sanity_checks(self):
if not self.name:
diff --git a/wger/nutrition/extract_info/off.py b/wger/nutrition/extract_info/off.py
index 3c81d314f..547854dc6 100644
--- a/wger/nutrition/extract_info/off.py
+++ b/wger/nutrition/extract_info/off.py
@@ -86,6 +86,11 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:
elif tag == 'en:maybe-vegetarian':
is_vegetarian = None
+ # Nutri-Score
+ nutriscore_value = product_data.get('nutrition_grades', None)
+ if nutriscore_value not in ('a', 'b', 'c', 'd', 'e'):
+ nutriscore_value = None
+
# License and author info
source_name = Source.OPEN_FOOD_FACTS.value
source_url = f'https://world.openfoodfacts.org/api/v2/product/{code}.json'
@@ -115,6 +120,7 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:
license_object_url=object_url,
is_vegan=is_vegan,
is_vegetarian=is_vegetarian,
+ nutriscore=nutriscore_value,
)
ingredient_data.sanity_checks()
return ingredient_data
diff --git a/wger/nutrition/forms.py b/wger/nutrition/forms.py
index 32ec900fc..db257f4e0 100644
--- a/wger/nutrition/forms.py
+++ b/wger/nutrition/forms.py
@@ -310,6 +310,7 @@ class IngredientForm(forms.ModelForm):
'license_author',
'is_vegan',
'is_vegetarian',
+ 'nutriscore',
)
widgets = {'category': forms.TextInput}
@@ -341,6 +342,7 @@ class IngredientForm(forms.ModelForm):
Column('is_vegetarian', css_class='col-6'),
css_class='form-row',
),
+ 'nutriscore',
Row(
Column('license', css_class='col-6'),
Column('license_author', css_class='col-6'),
diff --git a/wger/nutrition/migrations/0029_ingredient_nutriscore.py b/wger/nutrition/migrations/0029_ingredient_nutriscore.py
new file mode 100644
index 000000000..58c3e72fd
--- /dev/null
+++ b/wger/nutrition/migrations/0029_ingredient_nutriscore.py
@@ -0,0 +1,29 @@
+# Generated by Django 5.2.12 on 2026-03-17 14:16
+
+# Django
+from django.db import (
+ migrations,
+ models,
+)
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ('nutrition', '0028_ingredient_dietary_properties'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='ingredient',
+ name='nutriscore',
+ field=models.CharField(
+ blank=True,
+ choices=[('a', 'A'), ('b', 'B'), ('c', 'C'), ('d', 'D'), ('e', 'E')],
+ default=None,
+ help_text='Nutri-Score grade from Open Food Facts',
+ max_length=1,
+ null=True,
+ verbose_name='Nutri-Score',
+ ),
+ ),
+ ]
diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.py
index 40f92b637..288827501 100644
--- a/wger/nutrition/models/ingredient.py
+++ b/wger/nutrition/models/ingredient.py
@@ -272,6 +272,25 @@ class Ingredient(AbstractLicenseModel, models.Model):
)
"""Dietary classification from Open Food Facts ingredients_analysis_tags"""
+ NUTRISCORE_CHOICES = [
+ ('a', 'A'),
+ ('b', 'B'),
+ ('c', 'C'),
+ ('d', 'D'),
+ ('e', 'E'),
+ ]
+
+ nutriscore = models.CharField(
+ max_length=1,
+ choices=NUTRISCORE_CHOICES,
+ verbose_name='Nutri-Score',
+ help_text='Nutri-Score grade from Open Food Facts',
+ null=True,
+ blank=True,
+ default=None,
+ )
+ """Nutri-Score from Open Food Facts"""
+
# Metaclass to set some other properties
class Meta:
ordering = [
diff --git a/wger/nutrition/tests/test_off.py b/wger/nutrition/tests/test_off.py
index 61d117e71..12817edb7 100644
--- a/wger/nutrition/tests/test_off.py
+++ b/wger/nutrition/tests/test_off.py
@@ -42,6 +42,7 @@ class ExtractInfoFromOffTestCase(SimpleTestCase):
'en:vegan',
'en:vegetarian',
],
+ 'nutrition_grades': 'c',
'nutriments': {
'energy-kcal_100g': 120,
'proteins_100g': 10,
@@ -83,6 +84,7 @@ class ExtractInfoFromOffTestCase(SimpleTestCase):
license_object_url='https://world.openfoodfacts.org/product/1234/',
is_vegan=True,
is_vegetarian=True,
+ nutriscore='c',
)
self.assertEqual(result, data)
@@ -175,6 +177,29 @@ class ExtractInfoFromOffTestCase(SimpleTestCase):
self.assertIsNone(result.is_vegan)
self.assertIsNone(result.is_vegetarian)
+ def test_nutriscore_extracted(self):
+ """
+ Test that nutriscore is correctly extracted
+ """
+ result = extract_info_from_off(self.off_data1, 1)
+ self.assertEqual(result.nutriscore, 'c')
+
+ def test_nutriscore_missing(self):
+ """
+ Test that missing nutrition_grades returns None
+ """
+ del self.off_data1['nutrition_grades']
+ result = extract_info_from_off(self.off_data1, 1)
+ self.assertIsNone(result.nutriscore)
+
+ def test_nutriscore_invalid(self):
+ """
+ Test that invalid nutrition_grades value returns None
+ """
+ self.off_data1['nutrition_grades'] = 'z'
+ result = extract_info_from_off(self.off_data1, 1)
+ self.assertIsNone(result.nutriscore)
+
def test_ingredient_clean_name(self):
data = IngredientData(
name='Stonebaked Pizza "the amer\x96ican \x99pepperoni"',