Skip to content

Commit

Permalink
Better function flexibility in get_nutrition_data()
Browse files Browse the repository at this point in the history
  • Loading branch information
purplebird committed Nov 14, 2015
1 parent 7ff10b5 commit cc79c75
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion wger/nutrition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,14 @@ def get_absolute_url(self):
'''
return reverse('nutrition:plan:view', kwargs={'id': self.id})

def get_nutritional_values(self, weight=0):
def get_nutritional_values(self, weight=None):
'''
Sums the nutritional info of all items in the plan
'''
use_metric = self.user.userprofile.use_metric
unit = 'kg' if use_metric else 'lb'
if weight is None:
weight = self.user.userprofile.weight
result = {'total': {'energy': 0,
'protein': 0,
'carbohydrates': 0,
Expand Down
2 changes: 1 addition & 1 deletion wger/nutrition/tests/test_nutritional_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_calculations_user(self):
'''
self.user_login('test')
plan = models.NutritionPlan.objects.get(pk=4)
values = plan.get_nutritional_values(plan.user.userprofile.weight)
values = plan.get_nutritional_values()

self.assertEqual(values['percent']['carbohydrates'], Decimal(29.79).quantize(TWOPLACES))
self.assertEqual(values['percent']['fat'], Decimal(20.36).quantize(TWOPLACES))
Expand Down
7 changes: 4 additions & 3 deletions wger/nutrition/views/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,11 @@ def view(request, id):
closest_weight_entry = plan.get_closest_weight_entry()
template_data['closest_weight_entry'] = closest_weight_entry
if closest_weight_entry is not None:
weight = closest_weight_entry.weight
template_data['nutritional_data'] = \
plan.get_nutritional_values(closest_weight_entry.weight)
else:
weight = 0
template_data['nutritional_data'] = plan.get_nutritional_values(weight)
template_data['nutritional_data'] = plan.get_nutritional_values()


# Tokens for the links
template_data['uid'] = uid
Expand Down

0 comments on commit cc79c75

Please sign in to comment.