Self hosted FLOSS fitness/workout, nutrition and weight tracker
Implementing AbstractHeight helper class and adding call to it in BMI calculator when imperial units are selected.
koenbb43be6174650
diff --git a/wger/core/models/profile.py b/wger/core/models/profile.pyindex 1671b59a0..fe1f0a2a4 100644--- a/wger/core/models/profile.py+++ b/wger/core/models/profile.py@@ -32,7 +32,7 @@ from django.utils.translation import gettext_lazy as _# wgerfrom wger.gym.models import Gymfrom wger.utils.constants import TWOPLACES-from wger.utils.units import AbstractWeight+from wger.utils.units import AbstractWeight, AbstractHeightfrom wger.weight.models import WeightEntry# Local@@ -446,7 +446,8 @@ by the US Department of Agriculture. It is extremely complete, with aroundreturn 0weight = self.weight if self.use_metric else AbstractWeight(self.weight, 'lb').kg- return weight / pow(self.height / decimal.Decimal(100), 2)+ height = self.height if self.use_metric else AbstractHeight(self.height, 'inches').inches+ return weight / pow(height / decimal.Decimal(100), 2)def calculate_basal_metabolic_rate(self, formula=1):"""diff --git a/wger/nutrition/views/bmi.py b/wger/nutrition/views/bmi.pyindex 5ecd83b7d..7e26f9012 100644--- a/wger/nutrition/views/bmi.py+++ b/wger/nutrition/views/bmi.py@@ -57,7 +57,6 @@ def calculate(request):"""data = []-form = BmiForm(request.POST, instance=request.user.userprofile)if form.is_valid():form.save()diff --git a/wger/utils/units.py b/wger/utils/units.pyindex 1fc055968..e6bfef28c 100644--- a/wger/utils/units.py+++ b/wger/utils/units.py@@ -22,7 +22,7 @@ from wger.utils.constants import FOURPLACESlogger = logging.getLogger(__name__)"""-Weight unit classes+Weight and height unit classes"""@@ -115,3 +115,63 @@ class AbstractWeight(object)::return: decimal"""return self.normalize(self.lb * 16)++class AbstractHeight(object):+ """+ Helper class to use when working with sensible (cm) or imperial units.++ Internally, all values are stored as cm and are converted only if+ needed. For consistency, all results are converted to python decimal and+ quantized to four places+ """++ CM_IN_INCHES = Decimal(0.393701)+ INCHES_IN_CM = Decimal(2.5400)++ height = 0+ is_cm = True++ def __init__(self, height, mode='cm'):+ """+ :param height: the numerical height+ :param mode: the mode, values 'cm' (default), 'inches' are supported+ """+ height = self.normalize(height)++ self.height = height+ self.is_cm = (mode == 'cm')++ @staticmethod+ def normalize(value):+ """+ Helper method that returns quantized+ :param value:+ :return: a quantized value to four decimal places+ """++ return Decimal(value).quantize(FOURPLACES)++ @property+ def cm(self):+ """+ Return the height in cm++ :return: decimal+ """+ if self.is_cm:+ return self.normalize(self.height)+ else:+ return self.normalize(self.height * self.INCHES_IN_CM)+++ @property+ def inches(self):+ """+ Return the height in inches++ :return: decimal+ """+ if self.is_cm:+ return self.normalize(self.height * self.CM_IN_INCHES)+ else:+ return self.normalize(self.height)