Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add "repeat" parameter to the abstract config objects
Roland Geider2e2247fa651a
diff --git a/wger/manager/api/consts.py b/wger/manager/api/consts.pyindex dd233eb93..0237de125 100644--- a/wger/manager/api/consts.py+++ b/wger/manager/api/consts.py@@ -6,4 +6,5 @@ CONFIG_FIELDS = ('operation','step','need_log_to_apply',+ 'repeat',)diff --git a/wger/manager/fixtures/test-routine-data.json b/wger/manager/fixtures/test-routine-data.jsonindex 6e9fb2f4e..361efc0b2 100644--- a/wger/manager/fixtures/test-routine-data.json+++ b/wger/manager/fixtures/test-routine-data.json@@ -165,7 +165,8 @@"value": 5,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -177,7 +178,8 @@"value": 2,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -189,7 +191,8 @@"value": 5,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -201,7 +204,8 @@"value": 3,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -213,7 +217,8 @@"value": 80,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -225,7 +230,8 @@"value": 20,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -237,7 +243,8 @@"value": 80,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -249,7 +256,8 @@"value": 50,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -261,7 +269,8 @@"value": 4,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -273,7 +282,8 @@"value": 5,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -285,7 +295,8 @@"value": 6,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}},{@@ -297,7 +308,8 @@"value": 4,"operation": "r","step": "abs",- "need_log_to_apply": false+ "need_log_to_apply": false,+ "repeat": false}}]diff --git a/wger/manager/migrations/0018_flexible_routines.py b/wger/manager/migrations/0018_flexible_routines.pyindex 552c80ef1..e2ee48406 100644--- a/wger/manager/migrations/0018_flexible_routines.py+++ b/wger/manager/migrations/0018_flexible_routines.py@@ -390,6 +390,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -436,6 +437,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -482,6 +484,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -528,6 +531,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -574,6 +578,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -620,6 +625,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -666,6 +672,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -712,6 +719,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(@@ -739,6 +747,7 @@ class Migration(migrations.Migration):),),('iteration', models.PositiveIntegerField()),+ ('value', models.PositiveIntegerField()),('operation',models.CharField(@@ -756,7 +765,7 @@ class Migration(migrations.Migration):),),('need_log_to_apply', models.BooleanField(default=False, null=True)),- ('value', models.PositiveIntegerField()),+ ('repeat', models.BooleanField(default=False)),('slot_entry',models.ForeignKey(diff --git a/wger/manager/models/abstract_config.py b/wger/manager/models/abstract_config.pyindex 17a73834a..c34c367a4 100644--- a/wger/manager/models/abstract_config.py+++ b/wger/manager/models/abstract_config.py@@ -78,6 +78,15 @@ class AbstractChangeConfig(models.Model):apply the rules anyway"""+ repeat = models.BooleanField(default=False)+ """+ This setting makes the current rule repeat for subsequent iterations until a+ new rule is defined. For example, if you want to increase the weight by 1kg+ each week, you only need to set this rule once and enable "repeat". The system+ will automatically apply it to every following week until you specify a+ different change.+ """+@propertydef replace(self):"""diff --git a/wger/manager/models/slot_entry.py b/wger/manager/models/slot_entry.pyindex b638a7f7c..daa87ff27 100644--- a/wger/manager/models/slot_entry.py+++ b/wger/manager/models/slot_entry.py@@ -15,8 +15,10 @@# along with this program. If not, see <http://www.gnu.org/licenses/>.# Standard Library+import copyimport importlibfrom decimal import Decimal+from typing import List# Djangofrom django.db import models@@ -151,7 +153,7 @@ class SlotEntry(models.Model):return self.slot.day.routine@staticmethod- def calculate_config_value(configs: list[AbstractChangeConfig]) -> Decimal | None:+ def calculate_config_value(configs: List[AbstractChangeConfig]) -> Decimal | None:if not configs:return None@@ -170,6 +172,29 @@ class SlotEntry(models.Model):return out+ @staticmethod+ def duplicate_configs(+ iteration: int,+ configs: List[AbstractChangeConfig],+ ) -> List[AbstractChangeConfig]:+ """Duplicates configs according to the "repeat" parameter"""++ out = []+ for config in configs:+ if config.repeat:+ for i in range(config.iteration, iteration + 1):+ # If there is another config responsible for the iteration, finish here+ if any(other.iteration == i and other.pk != config.pk for other in configs):+ break++ new_config = copy.copy(config)+ new_config.iteration = i+ out.append(new_config)+ else:+ out.append(config)++ return out+def get_config(self, iteration: int) -> SetConfigData:# If there is a custom class set, pass all responsibilities to itif self.class_name:@@ -255,12 +280,14 @@ class SlotEntry(models.Model):weight=weight,max_weight=max_weight if max_weight and weight and max_weight > weight else None,weight_rounding=self.weight_rounding if weight is not None else None,+ # TODO: decide on whether to return None or always the unit# weight_unit=self.weight_unit.pk if weight is not None else None,weight_unit=self.weight_unit.pk,reps=reps,max_reps=max_reps if max_reps and 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,+ # TODO: decide on whether to return None or always the unit# reps_unit=self.repetition_unit.pk if reps is not None else None,rir=self.get_rir(iteration),rest=rest,@@ -270,30 +297,72 @@ class SlotEntry(models.Model):)def get_sets(self, iteration: int) -> Decimal | None:- return self.calculate_config_value(self.setsconfig_set.filter(iteration__lte=iteration))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration,+ list(self.setsconfig_set.filter(iteration__lte=iteration)),+ )+ )def get_max_sets(self, iteration: int) -> Decimal | None:- return self.calculate_config_value(self.maxsetsconfig_set.filter(iteration__lte=iteration))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration,+ list(self.maxsetsconfig_set.filter(iteration__lte=iteration)),+ )+ )def get_weight(self, iteration: int) -> Decimal | None:- return self.calculate_config_value(self.weightconfig_set.filter(iteration__lte=iteration))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration, list(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),+ self.duplicate_configs(+ iteration,+ list(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))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration,+ list(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))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration,+ list(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))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration,+ list(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))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration,+ list(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))+ return self.calculate_config_value(+ self.duplicate_configs(+ iteration,+ list(self.maxrestconfig_set.filter(iteration__lte=iteration)),+ )+ )diff --git a/wger/manager/tests/test_generator.py b/wger/manager/tests/test_generator.pyindex c9c017ee5..06c5b1a3a 100644--- a/wger/manager/tests/test_generator.py+++ b/wger/manager/tests/test_generator.py@@ -29,7 +29,7 @@ class RoutineGeneratorTestCase(WgerTestCase):Workout.objects.all().delete()# Act- call_command('dummy-generator-routine', '--plans', 10)+ call_command('dummy-generator-routines', '--plans', 10)# Assertself.assertEqual(Workout.objects.filter(user_id=1).count(), 10)@@ -39,7 +39,7 @@ class RoutineGeneratorTestCase(WgerTestCase):Workout.objects.all().delete()# Act- call_command('dummy-generator-routine', '--plans', 1)+ call_command('dummy-generator-routines', '--plans', 1)call_command('dummy-generator-workout-diary', '--diary-entries', 10)# Assertdiff --git a/wger/manager/tests/test_slot_entry.py b/wger/manager/tests/test_slot_entry.pyindex 5a4633adc..a831365d0 100644--- a/wger/manager/tests/test_slot_entry.py+++ b/wger/manager/tests/test_slot_entry.py@@ -379,20 +379,130 @@ class SlotEntryTestCase(WgerTestCase):Test that the correct config is calculated if there are no configs at all"""- print(self.slot_entry.get_config(1))self.assertEqual(self.slot_entry.get_config(1),SetConfigData(slot_entry_id=self.slot_entry.pk,exercise=1,sets=1,+ max_sets=None,weight=None,weight_rounding=None,- weight_unit=None,+ weight_unit=1,reps=None,reps_rounding=None,- reps_unit=None,+ reps_unit=1,rir=None,rest=None,),)++ def test_duplicate_configs(self):+ configs = [+ WeightConfig(+ pk=1,+ iteration=1,+ value=80,+ operation=OperationChoices.REPLACE,+ repeat=False,+ ),+ WeightConfig(+ pk=2,+ iteration=2,+ value=2,+ operation=OperationChoices.PLUS,+ repeat=True,+ ),+ WeightConfig(+ pk=3,+ iteration=6,+ value=50,+ operation=OperationChoices.REPLACE,+ repeat=False,+ ),+ ]++ result = SlotEntry.duplicate_configs(+ 10,+ configs=configs,+ )++ # Repeats the config of iteration 2 up to the 8th one, the rest remains unchanged+ self.assertEqual(len(result), 6)++ self.assertEqual(result[0].iteration, 1)+ self.assertEqual(result[0].value, 80)++ self.assertEqual(result[1].iteration, 2)+ self.assertEqual(result[1].value, 2)++ self.assertEqual(result[2].iteration, 3)+ self.assertEqual(result[2].value, 2)++ self.assertEqual(result[3].iteration, 4)+ self.assertEqual(result[3].value, 2)++ self.assertEqual(result[4].iteration, 5)+ self.assertEqual(result[4].value, 2)++ self.assertEqual(result[5].iteration, 6)+ self.assertEqual(result[5].value, 50)++ def test_duplicate_configs_2(self):+ """Test that repeat configs can follow each other"""++ configs = [+ WeightConfig(+ pk=1,+ iteration=1,+ value=80,+ operation=OperationChoices.REPLACE,+ repeat=False,+ ),+ WeightConfig(+ pk=2,+ iteration=2,+ value=2,+ operation=OperationChoices.PLUS,+ repeat=True,+ ),+ WeightConfig(+ pk=3,+ iteration=5,+ value=3,+ operation=OperationChoices.MINUS,+ repeat=True,+ ),+ ]++ result = SlotEntry.duplicate_configs(+ 10,+ configs=configs,+ )++ # Repeats the config of iteration 2 up to the 8th one, the rest remains unchanged+ self.assertEqual(len(result), 10)++ self.assertEqual(result[0].iteration, 1)+ self.assertEqual(result[0].value, 80)+ self.assertFalse(result[0].repeat)++ self.assertEqual(result[1].iteration, 2)+ self.assertEqual(result[1].value, 2)+ self.assertTrue(result[1].repeat)++ self.assertEqual(result[2].iteration, 3)+ self.assertEqual(result[2].value, 2)+ self.assertTrue(result[2].repeat)++ self.assertEqual(result[3].iteration, 4)+ self.assertEqual(result[3].value, 2)+ self.assertTrue(result[3].repeat)++ self.assertEqual(result[4].iteration, 5)+ self.assertEqual(result[4].value, 3)+ self.assertTrue(result[4].repeat)++ self.assertEqual(result[5].iteration, 6)+ self.assertEqual(result[5].value, 3)+ self.assertTrue(result[5].repeat)