Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

added Roland's suggested changes and debugged

Noah Pinter10f3fbff8d34
diff --git a/wger/nutrition/models.py b/wger/nutrition/models.py
index fa8b15f3e..98101ffd1 100644
--- a/wger/nutrition/models.py
+++ b/wger/nutrition/models.py
@@ -150,6 +150,7 @@ class NutritionPlan(models.Model):
result['total'][key] += values[key]
energy = result['total']['energy']
+ result['total']['energy_kilojoule'] = result['total']['energy'] * Decimal(4.184)
# In percent
if energy:
@@ -512,17 +513,16 @@ class Ingredient(AbstractSubmissionModel, AbstractLicenseModel, models.Model):
"""
return False
- def _get_energy_kilojoule(self):
+ @property
+ def energy_kilojoule(self):
"""
returns kilojoules for current ingredient, 0 if energy is uninitialized
"""
if self.energy:
- return self.energy * 4184
+ return Decimal(self.energy * 4.184).quantize(TWOPLACES)
else:
return 0
- energy_kilojoule = property(_get_energy_kilojoule)
-
class WeightUnit(models.Model):
"""
@@ -641,6 +641,8 @@ class Meal(models.Model):
for key in nutritional_info.keys():
nutritional_info[key] += values[key]
+ nutritional_info['energy_kilojoule'] = Decimal(nutritional_info['energy']) * Decimal(4.184)
+
# Only 2 decimal places, anything else doesn't make sense
for i in nutritional_info:
nutritional_info[i] = Decimal(nutritional_info[i]).quantize(TWOPLACES)
@@ -719,6 +721,8 @@ class BaseMealItem(object):
# Everything else, to ounces
nutritional_info[key] = AbstractWeight(value, 'g').oz
+ nutritional_info['energy_kilojoule'] = Decimal(nutritional_info['energy']) * Decimal(4.184)
+
# Only 2 decimal places, anything else doesn't make sense
for i in nutritional_info:
nutritional_info[i] = Decimal(nutritional_info[i]).quantize(TWOPLACES)
diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.html
index 677b63e37..c42efb169 100644
--- a/wger/nutrition/templates/plan/view.html
+++ b/wger/nutrition/templates/plan/view.html
@@ -61,7 +61,7 @@ function wgerCustomModalInit()
<tbody>
<tr>
<td colspan="2"></td>
- <td class="align-right">{% trans 'kcal' %}</td>
+ <td class="align-right">{% trans 'kcal' %} / {% trans 'kJ' %}</td>
<td class="align-right">{% trans_weight_unit 'g' owner_user %}</td>
<td class="align-right">{% trans_weight_unit 'g' owner_user %}</td>
<td class="align-right">{% trans_weight_unit 'g' owner_user %}</td>
@@ -127,7 +127,8 @@ function wgerCustomModalInit()
</span>
{% endif %}
</td>
- <td class="align-right">{{item.get_nutritional_values.energy|floatformat:1}}</td>
+ <td class="align-right">{{item.get_nutritional_values.energy|floatformat:1}} /
+ {{item.get_nutritional_values.energy_kilojoule|floatformat:1}}</td>
<td class="align-right">{{item.get_nutritional_values.protein|floatformat:1}}</td>
<td class="align-right">{{item.get_nutritional_values.carbohydrates|floatformat:1}}</td>
<td class="align-right">{{item.get_nutritional_values.fat|floatformat:1}}</td>
@@ -203,7 +204,8 @@ function wgerCustomModalInit()
<tbody>
<tr>
<td>{% trans "Energy" %}</td>
- <td class="align-right">{{nutritional_data.total.energy|floatformat:0}} {% trans "kcal" %}</td>
+ <td class="align-right">{{nutritional_data.total.energy|floatformat:0}} {% trans "kcal" %} /
+ {{nutritional_data.total.energy_kilojoule|floatformat:0}} {% trans "kJ" %}</td>
<td></td>
<td></td>
</tr>
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index b73779f60..1db53ebd0 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -235,9 +235,10 @@ class IngredientValuesTestCase(WgerTestCase):
self.assertEqual(response.status_code, 200)
result = json.loads(response.content.decode('utf8'))
- self.assertEqual(len(result), 8)
+ self.assertEqual(len(result), 9)
self.assertEqual(result, {'sodium': '0.01',
'energy': '1.76',
+ 'energy_kilojoule': '7.36',
'fat': '0.08',
'carbohydrates_sugar': '0.00',
'fat_saturated': '0.03',
@@ -253,9 +254,10 @@ class IngredientValuesTestCase(WgerTestCase):
self.assertEqual(response.status_code, 200)
result = json.loads(response.content.decode('utf8'))
- self.assertEqual(len(result), 8)
+ self.assertEqual(len(result), 9)
self.assertEqual(result, {'sodium': '0.61',
'energy': '196.24',
+ 'energy_kilojoule': '821.07',
'fat': '9.13',
'carbohydrates_sugar': '0.00',
'fat_saturated': '3.62',
@@ -263,28 +265,12 @@ class IngredientValuesTestCase(WgerTestCase):
'protein': '28.58',
'carbohydrates': '0.14'})
- def verify_kilojoules(self):
- """
- Verify kilojoules as propety of ingredient --> (ingredient.energy * 4184)
- """
- ingredient1 = Ingredient.objects.get(pk=1)
- self.assertEqual(ingredient1.energy_kilojoule, (ingredient1.energy * 4184))
- self.assertEqual(ingredient1.energy_kilojoule, 736384)
-
- ingredient2 = Ingredient()
- self.assertEqual(ingredient2.energy_kilojoule, 0)
- ingredient2.energy = 75
- self.assertEqual(ingredient2.energy_kilojoule, 313800)
- ingredient2.energy = 100
- self.assertEqual(ingredient2.energy_kilojoule, 418400)
-
def test_calculate_value_anonymous(self):
"""
Calculate the nutritional values as an anonymous user
"""
self.calculate_value()
- self.verify_kilojoules()
def test_calculate_value_logged_in(self):
"""
@@ -293,7 +279,6 @@ class IngredientValuesTestCase(WgerTestCase):
self.user_login('test')
self.calculate_value()
- self.verify_kilojoules()
class IngredientTestCase(WgerTestCase):