Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use correct weight units for bmi and basal rate

Roland Geidere70f4a3e6e17
diff --git a/wger/core/models.py b/wger/core/models.py
index 98b3e715e..bf18a296d 100644
--- a/wger/core/models.py
+++ b/wger/core/models.py
@@ -29,6 +29,7 @@ from wger.gym.models import Gym
from wger.utils.helpers import disable_for_loaddata
from wger.utils.constants import TWOPLACES
+from wger.utils.units import AbstractWeight
from wger.weight.models import WeightEntry
@@ -339,8 +340,9 @@ by the US Department of Agriculture. It is extremely complete, with around
if not self.weight or not self.height:
return 0
- return self.weight / (self.height / decimal.Decimal(100) *
- self.height / decimal.Decimal(100.0))
+ weight = self.weight if self.weight_unit == 'kg' else AbstractWeight(self.weight, 'lb').kg
+ return weight / (self.height / decimal.Decimal(100) *
+ self.height / decimal.Decimal(100.0))
def calculate_basal_metabolic_rate(self, formula=1):
'''
@@ -349,9 +351,10 @@ by the US Department of Agriculture. It is extremely complete, with around
Currently only the Mifflin-St.Jeor formula is supported
'''
factor = 5 if self.gender == self.GENDER_MALE else -161
+ weight = self.weight if self.weight_unit == 'kg' else AbstractWeight(self.weight, 'lb').kg
try:
- rate = ((10 * self.weight) # in kg
+ rate = ((10 * weight) # in kg
+ (decimal.Decimal(6.25) * self.height) # in cm
- (5 * self.age) # in years
+ factor)
diff --git a/wger/nutrition/tests/test_bmi.py b/wger/nutrition/tests/test_bmi.py
index 34c30332c..3d8cddf55 100644
--- a/wger/nutrition/tests/test_bmi.py
+++ b/wger/nutrition/tests/test_bmi.py
@@ -13,11 +13,12 @@
# You should have received a copy of the GNU Affero General Public License
import json
-import decimal
import datetime
+from decimal import Decimal
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
+from wger.core.models import UserProfile
from wger.utils.constants import TWOPLACES
from wger.weight.models import WeightEntry
@@ -49,9 +50,28 @@ class BmiTestCase(WorkoutManagerTestCase):
'weight': 80})
self.assertEqual(response.status_code, 200)
bmi = json.loads(response.content.decode('utf8'))
- self.assertEqual(decimal.Decimal(bmi['bmi']), decimal.Decimal(24.69).quantize(TWOPLACES))
- self.assertEqual(decimal.Decimal(bmi['weight']), decimal.Decimal(80))
- self.assertEqual(decimal.Decimal(bmi['height']), decimal.Decimal(180))
+ self.assertEqual(Decimal(bmi['bmi']), Decimal(24.69).quantize(TWOPLACES))
+ self.assertEqual(Decimal(bmi['weight']), Decimal(80))
+ self.assertEqual(Decimal(bmi['height']), Decimal(180))
+
+ def test_calculator_imperial(self):
+
+ '''
+ Tests the calculator using imperial units
+ '''
+
+ self.user_login('test')
+ profile = UserProfile.objects.get(user__username='test')
+ profile.weight_unit = 'lb'
+ profile.save()
+ response = self.client.post(reverse('nutrition:bmi:calculate'),
+ {'height': 180,
+ 'weight': 176.36})
+ self.assertEqual(response.status_code, 200)
+ bmi = json.loads(response.content.decode('utf8'))
+ self.assertEqual(Decimal(bmi['bmi']), Decimal(24.69).quantize(TWOPLACES))
+ self.assertEqual(Decimal(bmi['weight']), Decimal(176.36).quantize(TWOPLACES))
+ self.assertEqual(Decimal(bmi['height']), Decimal(180))
def test_automatic_weight_entry(self):
'''