Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

AbstractWeights can be initialised with g and oz

Roland Geiderf172cb107f0e
diff --git a/wger/utils/tests/test_unit_conversion.py b/wger/utils/tests/test_unit_conversion.py
index 4838b52bd..4ec0632b0 100644
--- a/wger/utils/tests/test_unit_conversion.py
+++ b/wger/utils/tests/test_unit_conversion.py
@@ -50,6 +50,35 @@ class WeightConversionTestCase(WorkoutManagerTestCase):
self.assertEqual(tmp.lb, Decimal(176.3698).quantize(FOURPLACES))
self.assertEqual(tmp.kg, 80)
+ def test_conversion_subunits(self):
+ '''
+ Test the weight conversion with "subunits" (grams, ounces)
+ '''
+
+ tmp = AbstractWeight(100.1, 'g')
+ self.assertEqual(tmp.kg, Decimal(0.1001).quantize(FOURPLACES))
+
+ tmp = AbstractWeight(100, 'g')
+ self.assertEqual(tmp.kg, Decimal(0.1).quantize(FOURPLACES))
+
+ tmp = AbstractWeight(1000, 'g')
+ self.assertEqual(tmp.kg, 1)
+
+ tmp = AbstractWeight(1.5, 'oz')
+ self.assertEqual(tmp.lb, Decimal(0.0938).quantize(FOURPLACES))
+
+ tmp = AbstractWeight(2, 'oz')
+ self.assertEqual(tmp.lb, Decimal(0.125).quantize(FOURPLACES))
+
+ tmp = AbstractWeight(4, 'oz')
+ self.assertEqual(tmp.lb, Decimal(0.25).quantize(FOURPLACES))
+
+ tmp = AbstractWeight(8, 'oz')
+ self.assertEqual(tmp.lb, Decimal(0.5).quantize(FOURPLACES))
+
+ tmp = AbstractWeight(16, 'oz')
+ self.assertEqual(tmp.lb, 1)
+
def test_sum(self):
'''
Tests adding two abstract weights
diff --git a/wger/utils/units.py b/wger/utils/units.py
index 245ad768d..f84a446ef 100644
--- a/wger/utils/units.py
+++ b/wger/utils/units.py
@@ -30,8 +30,9 @@ class AbstractWeight(object):
'''
Helper class to use when working with sensible (kg) or imperial units.
- For consistency, all results are converted to python decimal and quantized
- to four places
+ Internally, all values are stored as kilograms and are converted only if
+ needed. For consistency, all results are converted to python decimal and
+ quantized to four places
'''
KG_IN_LBS = Decimal(2.20462262)
@@ -43,10 +44,17 @@ class AbstractWeight(object):
def __init__(self, weight, mode='kg'):
'''
:param weight: the numerical weight
- :param mode: the mode, only the values 'kg' (default) and 'lb' are supported
+ :param mode: the mode, values 'kg' (default), 'g', 'lb' and 'oz' are supported
'''
- self.weight = Decimal(weight).quantize(FOURPLACES)
- self.is_kg = True if mode == 'kg' else False
+ weight = self.normalize(weight)
+
+ if mode == 'g':
+ weight /= Decimal(1000)
+ elif mode == 'oz':
+ weight /= Decimal(16.0)
+
+ self.weight = weight
+ self.is_kg = True if mode in ('kg', 'g') else False
def __add__(self, other):
'''
@@ -56,6 +64,16 @@ class AbstractWeight(object):
'''
return AbstractWeight(self.kg + other.kg)
+ @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 kg(self):
'''
@@ -64,9 +82,9 @@ class AbstractWeight(object):
:return: decimal
'''
if self.is_kg:
- return self.weight
+ return self.normalize(self.weight)
else:
- return (self.weight * self.LB_IN_KG).quantize(FOURPLACES)
+ return self.normalize(self.weight * self.LB_IN_KG)
@property
def g(self):
@@ -75,7 +93,7 @@ class AbstractWeight(object):
:return: decimal
'''
- return self.kg * 1000
+ return self.normalize(self.kg * 1000)
@property
def lb(self):
@@ -85,9 +103,9 @@ class AbstractWeight(object):
:return: decimal
'''
if self.is_kg:
- return (self.weight * self.KG_IN_LBS).quantize(FOURPLACES)
+ return self.normalize(self.weight * self.KG_IN_LBS)
else:
- return self.weight
+ return self.normalize(self.weight)
@property
def oz(self):
@@ -96,4 +114,4 @@ class AbstractWeight(object):
:return: decimal
'''
- return self.lb * 16
+ return self.normalize(self.lb * 16)