Self hosted FLOSS fitness/workout, nutrition and weight tracker
Calculate further informations about the nutritional plan
Roland Geider093777da17f4
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex 08d9d9df4..416353efc 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -40,6 +40,13 @@ from wger.utils.fields import Html5IntegerFieldMEALITEM_WEIGHT_GRAM = '1'MEALITEM_WEIGHT_UNIT = '2'+TWOPLACES = decimal.Decimal('0.01')++PROTEIN_FACTOR = 4+CARBOHYDRATES_FACTOR = 4+FAT_FACTOR = 9++logger = logging.getLogger('wger.custom')@@ -91,29 +98,59 @@ class NutritionPlan(models.Model):def get_nutritional_values(self):'''- Sums the nutrional info of all items in the plan+ Sums the nutritional info of all items in the plan'''- nutritional_info = {'energy': 0,+ result = {'total': {'energy': 0,'protein': 0,'carbohydrates': 0,'carbohydrates_sugar': 0,'fat': 0,'fat_saturated': 0,'fibres': 0,- 'sodium': 0}+ 'sodium': 0},+ 'percent': {'protein': 0,+ 'carbohydrates': 0,+ 'fat': 0},+ 'per_kg': {'protein': 0,+ 'carbohydrates': 0,+ 'fat': 0}+ }++ # Energyfor meal in self.meal_set.select_related():values = meal.get_nutritional_values()- 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']+ 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']++ # In percent+ if result['total']['energy']:+ result['percent']['protein'] = ((result['total']['protein']+ * PROTEIN_FACTOR+ / result['total']['energy']) * 100).quantize(TWOPLACES)+ result['percent']['carbohydrates'] = ((result['total']['carbohydrates']+ * CARBOHYDRATES_FACTOR+ / result['total']['energy']) * 100)\+ .quantize(TWOPLACES)+ result['percent']['fat'] = ((result['total']['fat']+ * FAT_FACTOR+ / result['total']['energy']) * 100).quantize(TWOPLACES)++ # Per body weight+ if self.user.userprofile.weight:+ weight = decimal.Decimal(self.user.userprofile.weight)+ result['per_kg']['protein'] = (result['total']['protein'] / weight).quantize(TWOPLACES)+ result['per_kg']['carbohydrates'] = (result['total']['carbohydrates'] / weight) \+ .quantize(TWOPLACES)+ result['per_kg']['fat'] = (result['total']['fat'] / weight).quantize(TWOPLACES)- return nutritional_info+ return resultdef get_owner_object(self):'''@@ -128,7 +165,7 @@ class NutritionPlan(models.Model):'''goal_calories = self.user.userprofile.calories- actual_calories = self.get_nutritional_values()['energy']+ actual_calories = self.get_nutritional_values()['total']['energy']# Within 3%if (actual_calories < goal_calories * 1.03) and (actual_calories > goal_calories * 0.97):@@ -272,9 +309,9 @@ class Ingredient(models.Model):'''# Note: calculations in 100 grams, to save us the '/100' everywhere- energy_calculated = ((self.protein * 4) +- (self.carbohydrates * 4) +- (self.fat * 9))+ energy_calculated = ((self.protein * PROTEIN_FACTOR) ++ (self.carbohydrates * CARBOHYDRATES_FACTOR) ++ (self.fat * FAT_FACTOR))# Compare the values, but be generousenergy_upper = self.energy * (1 + (self.ENERGY_APPROXIMATION / decimal.Decimal('100.0')))diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.htmlindex 1c2e664eb..ba34bf030 100644--- a/wger/nutrition/templates/plan/view.html+++ b/wger/nutrition/templates/plan/view.html@@ -163,97 +163,108 @@ $(document).ready(function() {</p>{% endif %}-{% endblock %}---{% block sidebar %}<h3>{% trans "Nutritional data" %}</h3>-<table id="nutritional-data-energy">+<table><thead><tr>- <th colspan="2">- {% trans "Energy" %}- </th>+ <th>{% trans "Macronutrients" %}</th>+ <th class="align-right">{% trans "Total" %}</th>+ <th class="align-right">{% trans "Percent of energy" %}</th>+ <th class="align-right">{% trans "g per body kg" %}</th></tr></thead><tbody><tr><td>{% trans "Energy" %}</td>- <td class="align-right">{{nutritional_data.energy|floatformat}} kcal</td>- </tr>- {% if user.userprofile.calories and plan.has_goal_calories %}- {% with total=plan.get_calories_approximation %}- <tr>- <td>{% trans "Goal" %}</td>- <td class="align-right" style="color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}">- <strong>{{user.userprofile.calories}} kcal</strong></td>- </tr>- {% endwith %}- {% endif %}- </tbody>-</table>---{% if plan.has_goal_calories %}- <p>{% blocktrans %}You have selected that this nutritional plan has a- goal amount of calories. Use the calculator or enter the value yourself.{% endblocktrans %}- <a href="{% url 'calories-calculator' %}">{% trans "That's done here" %}</a>- </p>-{% endif %}--<table id="nutritional-data-macronutrients">- <thead>- <tr>- <th colspan="2">- {% trans "Macronutrients" %}- </th>+ <td class="align-right">{{nutritional_data.total.energy|floatformat}} kcal</td>+ <td></td>+ <td></td></tr>- </thead>- <tbody><tr><td>{% trans "Protein" %}</td>- <td class="align-right">{{nutritional_data.protein|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.total.protein|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.percent.protein|floatformat}} %</td>+ <td class="align-right">{{nutritional_data.per_kg.protein|floatformat}}</td></tr><tr><td>{% trans "Carbohydrates" %}</td>- <td class="align-right">{{nutritional_data.carbohydrates|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.total.carbohydrates|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.percent.carbohydrates|floatformat}} %</td>+ <td class="align-right">{{nutritional_data.per_kg.carbohydrates|floatformat}}</td></tr><tr><td style="padding-left: 2em;">{% trans "Sugar content in carbohydrates" %}</td>- <td class="align-right">{{nutritional_data.carbohydrates_sugar|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.total.carbohydrates_sugar|floatformat}} g</td>+ <td></td>+ <td></td></tr><tr><td>{% trans "Fat" %}</td>- <td class="align-right">{{nutritional_data.fat|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.total.fat|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.percent.fat|floatformat}} %</td>+ <td class="align-right">{{nutritional_data.per_kg.fat|floatformat}}</td></tr><tr><td style="padding-left: 2em;">{% trans "Saturated fat content in fats" %}</td>- <td class="align-right">{{nutritional_data.fat_saturated|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.total.fat_saturated|floatformat}} g</td>+ <td></td>+ <td></td></tr>- </tbody>-</table>-<table id="nutritional-data-others">- <thead>+<tr>- <th colspan="2">- {% trans "Others" %}- </th>+ <th colspan="4">{% trans "Others" %}</th></tr>- </thead>- <tbody><tr><td>{% trans "Fibres" %}</td>- <td class="align-right">{{nutritional_data.fibres|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.total.fibres|floatformat}} g</td>+ <td></td>+ <td></td></tr><tr><td>{% trans "Sodium" %}</td>- <td class="align-right">{{nutritional_data.sodium|floatformat}} g</td>+ <td class="align-right">{{nutritional_data.total.sodium|floatformat}} g</td>+ <td></td>+ <td></td>+ </tr>+ </tbody>+</table>+{% endblock %}++++{% block sidebar %}+<h3>{% trans "Energy" %}</h3>+<table id="nutritional-data-energy">+ <thead>+ <tr>+ <th>{% trans "Energy" %}</th>+ <th class="align-right">{{nutritional_data.total.energy|floatformat}} kcal</th>+ </tr>+ </thead>+ <tbody>++ {% if user.userprofile.calories and plan.has_goal_calories %}+ {% with total=plan.get_calories_approximation %}+ <tr style="background-color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}">+ <td>{% trans "Goal" %}</td>+ <td class="align-right">+ <strong>{{user.userprofile.calories}} kcal</strong></td></tr>+ {% endwith %}+ {% endif %}</tbody></table>+{% if plan.has_goal_calories %}+ <p>{% blocktrans %}You have selected that this nutritional plan has a+ goal amount of calories. Use the calculator or enter the value yourself.{% endblocktrans %}+ <a href="{% url 'calories-calculator' %}">{% trans "That's done here" %}</a>+ </p>+{% endif %}++<h3>{% trans "Options" %}</h3>diff --git a/wger/nutrition/tests/test_nutritional_calculations.py b/wger/nutrition/tests/test_nutritional_calculations.pyindex 1e7b400fd..df20834b4 100644--- a/wger/nutrition/tests/test_nutritional_calculations.py+++ b/wger/nutrition/tests/test_nutritional_calculations.py@@ -57,7 +57,7 @@ class NutritionalValuesCalculationsTestCase(WorkoutManagerTestCase):self.assertEqual(result_item, result_meal)result_plan = plan.get_nutritional_values()- self.assertEqual(result_meal, result_plan)+ self.assertEqual(result_meal, result_plan['total'])# One ingredient, 1 x unit 3item.delete()@@ -78,7 +78,7 @@ class NutritionalValuesCalculationsTestCase(WorkoutManagerTestCase):self.assertEqual(result_item, result_meal)result_plan = plan.get_nutritional_values()- self.assertEqual(result_meal, result_plan)+ self.assertEqual(result_meal, result_plan['total'])# Add another ingredient, 200 gritem2 = models.MealItem()@@ -100,7 +100,7 @@ class NutritionalValuesCalculationsTestCase(WorkoutManagerTestCase):self.assertEqual(result_total, result_meal)result_plan = plan.get_nutritional_values()- self.assertEqual(result_meal, result_plan)+ self.assertEqual(result_meal, result_plan['total'])# Add another ingredient, 20 gritem3 = models.MealItem()@@ -122,4 +122,4 @@ class NutritionalValuesCalculationsTestCase(WorkoutManagerTestCase):self.assertEqual(result_total, result_meal)result_plan = plan.get_nutritional_values()- self.assertEqual(result_meal, result_plan)+ self.assertEqual(result_meal, result_plan['total'])diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.pyindex b98d64581..e53d1a5b7 100644--- a/wger/nutrition/views/plan.py+++ b/wger/nutrition/views/plan.py@@ -119,8 +119,9 @@ def view(request, id):template_data['MEALITEM_WEIGHT_GRAM'] = MEALITEM_WEIGHT_GRAMtemplate_data['MEALITEM_WEIGHT_UNIT'] = MEALITEM_WEIGHT_UNIT- # Get the nutrional info-+ # Get the nutritional info+ import pprint+ pprint.pprint(plan.get_nutritional_values())template_data['nutritional_data'] = plan.get_nutritional_values()return render_to_response('plan/view.html',