Self hosted FLOSS fitness/workout, nutrition and weight tracker
Copy AbstractWeight from routine-generator branch
Roland Geider84bd76aa8a3f
diff --git a/wger/utils/constants.py b/wger/utils/constants.pyindex c09a097b7..caf3cf5b3 100644--- a/wger/utils/constants.py+++ b/wger/utils/constants.py@@ -27,6 +27,7 @@ USER_TAB = 'user'# Default quantizationTWOPLACES = Decimal('0.01')+FOURPLACES = Decimal('0.0001')# Valid date formatsDATE_FORMATS = ['%d.%m.%Y', # '25.10.2012'diff --git a/wger/utils/tests/test_unit_conversion.py b/wger/utils/tests/test_unit_conversion.pynew file mode 100644index 000000000..ed69aba06--- /dev/null+++ b/wger/utils/tests/test_unit_conversion.py@@ -0,0 +1,70 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++from decimal import Decimal++from wger.manager.tests.testcase import WorkoutManagerTestCase+from wger.utils.constants import FOURPLACES++from wger.utils.units import AbstractWeight+++class WeightConversionTestCase(WorkoutManagerTestCase):+ '''+ Test the abstract weight class+ '''++ def test_conversion(self):+ '''+ Test the weight conversion+ '''++ tmp = AbstractWeight(10)+ self.assertEqual(tmp.kg, 10)+ self.assertEqual(tmp.lb, Decimal(22.0462).quantize(FOURPLACES))++ tmp = AbstractWeight(10, 'lb')+ self.assertEqual(tmp.lb, 10)+ self.assertEqual(tmp.kg, Decimal(4.5359).quantize(FOURPLACES))++ tmp = AbstractWeight(0.4536)+ self.assertEqual(tmp.lb, Decimal(1))+ self.assertEqual(tmp.kg, Decimal(0.4536).quantize(FOURPLACES))++ tmp = AbstractWeight(80)+ self.assertEqual(tmp.lb, Decimal(176.3698).quantize(FOURPLACES))+ self.assertEqual(tmp.kg, 80)++ tmp = AbstractWeight(80, 'kg')+ self.assertEqual(tmp.lb, Decimal(176.3698).quantize(FOURPLACES))+ self.assertEqual(tmp.kg, 80)++ def test_sum(self):+ '''+ Tests adding two abstract weights+ '''+ weight1 = AbstractWeight(80, 'kg')+ weight2 = AbstractWeight(10, 'kg')+ sum = weight1 + weight2+ self.assertEqual(sum.kg, 90)++ weight1 = AbstractWeight(80, 'kg')+ weight2 = AbstractWeight(10, 'lb')+ sum = weight1 + weight2+ self.assertEqual(sum.kg, Decimal(84.5359).quantize(FOURPLACES))++ weight1 = AbstractWeight(80, 'lb')+ weight2 = AbstractWeight(10, 'lb')+ sum = weight1 + weight2+ self.assertEqual(sum.lb, Decimal(90))diff --git a/wger/utils/units.py b/wger/utils/units.pynew file mode 100644index 000000000..8d4600565--- /dev/null+++ b/wger/utils/units.py@@ -0,0 +1,77 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++import logging+from decimal import Decimal++from wger.utils.constants import TWOPLACES, FOURPLACES+++logger = logging.getLogger('wger.custom')+++'''+Weight unit classes+'''+++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+ '''++ KG_IN_LBS = Decimal(2.20462262)+ LB_IN_KG = Decimal(0.45359237)++ weight = 0+ is_kg = True++ def __init__(self, weight, mode='kg'):+ '''+ :param weight: the numerical weight+ :param mode: the mode, only the values 'kg' (default) and 'lb' are supported+ '''+ self.weight = Decimal(weight).quantize(FOURPLACES)+ self.is_kg = True if mode == 'kg' else False++ def __add__(self, other):+ '''+ Implement adding abstract weights.++ For simplicity, the sum always occurs in kg+ '''+ return AbstractWeight(self.kg + other.kg)++ @property+ def kg(self):+ '''+ :return: Return the weight in kilograms+ '''+ if self.is_kg:+ return self.weight+ else:+ return (self.weight * self.LB_IN_KG).quantize(FOURPLACES)++ @property+ def lb(self):+ '''+ :return: Return the weight in pounds+ '''+ if self.is_kg:+ return (self.weight * self.KG_IN_LBS).quantize(FOURPLACES)+ else:+ return self.weight