Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Merge PR #1726 (plus some changes)

Roland Geider87aae7191da8
diff --cc wger/nutrition/dataclasses.py
index 1e05ed13a,9f81f1095..dbb7329d1
--- a/wger/nutrition/dataclasses.py
+++ b/wger/nutrition/dataclasses.py
@@@ -85,16 -68,44 +86,27 @@@ class IngredientData
if value and value > 100:
raise ValueError(f'Value for {macro} is greater than 100: {value}')
+ if self.fat_saturated and self.fat_saturated > self.fat:
+ raise ValueError(
+ f'Saturated fat is greater than fat: {self.fat_saturated} > {self.fat}'
+ )
+
+ if self.carbohydrates_sugar and self.carbohydrates_sugar > self.carbohydrates:
+ raise ValueError(
- f'Sugar is greater than carbohydrates: {self.carbohydrates_sugar} > {self.carbohydrates}'
++ f'Sugar is greater than carbohydrates: '
++ f'{self.carbohydrates_sugar} > {self.carbohydrates}'
+ )
+
if self.carbohydrates + self.protein + self.fat > 100:
- raise ValueError(f'Total of carbohydrates, protein and fat is greater than 100!')
-
- # Energy approximations
- energy_protein = self.protein * ENERGY_FACTOR['protein']['metric']
- energy_carbohydrates = self.carbohydrates * ENERGY_FACTOR['carbohydrates']['metric']
- energy_fat = self.fat * ENERGY_FACTOR['fat']['metric']
- energy_calculated = energy_protein + energy_carbohydrates + energy_fat
-
- if energy_fat > self.energy:
- raise ValueError(
- f'Energy calculated from fat is greater than total energy: {energy_fat} > {self.energy}'
- )
-
- if energy_carbohydrates > self.energy:
- raise ValueError(
- f'Energy calculated from carbohydrates is greater than total energy: {energy_carbohydrates} > {self.energy}'
- )
-
- if energy_protein > self.energy:
- raise ValueError(
- f'Energy calculated from protein is greater than total energy: {energy_protein} > {self.energy}'
- )
-
- if energy_calculated > self.energy:
- raise ValueError(
- f'Total energy calculated is greater than energy: {energy_calculated} > {self.energy}'
- )
+ raise ValueError('Total of carbohydrates, protein and fat is greater than 100!')
def dict(self):
- return asdict(self)
+ data = asdict(self)
+ data.pop('serving_size_gram', None)
+ data.pop('serving_size_unit', None)
+ data.pop('serving_size_amount', None)
+ return data
+
+ def clean_name(self):
+ self.name = remove_problematic_characters(self.name)
+ self.name = change_html_entities_to_human_readable(self.name)
diff --cc wger/nutrition/models/ingredient.py
index 71d1377b3,572bc038b..709389142
--- a/wger/nutrition/models/ingredient.py
+++ b/wger/nutrition/models/ingredient.py
@@@ -24,7 -24,7 +24,6 @@@ from json import JSONDecodeErro
from django.conf import settings
from django.contrib.postgres.indexes import GinIndex
from django.core.cache import cache
--from django.core.exceptions import ValidationError
from django.core.validators import (
MaxValueValidator,
MinLengthValidator,
@@@ -47,11 -46,7 +46,8 @@@ from requests import
# wger
from wger.core.models import Language
- from wger.nutrition.consts import (
- ENERGY_FACTOR,
- KJ_PER_KCAL,
- )
+ from wger.nutrition.consts import KJ_PER_KCAL
+from wger.nutrition.dataclasses import IngredientData
from wger.nutrition.managers import ApproximateCountManager
from wger.nutrition.models.ingredient_category import IngredientCategory
from wger.nutrition.models.sources import Source
diff --cc wger/nutrition/tests/test_dataclass.py
index 000000000,e838bce66..17cab8ab4
mode 000000,100644..100644
--- a/wger/nutrition/tests/test_dataclass.py
+++ b/wger/nutrition/tests/test_dataclass.py
@@@ -1,0 -1,130 +1,80 @@@
+ # 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.dataclasses import IngredientData
+ from wger.utils.constants import CC_0_LICENSE_ID
+
+
+ class IngredientDataclassTestCase(SimpleTestCase):
+ """
+ Test validation rules
+ """
+
+ ingredient_data: IngredientData
+
+ def setUp(self):
+ self.ingredient_data = IngredientData(
+ name='Foo With Chocolate',
+ remote_id='1234567',
+ language_id=1,
+ energy=166.0,
+ protein=32.1,
+ carbohydrates=0.0,
+ carbohydrates_sugar=None,
+ fat=3.24,
+ fat_saturated=None,
+ fiber=None,
+ sodium=None,
+ code=None,
+ source_name='USDA',
+ source_url='',
+ common_name='',
+ brand='',
+ license_id=CC_0_LICENSE_ID,
+ license_author='',
+ license_title='',
+ license_object_url='',
+ )
+
+ def test_validation_ok(self):
- """"""
++ """Test that the validation proceeds with correct data"""
+ self.assertEqual(self.ingredient_data.sanity_checks(), None)
+
+ def test_validation_bigger_100(self):
+ """
+ Test the validation for values bigger than 100
+ """
+ self.ingredient_data.protein = 101
+ self.assertRaises(ValueError, self.ingredient_data.sanity_checks)
+
+ def test_validation_saturated_fat(self):
+ """
+ Test the validation for saturated fat
+ """
+ self.ingredient_data.fat = 20
+ self.ingredient_data.fat_saturated = 30
+ self.assertRaises(ValueError, self.ingredient_data.sanity_checks)
+
+ def test_validation_sugar(self):
+ """
+ Test the validation for sugar
+ """
+ self.ingredient_data.carbohydrates = 20
+ self.ingredient_data.carbohydrates_sugar = 30
+ self.assertRaises(ValueError, self.ingredient_data.sanity_checks)
-
- def test_validation_energy_fat(self):
- """
- Test the validation for energy and fat
- """
- self.ingredient_data.energy = 200
- self.ingredient_data.fat = 30 # generates 30 * 9 = 270 kcal
- self.assertRaisesRegex(
- ValueError,
- 'Energy calculated from fat',
- self.ingredient_data.sanity_checks,
- )
-
- def test_validation_energy_protein(self):
- """
- Test the validation for energy and protein
- """
- self.ingredient_data.energy = 100
- self.ingredient_data.protein = 30 # generates 30 * 4 = 120 kcal
- self.assertRaisesRegex(
- ValueError,
- 'Energy calculated from protein',
- self.ingredient_data.sanity_checks,
- )
-
- def test_validation_energy_carbohydrates(self):
- """
- Test the validation for energy and carbohydrates
- """
- self.ingredient_data.energy = 100
- self.ingredient_data.carbohydrates = 30 # generates 30 * 4 = 120 kcal
- self.assertRaisesRegex(
- ValueError,
- 'Energy calculated from carbohydrates',
- self.ingredient_data.sanity_checks,
- )
-
- def test_validation_energy_total(self):
- """
- Test the validation for energy total
- """
- self.ingredient_data.energy = 200 # less than 120 + 80 + 90
- self.ingredient_data.protein = 30 # generates 30 * 4 = 120 kcal
- self.ingredient_data.carbohydrates = 20 # generates 20 * 4 = 80 kcal
- self.ingredient_data.fat = 10 # generates 10 * 9 = 90 kcal
- self.assertRaisesRegex(
- ValueError,
- 'Total energy calculated',
- self.ingredient_data.sanity_checks,
- )
diff --cc wger/nutrition/tests/test_ingredient.py
index 53c5e3b80,dbd39ea0b..957591183
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@@ -502,16 -424,11 +471,16 @@@ class IngredientModelTestCase(WgerTestC
'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,
+ 'energy-kcal_100g': 600,
'proteins_100g': 10,
- 'carbohydrates_100g': 20,
- 'sugars_100g': 30,
+ 'carbohydrates_100g': 30,
+ 'sugars_100g': 20,
'fat_100g': 40,
'saturated-fat_100g': 11,
'sodium_100g': 5,
diff --cc wger/nutrition/tests/test_off.py
index 980c42edf,6125aa139..82a6e509c
--- a/wger/nutrition/tests/test_off.py
+++ b/wger/nutrition/tests/test_off.py
@@@ -37,17 -37,11 +37,17 @@@ class ExtractInfoFromOffTestCase(Simple
'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',
+ ],
+ 'nutrition_grades': 'c',
'nutriments': {
- 'energy-kcal_100g': 120,
+ 'energy-kcal_100g': 600,
'proteins_100g': 10,
- 'carbohydrates_100g': 20,
- 'sugars_100g': 30,
+ 'carbohydrates_100g': 30,
+ 'sugars_100g': 20,
'fat_100g': 40,
'saturated-fat_100g': 11,
'sodium_100g': 5,