Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add the possibility to configure ranges for weight, reps and rest time
Roland Geiderbea884c25196
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.pyindex 75d20deee..dadf71db3 100644--- a/wger/manager/api/serializers.py+++ b/wger/manager/api/serializers.py@@ -299,13 +299,16 @@ class SetConfigDataSerializer(serializers.Serializer):exercise = serializers.IntegerField()sets = serializers.IntegerField()weight = serializers.DecimalField(max_digits=5, decimal_places=2)+ max_weight = serializers.DecimalField(max_digits=5, decimal_places=2)weight_unit = serializers.IntegerField()weight_rounding = serializers.DecimalField(max_digits=4, decimal_places=2)reps = serializers.DecimalField(max_digits=5, decimal_places=2)+ max_reps = serializers.DecimalField(max_digits=5, decimal_places=2)reps_unit = serializers.IntegerField()reps_rounding = serializers.DecimalField(max_digits=4, decimal_places=2)rir = serializers.DecimalField(max_digits=5, decimal_places=2)rest = serializers.DecimalField(max_digits=5, decimal_places=2)+ max_rest = serializers.DecimalField(max_digits=5, decimal_places=2)type = serializers.CharField()text_repr = serializers.CharField()comment = serializers.CharField()diff --git a/wger/manager/dataclasses.py b/wger/manager/dataclasses.pyindex 533e0e842..49eb1921c 100644--- a/wger/manager/dataclasses.py+++ b/wger/manager/dataclasses.py@@ -47,6 +47,10 @@ class SetConfigData:rir: Decimal | int | Nonerest: Decimal | int | None+ max_weight: Decimal | int | None = None+ max_reps: Decimal | int | None = None+ max_rest: Decimal | int | None = None+sets: Decimal | int = 1weight_unit: int | None = 1reps_unit: int | None = 1@@ -76,6 +80,12 @@ class SetConfigData:if self.reps:reps = round_value(self.reps, self.reps_rounding)+ max_reps = (+ round_value(self.max_reps, self.reps_rounding) if self.max_reps else self.max_reps+ )++ if max_reps:+ reps = f'{reps}-{max_reps}'unit = ''if self.reps_unit not in (1, 2):@@ -89,6 +99,14 @@ class SetConfigData:if self.weight:weight = round_value(self.weight, self.weight_rounding)+ max_weight = (+ round_value(self.max_weight, self.weight_rounding)+ if self.max_weight+ else self.max_weight+ )++ if max_weight:+ weight = f'{weight}-{max_weight}'unit = ''if self.weight_unit:diff --git a/wger/manager/migrations/0018_flexible_routines.py b/wger/manager/migrations/0018_flexible_routines.pyindex 4e9926a26..2178cb9a4 100644--- a/wger/manager/migrations/0018_flexible_routines.py+++ b/wger/manager/migrations/0018_flexible_routines.py@@ -383,6 +383,63 @@ class Migration(migrations.Migration):'unique_together': {('slot_config', 'iteration')},},),+ migrations.CreateModel(+ name='MaxWeightConfig',+ fields=[+ (+ 'id',+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name='ID',+ ),+ ),+ ('iteration', models.PositiveIntegerField()),+ (+ 'trigger',+ models.CharField(+ choices=[('session', 'Session'), ('week', 'Week')],+ default='session',+ max_length=10,+ null=True,+ ),+ ),+ ('value', models.DecimalField(decimal_places=2, max_digits=6)),+ (+ 'operation',+ models.CharField(+ choices=[('+', 'Plus'), ('-', 'Minus')],+ default='+',+ max_length=1,+ null=True,+ ),+ ),+ (+ 'step',+ models.CharField(+ choices=[('abs', 'Absolute'), ('percent', 'Percent')],+ default='abs',+ max_length=10,+ null=True,+ ),+ ),+ ('replace', models.BooleanField(default=False)),+ ('need_log_to_apply', models.BooleanField(default=False)),+ (+ 'slot_config',+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ to='manager.slotconfig',+ ),+ ),+ ],+ options={+ 'ordering': ['slot_config', 'iteration'],+ 'abstract': False,+ 'unique_together': {('slot_config', 'iteration')},+ },+ ),migrations.CreateModel(name='RiRConfig',fields=[@@ -497,6 +554,63 @@ class Migration(migrations.Migration):'unique_together': {('slot_config', 'iteration')},},),+ migrations.CreateModel(+ name='MaxRestConfig',+ fields=[+ (+ 'id',+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name='ID',+ ),+ ),+ ('iteration', models.PositiveIntegerField()),+ (+ 'trigger',+ models.CharField(+ choices=[('session', 'Session'), ('week', 'Week')],+ default='session',+ max_length=10,+ null=True,+ ),+ ),+ ('value', models.DecimalField(decimal_places=2, max_digits=6)),+ (+ 'operation',+ models.CharField(+ choices=[('+', 'Plus'), ('-', 'Minus')],+ default='+',+ max_length=1,+ null=True,+ ),+ ),+ (+ 'step',+ models.CharField(+ choices=[('abs', 'Absolute'), ('percent', 'Percent')],+ default='abs',+ max_length=10,+ null=True,+ ),+ ),+ ('replace', models.BooleanField(default=False)),+ ('need_log_to_apply', models.BooleanField(default=False)),+ (+ 'slot_config',+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ to='manager.slotconfig',+ ),+ ),+ ],+ options={+ 'ordering': ['slot_config', 'iteration'],+ 'abstract': False,+ 'unique_together': {('slot_config', 'iteration')},+ },+ ),migrations.CreateModel(name='RepsConfig',fields=[@@ -553,6 +667,62 @@ class Migration(migrations.Migration):'unique_together': {('slot_config', 'iteration')},},),+ migrations.CreateModel(+ name='MaxRepsConfig',+ fields=[+ (+ 'id',+ models.AutoField(+ auto_created=True,+ primary_key=True,+ serialize=False,+ verbose_name='ID',+ ),+ ),+ ('iteration', models.PositiveIntegerField()),+ (+ 'trigger',+ models.CharField(+ choices=[('session', 'Session'), ('week', 'Week')],+ default='session',+ max_length=10,+ null=True,+ ),+ ),+ ('value', models.DecimalField(decimal_places=2, max_digits=6)),+ (+ 'operation',+ models.CharField(+ choices=[('+', 'Plus'), ('-', 'Minus')],+ default='+',+ max_length=1,+ null=True,+ ),+ ),+ (+ 'step',+ models.CharField(+ choices=[('abs', 'Absolute'), ('percent', 'Percent')],+ default='abs',+ max_length=10,+ null=True,+ ),+ ),+ ('replace', models.BooleanField(default=False)),+ ('need_log_to_apply', models.BooleanField(default=False)),+ (+ 'slot_config',+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE, to='manager.slotconfig'+ ),+ ),+ ],+ options={+ 'ordering': ['slot_config', 'iteration'],+ 'abstract': False,+ 'unique_together': {('slot_config', 'iteration')},+ },+ ),migrations.CreateModel(name='SetsConfig',fields=[diff --git a/wger/manager/models/__init__.py b/wger/manager/models/__init__.pyindex fb6f51941..a458ee7e8 100644--- a/wger/manager/models/__init__.py+++ b/wger/manager/models/__init__.py@@ -22,8 +22,14 @@ from .day import ()from .label import Labelfrom .log import WorkoutLog-from .reps_config import RepsConfig-from .rest_config import RestConfig+from .reps_config import (+ MaxRepsConfig,+ RepsConfig,+)+from .rest_config import (+ MaxRestConfig,+ RestConfig,+)from .rir_config import RiRConfigfrom .routine import Routinefrom .schedule import Schedule@@ -34,5 +40,8 @@ from .sets_config import SetsConfigfrom .setting import Settingfrom .slot import Slotfrom .slot_config import SlotConfig-from .weight_config import WeightConfig+from .weight_config import (+ MaxWeightConfig,+ WeightConfig,+)from .workout import Workoutdiff --git a/wger/manager/models/reps_config.py b/wger/manager/models/reps_config.pyindex 14d453f38..4575958c9 100644--- a/wger/manager/models/reps_config.py+++ b/wger/manager/models/reps_config.py@@ -24,3 +24,11 @@ class RepsConfig(AbstractChangeConfig):"""pass+++class MaxRepsConfig(AbstractChangeConfig):+ """+ Configuration model for the upper limit of the reps for a workout set+ """++ passdiff --git a/wger/manager/models/rest_config.py b/wger/manager/models/rest_config.pyindex 2fdf79fe5..c2952d55d 100644--- a/wger/manager/models/rest_config.py+++ b/wger/manager/models/rest_config.py@@ -24,3 +24,11 @@ class RestConfig(AbstractChangeConfig):"""pass+++class MaxRestConfig(AbstractChangeConfig):+ """+ Configuration model for the upper limit of the rest time for a workout set+ """++ passdiff --git a/wger/manager/models/slot_config.py b/wger/manager/models/slot_config.pyindex 7942aed32..885ced992 100644--- a/wger/manager/models/slot_config.py+++ b/wger/manager/models/slot_config.py@@ -185,60 +185,77 @@ class SlotConfig(models.Model):return custom_logic.calculate()- # Calculate the weights normallymax_iter_weight = 1+ max_iter_max_weight = 1max_iter_reps = 1+ max_iter_max_reps = 1weight = self.get_weight(max_iter_weight)reps = self.get_reps(max_iter_reps)+ # Calculate the weights and reps. Note that we can't just take the configs+ # and calculate it like with the other values since these might depend on+ # logged values to continue. Because of this, we need to check step by step+ # up to the current iteration if the weights and reps can be increased.for i in range(1, iteration + 1):- # We can't directly do a .get(iteration=i) because there probably isn't one- # so we simply take the last one, which will be responsible to calculate the- # weight for the current iterationweight_config = self.weightconfig_set.filter(iteration__lte=i).last()reps_config = self.repsconfig_set.filter(iteration__lte=i).last()+ # No configs, return Noneif not weight_config or not reps_config:break+ # If we don't need to consider any log values, just calculate the valueelif not weight_config.need_log_to_apply and not reps_config.need_log_to_apply:max_iter_weight = i+ max_iter_max_weight = imax_iter_reps = i+ max_iter_max_reps = i+ # We do need to check the logs, iterate over them and check that we+ # matched the planned values and continue orelif weight_config.need_log_to_apply or reps_config.need_log_to_apply:- log_data = self.workoutlog_set.filter(iteration=i - 1)+ log_data = self.workoutlog_set.filter(slot_config_id=self.id, iteration=i - 1)# If any of the entries in last log is greater than the last config data,# proceed. Otherwise, the weight won't changefor log in log_data:if log.weight >= weight and log.reps >= reps:- # weight = log.weight- # reps = log.reps-max_iter_weight = i+ max_iter_max_weight = imax_iter_reps = i+ max_iter_max_reps = i- # As soon as we find a log, stop+ # As soon as we find a matching log, stopbreak+ sets = self.get_sets(iteration)+weight = self.get_weight(max_iter_weight)+ max_weight = self.get_max_weight(max_iter_max_weight)+reps = self.get_reps(max_iter_reps)- sets = self.get_sets(iteration)+ max_reps = self.get_max_reps(max_iter_max_reps)++ rest = self.get_rest(iteration)+ max_rest = self.get_max_rest(iteration)return SetConfigData(slot_config_id=self.id,exercise=self.exercise.id,sets=sets if sets is not None else 1,weight=weight,+ max_weight=max_weight if max_weight and max_weight > weight else None,weight_rounding=self.weight_rounding if weight is not None else None,weight_unit=self.weight_unit.pk if weight is not None else None,reps=reps,+ max_reps=max_reps if max_reps and max_reps > reps else None,reps_rounding=self.repetition_rounding if reps is not None else None,reps_unit=self.repetition_unit.pk if reps is not None else None,rir=self.get_rir(iteration),- rest=self.get_rest(iteration),- type=self.type,+ rest=rest,+ max_rest=max_rest if max_rest and max_rest > rest else None,+ type=str(self.type),comment=self.comment,)@@ -248,11 +265,22 @@ class SlotConfig(models.Model):def get_weight(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.weightconfig_set.filter(iteration__lte=iteration))+ def get_max_weight(self, iteration: int) -> Decimal | None:+ return self.calculate_config_value(+ self.maxweightconfig_set.filter(iteration__lte=iteration),+ )+def get_reps(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.repsconfig_set.filter(iteration__lte=iteration))+ def get_max_reps(self, iteration: int) -> Decimal | None:+ return self.calculate_config_value(self.maxrepsconfig_set.filter(iteration__lte=iteration))+def get_rir(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.rirconfig_set.filter(iteration__lte=iteration))def get_rest(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.restconfig_set.filter(iteration__lte=iteration))++ def get_max_rest(self, iteration: int) -> Decimal | None:+ return self.calculate_config_value(self.maxrestconfig_set.filter(iteration__lte=iteration))diff --git a/wger/manager/models/weight_config.py b/wger/manager/models/weight_config.pyindex a1fe311c1..fe3bf59c9 100644--- a/wger/manager/models/weight_config.py+++ b/wger/manager/models/weight_config.py@@ -24,3 +24,11 @@ class WeightConfig(AbstractChangeConfig):"""pass+++class MaxWeightConfig(AbstractChangeConfig):+ """+ Configuration model for the upper limit of the weight for a workout set+ """++ passdiff --git a/wger/manager/tests/test_log_model.py b/wger/manager/tests/test_log_model.pyindex 2b9840912..1737d2036 100644--- a/wger/manager/tests/test_log_model.py+++ b/wger/manager/tests/test_log_model.py@@ -22,6 +22,7 @@ from wger.manager.models import (WorkoutSession,)+logger = logging.getLogger(__name__)diff --git a/wger/manager/tests/test_set_config_data.py b/wger/manager/tests/test_set_config_data.pyindex 3185bbc08..26bc01433 100644--- a/wger/manager/tests/test_set_config_data.py+++ b/wger/manager/tests/test_set_config_data.py@@ -84,3 +84,11 @@ class SetConfigDataTestCase(WgerTestCase):def test_amrap(self):self.config.reps_unit = 2self.assertEqual(self.config.text_repr, '∞ × 20 kg @ 3 RiR')++ def test_reps_range(self):+ self.config.max_reps = 6+ self.assertEqual(self.config.text_repr, '4-6 × 20 kg @ 3 RiR')++ def test_weight_range(self):+ self.config.max_weight = 30+ self.assertEqual(self.config.text_repr, '4 × 20-30 kg @ 3 RiR')diff --git a/wger/manager/tests/test_slot_config.py b/wger/manager/tests/test_slot_config.pyindex 9bc224e2b..db31d6835 100644--- a/wger/manager/tests/test_slot_config.py+++ b/wger/manager/tests/test_slot_config.py@@ -19,6 +19,8 @@ from decimal import Decimalfrom wger.core.tests.base_testcase import WgerTestCasefrom wger.manager.dataclasses import SetConfigDatafrom wger.manager.models import (+ MaxRepsConfig,+ MaxWeightConfig,RepsConfig,RestConfig,RiRConfig,@@ -134,9 +136,9 @@ class SlotConfigTestCase(WgerTestCase):# Only did 4x82.5 at iteration 2WorkoutLog(- exercise_base_id=1,+ exercise_id=1,user_id=1,- workout_id=1,+ routine_id=1,slot_config=self.slot_config,iteration=2,weight=82.5,@@ -145,9 +147,9 @@ class SlotConfigTestCase(WgerTestCase):# Did 5x82.5 at iteration 3WorkoutLog(- exercise_base_id=1,+ exercise_id=1,user_id=1,- workout_id=1,+ routine_id=1,slot_config=self.slot_config,iteration=3,weight=82.5,@@ -157,6 +159,7 @@ class SlotConfigTestCase(WgerTestCase):self.assertEqual(self.slot_config.get_config(1),SetConfigData(+ slot_config_id=self.slot_config.pk,exercise=1,sets=4,weight=80,@@ -171,6 +174,7 @@ class SlotConfigTestCase(WgerTestCase):self.assertEqual(self.slot_config.get_config(2),SetConfigData(+ slot_config_id=self.slot_config.pk,exercise=1,sets=4,weight=80,@@ -185,6 +189,7 @@ class SlotConfigTestCase(WgerTestCase):self.assertEqual(self.slot_config.get_config(3),SetConfigData(+ slot_config_id=self.slot_config.pk,exercise=1,sets=4,weight=80,@@ -199,6 +204,7 @@ class SlotConfigTestCase(WgerTestCase):self.assertEqual(self.slot_config.get_config(4),SetConfigData(+ slot_config_id=self.slot_config.pk,exercise=1,sets=4,weight=Decimal(82.5),@@ -213,6 +219,7 @@ class SlotConfigTestCase(WgerTestCase):self.assertEqual(self.slot_config.get_config(5),SetConfigData(+ slot_config_id=self.slot_config.pk,exercise=1,sets=4,weight=42,@@ -227,6 +234,7 @@ class SlotConfigTestCase(WgerTestCase):self.assertEqual(self.slot_config.get_config(6),SetConfigData(+ slot_config_id=self.slot_config.pk,exercise=1,sets=4,weight=42,@@ -238,6 +246,87 @@ class SlotConfigTestCase(WgerTestCase):),)+ def test_weight_config_with_logs_and_range(self):+ """+ Test that the weight is correctly calculated for each step / iteration+ if there are logs and there is a weight / rep range+ """++ self.slot_config.weight_rounding = 2.5+ self.slot_config.repetition_rounding = 2+ self.slot_config.save()++ # Initial value: 5-6 reps x 80-100 kg+ RepsConfig(slot_config=self.slot_config, iteration=1, value=5).save()+ MaxRepsConfig(slot_config=self.slot_config, iteration=1, value=6).save()+ WeightConfig(+ slot_config=self.slot_config,+ iteration=1,+ value=80,+ ).save()++ MaxWeightConfig(+ slot_config=self.slot_config,+ iteration=1,+ value=100,+ ).save()++ # Only did 4x82.5 at iteration 2+ WorkoutLog(+ exercise_id=1,+ user_id=1,+ routine_id=1,+ slot_config=self.slot_config,+ iteration=2,+ weight=82.5,+ reps=4,+ ).save()++ # 5x80 at iteration 3+ WorkoutLog(+ exercise_id=1,+ user_id=1,+ routine_id=1,+ slot_config=self.slot_config,+ iteration=3,+ weight=80,+ reps=5,+ ).save()++ self.assertEqual(+ self.slot_config.get_config(1),+ SetConfigData(+ slot_config_id=self.slot_config.pk,+ exercise=1,+ sets=1,+ weight=80,+ max_weight=100,+ weight_rounding=Decimal('2.5'),+ reps=5,+ max_reps=6,+ reps_rounding=2,+ rir=None,+ rest=None,+ ),+ )++ self.assertEqual(+ self.slot_config.get_config(2),+ SetConfigData(+ slot_config_id=self.slot_config.pk,+ exercise=1,+ sets=1,+ weight=80,+ max_weight=100,+ weight_rounding=Decimal('2.5'),+ reps=5,+ max_reps=6,+ reps_rounding=2,+ rir=None,+ rest=None,+ ),+ )+def test_weight_config_custom_python_class(self):"""Test that the weight is correctly calculated for each step / iteration@@ -275,6 +364,7 @@ class SlotConfigTestCase(WgerTestCase):self.assertEqual(self.slot_config.get_config(1),SetConfigData(+ slot_config_id=self.slot_config.pk,exercise=1,sets=1,weight=None,diff --git a/wger/manager/tests/test_weight_log.py b/wger/manager/tests/test_weight_log.pyindex 3b8f30eb5..827d71c8a 100644--- a/wger/manager/tests/test_weight_log.py+++ b/wger/manager/tests/test_weight_log.py@@ -38,6 +38,7 @@ from wger.manager.models import ()from wger.utils.cache import cache_mapper+logger = logging.getLogger(__name__)diff --git a/wger/weight/helpers.py b/wger/weight/helpers.pyindex 4a86c4a18..2965fa0b2 100644--- a/wger/weight/helpers.py+++ b/wger/weight/helpers.py@@ -35,6 +35,7 @@ from wger.utils.cache import cache_mapperfrom wger.utils.helpers import DecimalJsonEncoderfrom wger.weight.models import WeightEntry+logger = logging.getLogger(__name__)