Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Better handling of computed rir/rpe values

Roland Geider5aa64901fa0d
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py
index 6afa73a19..5a67e53ac 100644
--- a/wger/manager/api/serializers.py
+++ b/wger/manager/api/serializers.py
@@ -373,7 +373,9 @@ class SetConfigDataSerializer(serializers.Serializer):
repetitions_rounding = serializers.DecimalField(max_digits=4, decimal_places=2)
rir = DecimalOrIntegerField(max_digits=2, decimal_places=1)
max_rir = DecimalOrIntegerField(max_digits=2, decimal_places=1)
- rpe = DecimalOrIntegerField(max_digits=2, decimal_places=1)
+ # max_digits=3 (not 2 like rir): RPE = 10 - RiR, so a RiR of 0 yields RPE 10,
+ # which needs three digits to serialize.
+ rpe = DecimalOrIntegerField(max_digits=3, decimal_places=1)
rest = DecimalOrIntegerField(max_digits=6, decimal_places=2)
max_rest = DecimalOrIntegerField(max_digits=6, decimal_places=2)
type = serializers.CharField()
diff --git a/wger/manager/dataclasses.py b/wger/manager/dataclasses.py
index 77e0abd7a..df5adc2d5 100644
--- a/wger/manager/dataclasses.py
+++ b/wger/manager/dataclasses.py
@@ -36,10 +36,6 @@ from typing import (
from django.utils.translation import gettext as _
# wger
-from wger.core.models import (
- RepetitionUnit,
- WeightUnit,
-)
from wger.manager.consts import (
REP_UNIT_REPETITIONS,
REP_UNIT_TILL_FAILURE,
@@ -79,7 +75,9 @@ class SetConfigData:
def rpe(self):
"""Converts the RiR scale to RPE"""
- if not self.rir:
+ # Check for None rather than falsiness, so RiR 0 (no reps in reserve,
+ # i.e. maximum effort) maps to RPE 10 instead of being dropped.
+ if self.rir is None:
return None
# If the RiR is too high, just approximate to 4
diff --git a/wger/manager/models/abstract_config.py b/wger/manager/models/abstract_config.py
index 24a5cd212..b0e03cf68 100644
--- a/wger/manager/models/abstract_config.py
+++ b/wger/manager/models/abstract_config.py
@@ -35,8 +35,14 @@ from wger.manager.dataclasses import ConfigRequirements
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."""
+MAX_COMPOUND_RIR = Decimal('9.5')
+"""
+Cap for RiR / RPE (display serializer field: max_digits=2, decimal_places=1).
+
+Capped at 9.5 rather than 9.9 because ``get_config`` rounds the value to the
+nearest 0.5 before serialization: 9.9 would round up to 10.0, which needs three
+digits and overflows the two-digit ``DecimalField`` with ``decimal.InvalidOperation``.
+"""
class OperationChoices(models.TextChoices):
diff --git a/wger/manager/tests/test_set_config_data.py b/wger/manager/tests/test_set_config_data.py
index fa38b4eca..cafa4a3e9 100644
--- a/wger/manager/tests/test_set_config_data.py
+++ b/wger/manager/tests/test_set_config_data.py
@@ -12,9 +12,17 @@
#
# You should have received a copy of the GNU Affero General Public License
+# Standard Library
+from decimal import Decimal
+
# wger
from wger.core.tests.base_testcase import WgerTestCase
-from wger.manager.dataclasses import SetConfigData
+from wger.manager.api.serializers import SetConfigDataSerializer
+from wger.manager.dataclasses import (
+ SetConfigData,
+ round_value,
+)
+from wger.manager.models.abstract_config import MAX_COMPOUND_RIR
class SetConfigDataTestCase(WgerTestCase):
@@ -148,3 +156,34 @@ class SetConfigDataTestCase(WgerTestCase):
def test_high_rpe_calculation(self):
self.config.rir = 7
self.assertEqual(self.config.rpe, 4)
+
+ def test_rpe_max_effort(self):
+ """RiR 0 (no reps in reserve = maximum effort) maps to RPE 10, not None."""
+ self.config.rir = 0
+ self.assertEqual(self.config.rpe, 10)
+
+ def test_rpe_10_serializes_without_overflow(self):
+ """
+ RPE 10 (derived from RiR 0) must fit the serializer's DecimalField; a
+ two-digit field would raise decimal.InvalidOperation on the routine
+ date-sequence endpoints.
+ """
+ self.config.rir = 0
+
+ data = SetConfigDataSerializer(self.config).data
+
+ self.assertEqual(Decimal(data['rpe']), Decimal('10'))
+
+ def test_capped_rir_serializes_without_overflow(self):
+ """
+ get_config rounds RiR to the nearest 0.5 before serialization, so the
+ compound safety cap must round to a value that still fits the two-digit
+ DecimalField. A cap that rounds up to 10.0 raises
+ decimal.InvalidOperation and 500s the routine date-sequence endpoints.
+ """
+ capped_rir = round_value(MAX_COMPOUND_RIR, 0.5)
+ self.config.rir = capped_rir
+
+ data = SetConfigDataSerializer(self.config).data
+
+ self.assertEqual(Decimal(data['rir']), capped_rir)