Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Change the type of RiR fields in the logs to decimal

Roland Geider6dc88a54827a
diff --git a/wger/manager/consts.py b/wger/manager/consts.py
index cbd1aaba1..4a61eab74 100644
--- a/wger/manager/consts.py
+++ b/wger/manager/consts.py
@@ -15,16 +15,16 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
RIR_OPTIONS = [
- (None, '------'),
- ('0', 0),
- ('0.5', 0.5),
- ('1', 1),
- ('1.5', 1.5),
- ('2', 2),
- ('2.5', 2.5),
- ('3', 3),
- ('3.5', 3.5),
- ('4', 4),
+ None,
+ 0,
+ 0.5,
+ 1,
+ 1.5,
+ 2,
+ 2.5,
+ 3,
+ 3.5,
+ 4,
]
REP_UNIT_REPETITIONS = 1
diff --git a/wger/manager/management/commands/dummy-generator-routines.py b/wger/manager/management/commands/dummy-generator-routines.py
index 557002ad4..63d1eb6da 100644
--- a/wger/manager/management/commands/dummy-generator-routines.py
+++ b/wger/manager/management/commands/dummy-generator-routines.py
@@ -126,7 +126,7 @@ class Command(BaseCommand):
sets = random.randint(2, 4)
weight = random.randint(20, 100)
rest = random.choice(range(100, 201, 10))
- rir = random.choice(RIR_OPTIONS)[0]
+ rir = random.choice(RIR_OPTIONS)
if random.random() < 0.9:
SetsConfig(
diff --git a/wger/manager/management/commands/dummy-generator-workout-diary.py b/wger/manager/management/commands/dummy-generator-workout-diary.py
index dbc04658b..d410f76a2 100644
--- a/wger/manager/management/commands/dummy-generator-workout-diary.py
+++ b/wger/manager/management/commands/dummy-generator-workout-diary.py
@@ -81,7 +81,7 @@ class Command(BaseCommand):
rir = (
set_data.rir + random.randint(0, 1)
if set_data.rir
- else random.choice(RIR_OPTIONS)[0]
+ else random.choice(RIR_OPTIONS)
)
rest = (
diff --git a/wger/manager/migrations/0022_alter_rir_type.py b/wger/manager/migrations/0022_alter_rir_type.py
new file mode 100644
index 000000000..30066246c
--- /dev/null
+++ b/wger/manager/migrations/0022_alter_rir_type.py
@@ -0,0 +1,43 @@
+# Generated by Django 4.2.20 on 2025-03-25 19:34
+
+from django.db import migrations, models
+import wger.manager.validators
+
+
+def migrate_forward(apps, schema_editor):
+ Log = apps.get_model('manager', 'WorkoutLog')
+
+ Log.objects.filter(rir='').update(rir=None)
+ Log.objects.filter(rir_target='').update(rir=None)
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ('manager', '0021_flexible_routines_cleanup'),
+ ]
+
+ operations = [
+ migrations.RunPython(migrate_forward, reverse_code=migrations.RunPython.noop),
+ migrations.AlterField(
+ model_name='workoutlog',
+ name='rir',
+ field=models.DecimalField(
+ blank=True,
+ decimal_places=1,
+ max_digits=2,
+ null=True,
+ validators=[wger.manager.validators.NullMinValueValidator(0)],
+ ),
+ ),
+ migrations.AlterField(
+ model_name='workoutlog',
+ name='rir_target',
+ field=models.DecimalField(
+ blank=True,
+ decimal_places=1,
+ max_digits=2,
+ null=True,
+ validators=[wger.manager.validators.NullMinValueValidator(0)],
+ ),
+ ),
+ ]
diff --git a/wger/manager/models/log.py b/wger/manager/models/log.py
index 744ebe66f..5060d5d0d 100644
--- a/wger/manager/models/log.py
+++ b/wger/manager/models/log.py
@@ -31,12 +31,14 @@ from wger.core.models import (
from wger.exercises.models import Exercise
from wger.manager.consts import (
REP_UNIT_REPETITIONS,
- RIR_OPTIONS,
WEIGHT_UNIT_KG,
)
from wger.manager.managers import WorkoutLogManager
from wger.manager.models.session import WorkoutSession
-from wger.manager.validators import NullMinValueValidator
+from wger.manager.validators import (
+ NullMinValueValidator,
+ validate_rir,
+)
from wger.utils.cache import reset_workout_log
@@ -177,24 +179,24 @@ class WorkoutLog(models.Model):
Target amount of weight
"""
- rir = models.CharField(
- verbose_name=_('RiR'),
- max_length=3,
- blank=True,
+ rir = models.DecimalField(
+ max_digits=2,
+ decimal_places=1,
+ validators=[NullMinValueValidator(0), validate_rir],
null=True,
- choices=RIR_OPTIONS,
+ blank=True,
)
"""
Reps in Reserve, RiR. The amount of reps that could realistically still be
done in the set.
"""
- rir_target = models.CharField(
- verbose_name=_('RiR'),
- max_length=3,
- blank=True,
+ rir_target = models.DecimalField(
+ max_digits=2,
+ decimal_places=1,
+ validators=[NullMinValueValidator(0), validate_rir],
null=True,
- choices=RIR_OPTIONS,
+ blank=True,
)
"""
Target Reps in Reserve
diff --git a/wger/manager/models/rir_config.py b/wger/manager/models/rir_config.py
index b8882b346..ba4671e1f 100644
--- a/wger/manager/models/rir_config.py
+++ b/wger/manager/models/rir_config.py
@@ -19,6 +19,10 @@ from django.db import models
# wger
from wger.manager.models import AbstractChangeConfig
+from wger.manager.validators import (
+ NullMinValueValidator,
+ validate_rir,
+)
class RiRConfig(AbstractChangeConfig):
@@ -29,7 +33,7 @@ class RiRConfig(AbstractChangeConfig):
value = models.DecimalField(
decimal_places=1,
max_digits=2,
- # choices=RIR_OPTIONS
+ validators=[NullMinValueValidator(0), validate_rir],
)
@@ -41,5 +45,5 @@ class MaxRiRConfig(AbstractChangeConfig):
value = models.DecimalField(
decimal_places=1,
max_digits=2,
- # choices=RIR_OPTIONS
+ validators=[NullMinValueValidator(0), validate_rir],
)
diff --git a/wger/manager/tests/test_api_rir.py b/wger/manager/tests/test_api_rir.py
new file mode 100644
index 000000000..77e2b78be
--- /dev/null
+++ b/wger/manager/tests/test_api_rir.py
@@ -0,0 +1,101 @@
+# 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
+
+# Django
+from django.urls import reverse
+
+# Third Party
+from rest_framework import status
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+
+
+class RiRApiTestCase(WgerTestCase):
+ """
+ Test the validators for the RiR entries
+ """
+
+ def setUp(self):
+ super().setUp()
+ self.user_login()
+
+ def test_rir_config_1(self):
+ response = self.client.post(
+ reverse('rir-config-list'),
+ data={
+ 'iteration': 1,
+ 'slot_entry': 1,
+ 'value': 1.4,
+ "operation": "r",
+ "step": "abs",
+ }
+ )
+ data = response.json()
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn('1.4 is not a valid RiR option', data['value'][0], )
+
+ def test_rir_config_2(self):
+ response = self.client.post(
+ reverse('rir-config-list'),
+ data={
+ 'iteration': 1,
+ 'slot_entry': 1,
+ 'value': -1,
+ "operation": "r",
+ "step": "abs",
+ }
+ )
+ data = response.json()
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn(
+ 'Ensure this value is either NULL or greater than or equal to 0.',
+ data['value'][0],
+ )
+
+ def test_max_rir_config1(self):
+ response = self.client.post(
+ reverse('max-rir-config-list'),
+ data={
+ 'iteration': 1,
+ 'slot_entry': 1,
+ 'value': -1.4,
+ "operation": "r",
+ "step": "abs",
+ }
+ )
+ data = response.json()
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn('-1.4 is not a valid RiR option', data['value'][1])
+ self.assertIn(
+ 'Ensure this value is either NULL or greater than or equal to 0.',
+ data['value'][0]
+ )
+
+ def test_log_1(self):
+ response = self.client.post(
+ reverse('workoutlog-list'),
+ data={
+ 'iteration': 1,
+ 'exercise': 1,
+ 'slot_entry': 1,
+ 'rir': 1.3,
+ 'rir_target': 1.6,
+ }
+ )
+ data = response.json()
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertIn('1.3 is not a valid RiR option', data['rir'][0])
+ self.assertIn('1.6 is not a valid RiR option', data['rir_target'][0])
diff --git a/wger/manager/validators.py b/wger/manager/validators.py
index 38341d7b6..fcbfb0892 100644
--- a/wger/manager/validators.py
+++ b/wger/manager/validators.py
@@ -14,9 +14,13 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
# Django
+from django.core.exceptions import ValidationError
from django.core.validators import BaseValidator
from django.utils.deconstruct import deconstructible
+# wger
+from wger.manager.consts import RIR_OPTIONS
+
@deconstructible
class NullMinValueValidator(BaseValidator):
@@ -28,3 +32,8 @@ class NullMinValueValidator(BaseValidator):
return True
return a < b
+
+
+def validate_rir(value):
+ if value not in RIR_OPTIONS:
+ raise ValidationError(f'{value} is not a valid RiR option: {RIR_OPTIONS}')