Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fall back to the base value when a max rule has no range

Roland Geider7a6a8a762497
diff --git a/wger/manager/consts.py b/wger/manager/consts.py
index 1c9a5a72e..e389bed99 100644
--- a/wger/manager/consts.py
+++ b/wger/manager/consts.py
@@ -32,7 +32,7 @@ the rule reads, and the progression field whose displayed prescription serves
as the threshold. The ``max_*`` rules compare the same log field as their base
rule but against the top of the prescribed range, enabling double progression
(e.g. ``max_repetitions``: increase the weight only once the top of the rep
-range is reached).
+range is reached). Without a range they behave like their base rule.
"""
REQUIREMENTS_RULES_KEYS = list(REQUIREMENT_RULES.keys())
diff --git a/wger/manager/models/slot_entry.py b/wger/manager/models/slot_entry.py
index 8e769ad85..b9dbdfcf3 100644
--- a/wger/manager/models/slot_entry.py
+++ b/wger/manager/models/slot_entry.py
@@ -106,6 +106,25 @@ FIELD_CAPS = {
"""Caps per field, mirror the limits of the display serializer fields"""
+def _range_top(states: dict, field: str) -> Decimal | None:
+ """
+ Value a requirement threshold is read from.
+
+ A max field gates on the top of the displayed range, which is the base
+ value when no range is prescribed (no max config, or a max not above the
+ base). Other fields are read as-is.
+ """
+ value = states[field].value
+ base_field = BASE_FIELD.get(field)
+ if base_field is None:
+ return value
+
+ base_value = states[base_field].value
+ if value is None or (base_value is not None and value <= base_value):
+ return base_value
+ return value
+
+
def _apply_config_value(
value: Decimal | None,
config: AbstractChangeConfig,
@@ -466,7 +485,7 @@ class SlotEntry(models.Model):
# their base field.
thresholds = {
threshold_field: round_value(
- states[threshold_field].value,
+ _range_top(states, threshold_field),
self._display_rounding(BASE_FIELD.get(threshold_field, threshold_field)),
)
for _, threshold_field in REQUIREMENT_RULES.values()
diff --git a/wger/manager/tests/test_slot_entry.py b/wger/manager/tests/test_slot_entry.py
index 0eb1595bf..f65e54b89 100644
--- a/wger/manager/tests/test_slot_entry.py
+++ b/wger/manager/tests/test_slot_entry.py
@@ -1291,6 +1291,44 @@ class DoubleProgressionTestCase(WgerTestCase):
self._log_set(2, repetitions=12)
self.assertEqual(self.slot_entry.get_config_data(3).weight, Decimal('82.5'))
+ def test_max_repetitions_without_range_gates_on_repetitions(self):
+ """Without a max reps config the top of the range is the prescribed value"""
+
+ self.slot_entry.weight_rounding = Decimal('2.5')
+ self.slot_entry.repetition_rounding = 1
+ self.slot_entry.save()
+
+ RepetitionsConfig(slot_entry=self.slot_entry, iteration=1, value=10).save()
+ WeightConfig(slot_entry=self.slot_entry, iteration=1, value=80).save()
+ WeightConfig(
+ slot_entry=self.slot_entry,
+ iteration=2,
+ value=Decimal('2.5'),
+ operation=OperationChoices.PLUS,
+ step=StepChoices.ABSOLUTE,
+ requirements={'rules': ['max_repetitions']},
+ ).save()
+
+ self._log_set(1, repetitions=9)
+ self.assertEqual(self.slot_entry.get_config_data(2).weight, Decimal(80))
+
+ self._log_set(1, repetitions=10)
+ self.assertEqual(self.slot_entry.get_config_data(2).weight, Decimal('82.5'))
+
+ def test_max_repetitions_with_max_below_min_gates_on_repetitions(self):
+ """A max reps config not above the min is no range and is not displayed either"""
+
+ self._build_double_progression({'rules': ['max_repetitions']})
+ MaxRepetitionsConfig.objects.filter(slot_entry=self.slot_entry).update(value=6)
+
+ self.assertIsNone(self.slot_entry.get_config_data(1).max_repetitions)
+
+ self._log_set(1, repetitions=7)
+ self.assertEqual(self.slot_entry.get_config_data(2).weight, Decimal(80))
+
+ self._log_set(1, repetitions=8)
+ self.assertEqual(self.slot_entry.get_config_data(2).weight, Decimal('82.5'))
+
def test_max_repetitions_vs_repetitions(self):
"""``repetitions`` bumps at the bottom (8); ``max_repetitions`` does not"""