Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add support for grams and ounces

Roland Geider64717b50c5bd
diff --git a/wger/utils/tests/test_unit_conversion.py b/wger/utils/tests/test_unit_conversion.py
index ed69aba06..4838b52bd 100644
--- a/wger/utils/tests/test_unit_conversion.py
+++ b/wger/utils/tests/test_unit_conversion.py
@@ -68,3 +68,14 @@ class WeightConversionTestCase(WorkoutManagerTestCase):
weight2 = AbstractWeight(10, 'lb')
sum = weight1 + weight2
self.assertEqual(sum.lb, Decimal(90))
+
+ def test_subunits(self):
+ '''
+ Test weight subunit calculations
+ '''
+
+ tmp = AbstractWeight(10)
+ self.assertEqual(tmp.g, 10000)
+
+ tmp = AbstractWeight(2, 'lb')
+ self.assertEqual(tmp.oz, 32)
diff --git a/wger/utils/units.py b/wger/utils/units.py
index 8d4600565..245ad768d 100644
--- a/wger/utils/units.py
+++ b/wger/utils/units.py
@@ -59,19 +59,41 @@ class AbstractWeight(object):
@property
def kg(self):
'''
- :return: Return the weight in kilograms
+ Return the weight in kilograms
+
+ :return: decimal
'''
if self.is_kg:
return self.weight
else:
return (self.weight * self.LB_IN_KG).quantize(FOURPLACES)
+ @property
+ def g(self):
+ '''
+ Return the weight in grams
+
+ :return: decimal
+ '''
+ return self.kg * 1000
+
@property
def lb(self):
'''
- :return: Return the weight in pounds
+ Return the weight in pounds
+
+ :return: decimal
'''
if self.is_kg:
return (self.weight * self.KG_IN_LBS).quantize(FOURPLACES)
else:
return self.weight
+
+ @property
+ def oz(self):
+ '''
+ Return the weight in ounces
+
+ :return: decimal
+ '''
+ return self.lb * 16