Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add a sanity check on the nutritional values of an ingredient according to the following rules

Roland Geidere58baa7b7b49
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index c95bf502b..b7c401096 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -22,6 +22,7 @@ from django.db import models
from django.template.defaultfilters import slugify # django.utils.text.slugify in django 1.5!
from django.core.validators import MaxValueValidator
+from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
@@ -105,6 +106,12 @@ class Ingredient(models.Model):
An ingredient, with some approximate nutrition values
'''
+ ENERGY_APPROXIMATION = 15
+ '''
+ How much the calculated energy from protein, etc. can deviate from the
+ energy amount given.
+ '''
+
# Metaclass to set some other properties
class Meta:
ordering = ["name", ]
@@ -141,7 +148,6 @@ class Ingredient(models.Model):
fat = models.DecimalField(decimal_places=3,
max_digits=6,
- blank=True,
verbose_name=_('Fat'),
help_text=_('In g per 100g of product'))
@@ -173,6 +179,32 @@ class Ingredient(models.Model):
return reverse('wger.nutrition.views.ingredient.view',
kwargs={'id': self.id, 'slug': slugify(self.name)})
+ def clean(self):
+ '''
+ Do a very broad sanity check on the nutritional values according to
+ the following rules:
+ - 1g of protein: 4kcal
+ - 1g of carbohydrates: 4kcal
+ - 1g of fat: 9kcal
+
+ The sum is then compared to the given total energy, with ENERGY_APPROXIMATION
+ percent tolerance.
+ '''
+
+ # Note: calculations in 100 grams, to save us the '/100' everywhere
+ energy_calculated = ((self.protein * 4) +
+ (self.carbohydrates * 4) +
+ (self.fat * 9))
+
+ # Compare the values, but be generous
+ energy_upper = self.energy * (1 + (self.ENERGY_APPROXIMATION/100.0))
+ energy_lower = self.energy * (1 - (self.ENERGY_APPROXIMATION/100.0))
+ #logger.debug("{0} > {1} > {2}".format(energy_upper, energy_calculated, energy_lower))
+
+ if not ((energy_upper > energy_calculated) and (energy_calculated > energy_lower)):
+ raise ValidationError(_('Total energy is not the approximate sum of the energy ' +
+ 'provided by protein, carbohydrates and fat.'))
+
def compare_with_database(self):
'''
Compares the current ingredient with the version saved in the database.
@@ -191,11 +223,17 @@ class Ingredient(models.Model):
return True
logger.debug('equal, not doing anything')
+ def get_owner_object(self):
+ '''
+ Ingredient has no owner information
+ '''
+ return False
+
def __unicode__(self):
'''
Return a more human-readable representation
'''
- return "%s" % (self.name, )
+ return "{0}".format(self.name)
def __eq__(self, other):
'''
@@ -213,12 +251,6 @@ class Ingredient(models.Model):
equal = False
return equal
- def get_owner_object(self):
- '''
- Ingredient has no owner information
- '''
- return False
-
class WeightUnit(models.Model):
'''
diff --git a/wger/nutrition/tests/ingredient.py b/wger/nutrition/tests/ingredient.py
index 5d89c4f9b..b941287ce 100644
--- a/wger/nutrition/tests/ingredient.py
+++ b/wger/nutrition/tests/ingredient.py
@@ -17,6 +17,7 @@ import json
import datetime
from django.core.urlresolvers import reverse
+from django.core.exceptions import ValidationError
from wger.nutrition.models import Ingredient
from wger.nutrition.models import Meal
@@ -54,7 +55,7 @@ class EditIngredientTestCase(WorkoutManagerEditTestCase):
'carbohydrates_sugar': 5,
'fat_saturated': 3.14,
'fibres': 2.1,
- 'protein': 30,
+ 'protein': 20,
'carbohydrates': 10}
def post_test_hook(self):
@@ -81,7 +82,7 @@ class AddIngredientTestCase(WorkoutManagerAddTestCase):
'carbohydrates_sugar': 5,
'fat_saturated': 3.14,
'fibres': 2.1,
- 'protein': 30,
+ 'protein': 20,
'carbohydrates': 10}
def post_test_hook(self):
@@ -331,3 +332,35 @@ class IngredientTestCase(WorkoutManagerTestCase):
meal = Meal.objects.get(pk=1)
self.assertFalse(ingredient1 == meal)
+
+ def test_total_energy(self):
+ '''
+ Tests the custom clean() method
+ '''
+ self.user_login('admin')
+
+ # Values OK
+ ingredient = Ingredient()
+ ingredient.name = 'FooBar, cooked, with salt'
+ ingredient.energy = 50
+ ingredient.protein = 0.5
+ ingredient.carbohydrates = 12
+ ingredient.fat = 0.1
+ ingredient.language_id = 1
+ self.assertFalse(ingredient.full_clean())
+
+ # Values wrong
+ ingredient.protein = 20
+ self.assertRaises(ValidationError, ingredient.full_clean)
+
+ ingredient.protein = 0.5
+ ingredient.fat = 5
+ self.assertRaises(ValidationError, ingredient.full_clean)
+
+ ingredient.fat = 0.1
+ ingredient.carbohydrates = 20
+ self.assertRaises(ValidationError, ingredient.full_clean)
+
+ ingredient.fat = 5
+ ingredient.carbohydrates = 20
+ self.assertRaises(ValidationError, ingredient.full_clean)