Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add is_vegan and is_vegetarian fields to Ingredient model
stuckvgn92188dfa7065
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.pyindex b459df98b..1a6c664c3 100644--- a/wger/nutrition/api/filtersets.py+++ b/wger/nutrition/api/filtersets.py@@ -111,6 +111,8 @@ class IngredientFilterSet(filters.FilterSet):'fat_saturated': ['exact'],'fiber': ['exact'],'sodium': ['exact'],+ 'is_vegan': ['exact'],+ 'is_vegetarian': ['exact'],'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.pyindex 2cd7aff4f..e5b616c28 100644--- a/wger/nutrition/api/serializers.py+++ b/wger/nutrition/api/serializers.py@@ -136,6 +136,8 @@ class IngredientSerializer(serializers.ModelSerializer):'fat_saturated','fiber','sodium',+ 'is_vegan',+ 'is_vegetarian','license','license_title','license_object_url',@@ -179,6 +181,8 @@ class IngredientInfoSerializer(serializers.ModelSerializer):'fat_saturated','fiber','sodium',+ 'is_vegan',+ 'is_vegetarian','weight_units','language','license',diff --git a/wger/nutrition/dataclasses.py b/wger/nutrition/dataclasses.pyindex 6991f6fc3..e2e02635e 100644--- a/wger/nutrition/dataclasses.py+++ b/wger/nutrition/dataclasses.py@@ -50,6 +50,8 @@ class IngredientData:license_object_url: strlicense_derivative_source_url: str = ''license_author_url: str = ''+ is_vegan: Optional[bool] = None+ is_vegetarian: Optional[bool] = Nonedef sanity_checks(self):if not self.name:diff --git a/wger/nutrition/extract_info/off.py b/wger/nutrition/extract_info/off.pyindex bb1f816c9..3c81d314f 100644--- a/wger/nutrition/extract_info/off.py+++ b/wger/nutrition/extract_info/off.py@@ -63,6 +63,29 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:fiber = product_data['nutriments'].get('fiber_100g', None)brand = product_data.get('brands', '')+ # Dietary properties from OFF ingredients analysis+ is_vegan = None+ is_vegetarian = None+ analysis_tags = product_data.get('ingredients_analysis_tags', [])+ for tag in analysis_tags:+ if tag == 'en:vegan':+ is_vegan = True+ elif tag == 'en:non-vegan':+ is_vegan = False+ elif tag == 'en:vegan-status-unknown':+ is_vegan = None+ elif tag == 'en:maybe-vegan':+ is_vegan = None++ if tag == 'en:vegetarian':+ is_vegetarian = True+ elif tag == 'en:non-vegetarian':+ is_vegetarian = False+ elif tag == 'en:vegetarian-status-unknown':+ is_vegetarian = None+ elif tag == 'en:maybe-vegetarian':+ is_vegetarian = None+# License and author infosource_name = Source.OPEN_FOOD_FACTS.valuesource_url = f'https://world.openfoodfacts.org/api/v2/product/{code}.json'@@ -90,6 +113,8 @@ def extract_info_from_off(product_data: dict, language: int) -> IngredientData:license_author=authors,license_title=name,license_object_url=object_url,+ is_vegan=is_vegan,+ is_vegetarian=is_vegetarian,)ingredient_data.sanity_checks()return ingredient_datadiff --git a/wger/nutrition/extract_info/wger.py b/wger/nutrition/extract_info/wger.pyindex 85016476e..f64e190f0 100644--- a/wger/nutrition/extract_info/wger.py+++ b/wger/nutrition/extract_info/wger.py@@ -44,6 +44,10 @@ def extract_info_from_wger_api(product_data: dict) -> IngredientData:brand = product_data.get('brand', '')+ # Dietary properties+ is_vegan = product_data.get('is_vegan', None)+ is_vegetarian = product_data.get('is_vegetarian', None)+# License and author infosource_name = product_data.get('source_name', '')source_url = product_data.get('source_url', '')@@ -78,5 +82,7 @@ def extract_info_from_wger_api(product_data: dict) -> IngredientData:license_author=license_authors,license_author_url=license_author_url,license_derivative_source_url=license_derivative_source_url,+ is_vegan=is_vegan,+ is_vegetarian=is_vegetarian,)return ingredient_datadiff --git a/wger/nutrition/migrations/0028_ingredient_dietary_properties.py b/wger/nutrition/migrations/0028_ingredient_dietary_properties.pynew file mode 100644index 000000000..0a8f3bb46--- /dev/null+++ b/wger/nutrition/migrations/0028_ingredient_dietary_properties.py@@ -0,0 +1,34 @@+# Generated by Django 5.2++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ('nutrition', '0027_prefill_end_date'),+ ]++ operations = [+ migrations.AddField(+ model_name='ingredient',+ name='is_vegan',+ field=models.BooleanField(+ blank=True,+ default=None,+ help_text='Whether the ingredient is suitable for a vegan diet',+ null=True,+ verbose_name='Vegan',+ ),+ ),+ migrations.AddField(+ model_name='ingredient',+ name='is_vegetarian',+ field=models.BooleanField(+ blank=True,+ default=None,+ help_text='Whether the ingredient is suitable for a vegetarian diet',+ null=True,+ verbose_name='Vegetarian',+ ),+ ),+ ]diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex 99099fb5c..40f92b637 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -254,6 +254,24 @@ class Ingredient(AbstractLicenseModel, models.Model):blank=True,)+ is_vegan = models.BooleanField(+ verbose_name='Vegan',+ help_text='Whether the ingredient is suitable for a vegan diet',+ null=True,+ blank=True,+ default=None,+ )+ """Dietary classification from Open Food Facts ingredients_analysis_tags"""++ is_vegetarian = models.BooleanField(+ verbose_name='Vegetarian',+ help_text='Whether the ingredient is suitable for a vegetarian diet',+ null=True,+ blank=True,+ default=None,+ )+ """Dietary classification from Open Food Facts ingredients_analysis_tags"""+# Metaclass to set some other propertiesclass Meta:ordering = [diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.pyindex bce6535a9..e885e6e34 100644--- a/wger/nutrition/tests/test_ingredient.py+++ b/wger/nutrition/tests/test_ingredient.py@@ -487,6 +487,11 @@ class IngredientModelTestCase(WgerTestCase):'generic_name': 'Foo with chocolate, 250g package','brands': 'The bar company','editors_tags': ['open food facts', 'MrX'],+ 'ingredients_analysis_tags': [+ 'en:palm-oil-free',+ 'en:vegan',+ 'en:vegetarian',+ ],'nutriments': {'energy-kcal_100g': 120,'proteins_100g': 10,@@ -521,6 +526,8 @@ class IngredientModelTestCase(WgerTestCase):self.assertEqual(ingredient.fiber, None)self.assertEqual(ingredient.brand, 'The bar company')self.assertEqual(ingredient.license_author, 'open food facts, MrX')+ self.assertTrue(ingredient.is_vegan)+ self.assertTrue(ingredient.is_vegetarian)@patch('openfoodfacts.api.ProductResource.get')def test_fetch_from_off_success_long_name(self, mock_api):diff --git a/wger/nutrition/tests/test_off.py b/wger/nutrition/tests/test_off.pyindex 093106b70..61d117e71 100644--- a/wger/nutrition/tests/test_off.py+++ b/wger/nutrition/tests/test_off.py@@ -37,6 +37,11 @@ class ExtractInfoFromOffTestCase(SimpleTestCase):'generic_name': 'Foo with chocolate, 250g package','brands': 'The bar company','editors_tags': ['open food facts', 'MrX'],+ 'ingredients_analysis_tags': [+ 'en:palm-oil-free',+ 'en:vegan',+ 'en:vegetarian',+ ],'nutriments': {'energy-kcal_100g': 120,'proteins_100g': 10,@@ -76,6 +81,8 @@ class ExtractInfoFromOffTestCase(SimpleTestCase):license_author='open food facts, MrX',license_title='Foo with chocolate',license_object_url='https://world.openfoodfacts.org/product/1234/',+ is_vegan=True,+ is_vegetarian=True,)self.assertEqual(result, data)@@ -112,6 +119,62 @@ class ExtractInfoFromOffTestCase(SimpleTestCase):self.assertEqual(result.carbohydrates_sugar, None)self.assertEqual(result.fat_saturated, None)+ def test_vegan_product(self):+ """+ Test that vegan/vegetarian status is correctly extracted+ """+ result = extract_info_from_off(self.off_data1, 1)+ self.assertTrue(result.is_vegan)+ self.assertTrue(result.is_vegetarian)++ def test_non_vegan_product(self):+ """+ Test that non-vegan product is correctly detected+ """+ self.off_data1['ingredients_analysis_tags'] = [+ 'en:palm-oil-free',+ 'en:non-vegan',+ 'en:vegetarian',+ ]+ result = extract_info_from_off(self.off_data1, 1)+ self.assertFalse(result.is_vegan)+ self.assertTrue(result.is_vegetarian)++ def test_non_vegetarian_product(self):+ """+ Test that non-vegetarian product is correctly detected+ """+ self.off_data1['ingredients_analysis_tags'] = [+ 'en:palm-oil-free',+ 'en:non-vegan',+ 'en:non-vegetarian',+ ]+ result = extract_info_from_off(self.off_data1, 1)+ self.assertFalse(result.is_vegan)+ self.assertFalse(result.is_vegetarian)++ def test_unknown_vegan_status(self):+ """+ Test that unknown vegan status returns None+ """+ self.off_data1['ingredients_analysis_tags'] = [+ 'en:palm-oil-free',+ 'en:vegan-status-unknown',+ 'en:vegetarian-status-unknown',+ ]+ result = extract_info_from_off(self.off_data1, 1)+ self.assertIsNone(result.is_vegan)+ self.assertIsNone(result.is_vegetarian)++ def test_no_analysis_tags(self):+ """+ Test that missing ingredients_analysis_tags returns None+ """+ del self.off_data1['ingredients_analysis_tags']+ result = extract_info_from_off(self.off_data1, 1)+ self.assertIsNone(result.is_vegan)+ self.assertIsNone(result.is_vegetarian)+def test_ingredient_clean_name(self):data = IngredientData(name='Stonebaked Pizza "the amer\x96ican \x99pepperoni"',