Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add a model method to calculate the nutritional values of a nutritional plan.

Roland Geider6224b4248882
diff --git a/nutrition/models.py b/nutrition/models.py
index aada5ba86..9fae4899c 100644
--- a/nutrition/models.py
+++ b/nutrition/models.py
@@ -40,6 +40,32 @@ class NutritionPlan(models.Model):
"""
return "Nutrition plan for %s, %s" % (self.user, self.creation_date)
+
+ def get_nutritional_values(self):
+ # Sum the nutrional info
+ nutritional_info = {'energy': 0,
+ 'protein': 0,
+ 'carbohydrates': 0,
+ 'fat': 0,
+ 'fibres': 0,
+ 'natrium': 0}
+ for meal in self.meal_set.select_related():
+ for item in meal.mealitem_set.select_related():
+
+ # Don't proceed if the ingredient was input by hand or has freetext units
+ if item.ingredient and item.amount_gramm:
+ nutritional_info['energy'] += item.ingredient.energy * item.amount_gramm / 100
+ nutritional_info['protein'] += item.ingredient.protein * item.amount_gramm / 100
+ nutritional_info['carbohydrates'] += item.ingredient.carbohydrates * item.amount_gramm / 100
+ nutritional_info['fat'] += item.ingredient.fat * item.amount_gramm / 100
+
+ if item.ingredient.fibres:
+ nutritional_info['fibres'] += item.ingredient.fibres * item.amount_gramm / 100
+
+ if item.ingredient.natrium:
+ nutritional_info['natrium'] += item.ingredient.natrium * item.amount_gramm / 100
+ return nutritional_info
+
class Ingredient(models.Model):
""" An ingredient, with some approximate nutrition values
"""
diff --git a/nutrition/views.py b/nutrition/views.py
index d1dd507ea..90a13e86b 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -102,30 +102,9 @@ def view(request, id):
language = load_language()
template_data['language'] = language
- # Sum the nutrional info
- nutritional_info = {'energy': 0,
- 'protein': 0,
- 'carbohydrates': 0,
- 'fat': 0,
- 'fibres': 0,
- 'natrium': 0}
- for meal in plan.meal_set.select_related():
- for item in meal.mealitem_set.select_related():
-
- # Don't proceed if the ingredient was input by hand or has freetext units
- if item.ingredient and item.amount_gramm:
- nutritional_info['energy'] += item.ingredient.energy * item.amount_gramm / 100
- nutritional_info['protein'] += item.ingredient.protein * item.amount_gramm / 100
- nutritional_info['carbohydrates'] += item.ingredient.carbohydrates * item.amount_gramm / 100
- nutritional_info['fat'] += item.ingredient.fat * item.amount_gramm / 100
-
- if item.ingredient.fibres:
- nutritional_info['fibres'] += item.ingredient.fibres * item.amount_gramm / 100
-
- if item.ingredient.natrium:
- nutritional_info['natrium'] += item.ingredient.natrium * item.amount_gramm / 100
- template_data['nutritional_data'] = nutritional_info
+ # Get the nutrional info
+ template_data['nutritional_data'] = plan.get_nutritional_values()
return render_to_response('view_nutrition_plan.html',
template_data,