Self hosted FLOSS fitness/workout, nutrition and weight tracker
Refactor some of the nutrition calculations
Roland Geider184486cf1ccc
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex 239533159..597e47298 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -24,7 +24,6 @@ from django.template.defaultfilters import slugify # django.utils.text.slugifyfrom django.core.validators import MaxValueValidatorfrom django.core.validators import MinValueValidatorfrom django.core.exceptions import ValidationError-from django.core.exceptions import ObjectDoesNotExistfrom django.core.urlresolvers import reversefrom django.core import mailfrom django.core.cache import cache@@ -44,14 +43,15 @@ from wger.utils.units import AbstractWeightMEALITEM_WEIGHT_GRAM = '1'MEALITEM_WEIGHT_UNIT = '2'--PROTEIN_FACTOR = 4-CARB_FACTOR = 4-FAT_FACTOR = 9--PROTEIN_FACTOR_OZ = 113-CARB_FACTOR_OZ = 113-FAT_FACTOR_OZ = 225+ENERGY_FACTOR = {'protein': {'kg': 4,+ 'lb': 113},+ 'carbohydrates': {'kg': 4,+ 'lb': 113},+ 'fat': {'kg': 9,+ 'lb': 225}}+'''+Simple approximation of energy (kcal) provided per gram or ounce+'''logger = logging.getLogger('wger.custom')@@ -108,6 +108,7 @@ class NutritionPlan(models.Model):Sums the nutritional info of all items in the plan'''use_metric = self.user.userprofile.use_metric+ unit = 'kg' if use_metric else 'lb'result = {'total': {'energy': 0,'protein': 0,'carbohydrates': 0,@@ -127,38 +128,22 @@ class NutritionPlan(models.Model):# Energyfor meal in self.meal_set.select_related():values = meal.get_nutritional_values(use_metric=use_metric)+ for key in result['total'].keys():+ result['total'][key] += values[key]- result['total']['energy'] += values['energy']- result['total']['protein'] += values['protein']- result['total']['carbohydrates'] += values['carbohydrates']- result['total']['carbohydrates_sugar'] += values['carbohydrates_sugar']- result['total']['fat'] += values['fat']- result['total']['fat_saturated'] += values['fat_saturated']- result['total']['fibres'] += values['fibres']- result['total']['sodium'] += values['sodium']energy = result['total']['energy']# In percentif energy:- tmp_protein = result['total']['protein'] / energy * 100- tmp_carbohydrates = result['total']['carbohydrates'] / energy * 100- tmp_fat = result['total']['fat'] / energy * 100-- if use_metric:- result['percent']['protein'] = tmp_protein * PROTEIN_FACTOR- result['percent']['carbohydrates'] = tmp_carbohydrates * CARB_FACTOR- result['percent']['fat'] = tmp_fat * FAT_FACTOR- else:- result['percent']['protein'] = tmp_protein * PROTEIN_FACTOR_OZ- result['percent']['carbohydrates'] = tmp_carbohydrates * CARB_FACTOR_OZ- result['percent']['fat'] = tmp_fat * FAT_FACTOR_OZ+ for key in result['percent'].keys():+ result['percent'][key] = \+ result['total'][key] * ENERGY_FACTOR[key][unit] / energy * 100# Per body weightif self.user.userprofile.weight:weight = Decimal(self.user.userprofile.weight)- result['per_kg']['protein'] = result['total']['protein'] / weight- result['per_kg']['carbohydrates'] = result['total']['carbohydrates'] / weight- result['per_kg']['fat'] = result['total']['fat'] / weight+ for key in result['per_kg'].keys():+ result['per_kg'][key] = result['total'][key] / weight# Only 2 decimal places, anything else doesn't make sensefor key in result.keys():@@ -341,15 +326,15 @@ class Ingredient(AbstractLicenseModel, models.Model):# Note: calculations in 100 grams, to save us the '/100' everywhereenergy_protein = 0if self.protein:- energy_protein = self.protein * PROTEIN_FACTOR+ energy_protein = self.protein * ENERGY_FACTOR['protein']['kg']energy_carbohydrates = 0if self.carbohydrates:- energy_carbohydrates = self.carbohydrates * CARB_FACTOR+ energy_carbohydrates = self.carbohydrates * ENERGY_FACTOR['carbohydrates']['kg']energy_fat = 0if self.fat:- energy_fat = self.fat * FAT_FACTOR+ energy_fat = self.fat * ENERGY_FACTOR['fat']['kg']energy_calculated = energy_protein + energy_carbohydrates + energy_fat@@ -549,15 +534,8 @@ class Meal(models.Model):for item in self.mealitem_set.select_related():values = item.get_nutritional_values(use_metric=use_metric)-- nutritional_info['energy'] += values['energy']- nutritional_info['protein'] += values['protein']- nutritional_info['carbohydrates'] += values['carbohydrates']- nutritional_info['carbohydrates_sugar'] += values['carbohydrates_sugar']- nutritional_info['fat'] += values['fat']- nutritional_info['fat_saturated'] += values['fat_saturated']- nutritional_info['fibres'] += values['fibres']- nutritional_info['sodium'] += values['sodium']+ for key in nutritional_info.keys():+ nutritional_info[key] += values[key]# Only 2 decimal places, anything else doesn't make sensefor i in nutritional_info:@@ -656,10 +634,6 @@ class MealItem(models.Model):if self.ingredient.sodium:nutritional_info['sodium'] += self.ingredient.sodium * item_weight / 100- # Only 2 decimal places, anything else doesn't make sense- for i in nutritional_info:- nutritional_info[i] = Decimal(nutritional_info[i]).quantize(TWOPLACES)-# If necessary, convert weight unitsif not use_metric:for key, value in nutritional_info.iteritems():@@ -671,4 +645,8 @@ class MealItem(models.Model):# Everything else, to ouncesnutritional_info[key] = AbstractWeight(value, 'g').oz+ # Only 2 decimal places, anything else doesn't make sense+ for i in nutritional_info:+ nutritional_info[i] = Decimal(nutritional_info[i]).quantize(TWOPLACES)+return nutritional_info