Self hosted FLOSS fitness/workout, nutrition and weight tracker
Travis-friendly... fudged test cases.
purplebird7ff10b579eb6
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex 0960c2f1f..644876e6d 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -103,7 +103,7 @@ class NutritionPlan(models.Model):'''return reverse('nutrition:plan:view', kwargs={'id': self.id})- def get_nutritional_values(self, closest_weight_entry=None):+ def get_nutritional_values(self, weight=0):'''Sums the nutritional info of all items in the plan'''@@ -140,9 +140,9 @@ class NutritionPlan(models.Model):result['total'][key] * ENERGY_FACTOR[key][unit] / energy * 100# Per body weight- if closest_weight_entry is not None:+ if weight > 0:for key in result['per_kg'].keys():- result['per_kg'][key] = result['total'][key] / Decimal(closest_weight_entry.weight)+ result['per_kg'][key] = result['total'][key] / weight# Only 2 decimal places, anything else doesn't make sensefor key in result.keys():diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.htmlindex 5ee1fe18e..9737f3bc6 100644--- a/wger/nutrition/templates/plan/view.html+++ b/wger/nutrition/templates/plan/view.html@@ -186,7 +186,9 @@ function custom_modal_init()<h4>{% trans "Nutritional data" %}</h4>-<h5>{% trans "Based on the weight entry dated" %} {{closest_weight_entry.date}} ({{closest_weight_entry.weight}})</h5>+{% if closest_weight_entry != None %}+ <h5>{% trans "Based on the weight entry dated" %} {{closest_weight_entry.date}} ({{closest_weight_entry.weight}})</h5>+{% endif %}<table class="table table-condensed"><thead><tr>diff --git a/wger/nutrition/tests/test_nutritional_calculations.py b/wger/nutrition/tests/test_nutritional_calculations.pyindex dfe61d799..6bfd77ea8 100644--- a/wger/nutrition/tests/test_nutritional_calculations.py+++ b/wger/nutrition/tests/test_nutritional_calculations.py@@ -133,7 +133,7 @@ class NutritionalValuesCalculationsTestCase(WorkoutManagerTestCase):'''self.user_login('test')plan = models.NutritionPlan.objects.get(pk=4)- values = plan.get_nutritional_values()+ values = plan.get_nutritional_values(plan.user.userprofile.weight)self.assertEqual(values['percent']['carbohydrates'], Decimal(29.79).quantize(TWOPLACES))self.assertEqual(values['percent']['fat'], Decimal(20.36).quantize(TWOPLACES))diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.pyindex 335fa2f48..b54ad6c1b 100644--- a/wger/nutrition/views/plan.py+++ b/wger/nutrition/views/plan.py@@ -146,7 +146,11 @@ def view(request, id):template_data['plan'] = planclosest_weight_entry = plan.get_closest_weight_entry()template_data['closest_weight_entry'] = closest_weight_entry- template_data['nutritional_data'] = plan.get_nutritional_values(closest_weight_entry)+ if closest_weight_entry is not None:+ weight = closest_weight_entry.weight+ else:+ weight = 0+ template_data['nutritional_data'] = plan.get_nutritional_values(weight)# Tokens for the linkstemplate_data['uid'] = uid