Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add caps for progression calculations to prevent overflow

Roland Geider78b511aa72a2
diff --git a/wger/manager/models/abstract_config.py b/wger/manager/models/abstract_config.py
index 3657af7b1..24a5cd212 100644
--- a/wger/manager/models/abstract_config.py
+++ b/wger/manager/models/abstract_config.py
@@ -14,6 +14,9 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# Standard Library
+from decimal import Decimal
+
# Django
from django.core.validators import (
MaxValueValidator,
@@ -25,6 +28,17 @@ from django.db import models
from wger.manager.dataclasses import ConfigRequirements
+# Upper bounds for progression output. Whilr individual progression rules
+# are already capped, percent operations can potetially grow beyond that
+# limit. These caps MUST mirror the ``max_digits`` / ``decimal_places`` of the
+# display serializer fields (see ``SetConfigDataSerializer``) and this model
+MAX_COMPOUND_VALUE = Decimal('9999.99')
+"""Cap for weight / repetitions / rest: max_digits=6, decimal_places=2."""
+
+MAX_COMPOUND_RIR = Decimal('9.9')
+"""Cap for RiR / RPE: max_digits=2, decimal_places=1."""
+
+
class OperationChoices(models.TextChoices):
PLUS = '+'
MINUS = '-'
diff --git a/wger/manager/models/slot_entry.py b/wger/manager/models/slot_entry.py
index 63829f2a4..dd222368c 100644
--- a/wger/manager/models/slot_entry.py
+++ b/wger/manager/models/slot_entry.py
@@ -47,6 +47,8 @@ from wger.manager.dataclasses import (
)
from wger.manager.models import WorkoutLog
from wger.manager.models.abstract_config import (
+ MAX_COMPOUND_RIR,
+ MAX_COMPOUND_VALUE,
AbstractChangeConfig,
OperationChoices,
StepChoices,
@@ -285,7 +287,17 @@ class SlotEntry(models.Model):
return [c for c in configs if c.iteration <= iteration]
@staticmethod
- def calculate_config_value(configs: List[AbstractChangeConfig]) -> Decimal | None:
+ def calculate_config_value(
+ configs: List[AbstractChangeConfig],
+ max_value: Decimal = MAX_COMPOUND_VALUE,
+ ) -> Decimal | None:
+ """
+ Calculates the result of a list of configs.
+
+ ``max_value`` clamps the running result so e.g. repeated percent progressions
+ can't overflow the display field's ``max_digits``. Callers for RiR-like
+ fields should pass ``MAX_COMPOUND_RIR``.
+ """
if not configs:
return None
@@ -293,14 +305,17 @@ class SlotEntry(models.Model):
for config in configs:
if config.replace:
out = config.value
- continue
+ else:
+ step = config.value if config.step == StepChoices.ABSOLUTE else out * config.value / 100
- step = config.value if config.step == StepChoices.ABSOLUTE else out * config.value / 100
+ if config.operation == OperationChoices.PLUS:
+ out += step
+ else:
+ out -= step
- if config.operation == OperationChoices.PLUS:
- out += step
- else:
- out -= step
+ # Safety net
+ if out > max_value:
+ out = max_value
return out
@@ -560,7 +575,8 @@ class SlotEntry(models.Model):
self.duplicate_configs(
iteration,
self.get_configuration_entries(ConfigType.RIR, iteration),
- )
+ ),
+ max_value=MAX_COMPOUND_RIR,
)
def calculate_maxrir(self, iteration: int) -> Decimal | None:
@@ -568,7 +584,8 @@ class SlotEntry(models.Model):
self.duplicate_configs(
iteration,
self.get_configuration_entries(ConfigType.MAXRIR, iteration),
- )
+ ),
+ max_value=MAX_COMPOUND_RIR,
)
def calculate_rest(self, iteration: int) -> Decimal | None:
diff --git a/wger/manager/tests/test_slot_entry.py b/wger/manager/tests/test_slot_entry.py
index e7edef4c9..8bc2c421f 100644
--- a/wger/manager/tests/test_slot_entry.py
+++ b/wger/manager/tests/test_slot_entry.py
@@ -35,6 +35,8 @@ from wger.manager.models import (
WorkoutLog,
)
from wger.manager.models.abstract_config import (
+ MAX_COMPOUND_RIR,
+ MAX_COMPOUND_VALUE,
OperationChoices,
StepChoices,
)
@@ -810,3 +812,48 @@ class SlotEntryDuplicateConfigTestCase(SimpleTestCase):
self.assertEqual(result[5].iteration, 6)
self.assertEqual(result[5].value, 3)
self.assertTrue(result[5].repeat)
+
+
+class CalculateConfigValueTestCase(SimpleTestCase):
+ def test_compound_weight_is_capped(self):
+ """Percent progressions can't push the output past MAX_COMPOUND_VALUE"""
+
+ configs = [
+ WeightConfig(iteration=1, value=100, operation=OperationChoices.REPLACE),
+ # +50% per iteration, repeated enough times to blow past 9999.99
+ *[
+ WeightConfig(
+ iteration=i,
+ value=50,
+ operation=OperationChoices.PLUS,
+ step=StepChoices.PERCENT,
+ )
+ for i in range(2, 20)
+ ],
+ ]
+
+ result = SlotEntry.calculate_config_value(configs)
+
+ self.assertEqual(result, MAX_COMPOUND_VALUE)
+
+ def test_rir_is_capped_at_rir_max(self):
+ """RiR uses the tighter cap (max_digits=2, decimal_places=1)"""
+
+ configs = [
+ RiRConfig(iteration=1, value=2, operation=OperationChoices.REPLACE),
+ RiRConfig(iteration=2, value=50, operation=OperationChoices.PLUS),
+ ]
+
+ result = SlotEntry.calculate_config_value(configs, max_value=MAX_COMPOUND_RIR)
+
+ self.assertEqual(result, MAX_COMPOUND_RIR)
+
+ def test_value_below_cap_is_unchanged(self):
+ configs = [
+ WeightConfig(iteration=1, value=80, operation=OperationChoices.REPLACE),
+ WeightConfig(iteration=2, value=5, operation=OperationChoices.PLUS),
+ ]
+
+ result = SlotEntry.calculate_config_value(configs)
+
+ self.assertEqual(result, Decimal(85))