Self hosted FLOSS fitness/workout, nutrition and weight tracker
Properly handle Null configuration options
Roland Geider23aeb3e538e9
diff --git a/wger/manager/dataclasses.py b/wger/manager/dataclasses.pyindex 63ee0d564..dac6114d9 100644--- a/wger/manager/dataclasses.py+++ b/wger/manager/dataclasses.py@@ -27,16 +27,16 @@ from typing import List@dataclassclass SetConfigData:- sets: Decimal | int- weight: Decimal | int- reps: Decimal | int- rir: Decimal | int- rest: Decimal | int+ weight: Decimal | int | None+ reps: Decimal | int | None+ rir: Decimal | int | None+ rest: Decimal | int | None- weight_unit: int = 1- reps_unit: int = 1- weight_rounding: Decimal | int = 1.25- reps_rounding: Decimal | int = 1+ sets: Decimal | int = 1+ weight_unit: int | None = 1+ reps_unit: int | None = 1+ weight_rounding: Decimal | int | None = 1.25+ reps_rounding: Decimal | int | None = 1@dataclassdiff --git a/wger/manager/fixtures/test-routine-data.json b/wger/manager/fixtures/test-routine-data.jsonindex 1fd36244d..6d7bf10f7 100644--- a/wger/manager/fixtures/test-routine-data.json+++ b/wger/manager/fixtures/test-routine-data.json@@ -56,8 +56,7 @@"fields": {"day": 1,"order": 1,- "comment": "test comment 123",- "is_dropset": false+ "comment": "test comment 123"}},{@@ -66,8 +65,7 @@"fields": {"day": 1,"order": 2,- "comment": "test comment 456",- "is_dropset": false+ "comment": "test comment 456"}}]diff --git a/wger/manager/migrations/0018_flexible_routines.py b/wger/manager/migrations/0018_flexible_routines.pyindex 058e87e68..e1429561b 100644--- a/wger/manager/migrations/0018_flexible_routines.py+++ b/wger/manager/migrations/0018_flexible_routines.py@@ -115,8 +115,10 @@ class Migration(migrations.Migration):to='core.repetitionunit',),),- ('repetition_rounding',- models.DecimalField(decimal_places=2, default=1, max_digits=4)),+ (+ 'repetition_rounding',+ models.DecimalField(decimal_places=2, default=1, max_digits=4),+ ),('slot',models.ForeignKey(@@ -133,8 +135,14 @@ class Migration(migrations.Migration):verbose_name='Unit',),),- ('weight_rounding',- models.DecimalField(decimal_places=2, default=1.25, max_digits=4, )),+ (+ 'weight_rounding',+ models.DecimalField(+ decimal_places=2,+ default=1.25,+ max_digits=4,+ ),+ ),],options={'ordering': ['order', 'id'],diff --git a/wger/manager/models/slot_config.py b/wger/manager/models/slot_config.pyindex 44ad4a10e..bd0ae02ac 100644--- a/wger/manager/models/slot_config.py+++ b/wger/manager/models/slot_config.py@@ -130,7 +130,10 @@ class SlotConfig(models.Model):return self.slot.day.routine@staticmethod- def calculate_config_value(configs: list[AbstractChangeConfig]) -> Decimal:+ def calculate_config_value(configs: list[AbstractChangeConfig]) -> Decimal | None:+ if not configs:+ return None+out = Decimal(0)for config in configs:if config.replace:@@ -181,7 +184,7 @@ class SlotConfig(models.Model):weight_config = self.weightconfig_set.filter(iteration__lte=i).last()reps_config = self.repsconfig_set.filter(iteration__lte=i).last()- if weight_config is None or reps_config is None:+ if not weight_config or not reps_config:breakelif not weight_config.need_log_to_apply and not reps_config.need_log_to_apply:@@ -193,39 +196,44 @@ class SlotConfig(models.Model):# If any of the entries in last log is greater than the last config data,# proceed. Otherwise, the weight won't change- if log_data.exists():- for log in log_data:- if log.weight >= weight and log.reps >= reps:- max_iter_weight = i- max_iter_reps = i+ for log in log_data:+ if log.weight >= weight and log.reps >= reps:+ # weight = log.weight+ # reps = log.reps++ max_iter_weight = i+ max_iter_reps = i++ # As soon as we find a log, stop+ break- # As soon as we find a log that we didn't make, stop- else:- break+ weight = self.get_weight(max_iter_weight)+ reps = self.get_reps(max_iter_reps)+ sets = self.get_sets(iteration)return SetConfigData(- sets=self.get_sets(iteration),- weight=self.get_weight(max_iter_weight),- weight_rounding=self.weight_rounding,- weight_unit=self.weight_unit.pk,- reps=self.get_reps(max_iter_reps),- reps_rounding=self.repetition_rounding,- reps_unit=self.repetition_unit.pk,+ sets=sets if sets is not None else 1,+ weight=weight,+ 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,+ 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),)- def get_sets(self, iteration: int) -> Decimal:+ def get_sets(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.setsconfig_set.filter(iteration__lte=iteration))- def get_weight(self, iteration: int) -> Decimal:+ def get_weight(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.weightconfig_set.filter(iteration__lte=iteration))- def get_reps(self, iteration: int) -> Decimal:+ def get_reps(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.repsconfig_set.filter(iteration__lte=iteration))- def get_rir(self, iteration: int) -> Decimal:+ 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:+ def get_rest(self, iteration: int) -> Decimal | None:return self.calculate_config_value(self.restconfig_set.filter(iteration__lte=iteration))diff --git a/wger/manager/tests/test_slot_config.py b/wger/manager/tests/test_slot_config.pyindex bb8b5821d..a33fbf802 100644--- a/wger/manager/tests/test_slot_config.py+++ b/wger/manager/tests/test_slot_config.py@@ -62,7 +62,10 @@ class SlotConfigTestCase(WgerTestCase):# Reduce by 2WeightConfig(- slot_config=self.slot_config, iteration=7, value=2, operation=OperationChoices.MINUS+ slot_config=self.slot_config,+ iteration=7,+ value=2,+ operation=OperationChoices.MINUS,).save()# Increase by 10%@@ -89,6 +92,10 @@ class SlotConfigTestCase(WgerTestCase):if there are logs"""+ self.slot_config.weight_rounding = 2.5+ self.slot_config.repetition_rounding = 2+ self.slot_config.save()+# Initial valueSetsConfig(slot_config=self.slot_config, iteration=1, value=4).save()RepsConfig(slot_config=self.slot_config, iteration=1, value=5).save()@@ -98,7 +105,6 @@ class SlotConfigTestCase(WgerTestCase):slot_config=self.slot_config,iteration=1,value=80,- rounding=Decimal('2.5'),).save()# Increase weight by 2.5 at iteration 2@@ -106,7 +112,6 @@ class SlotConfigTestCase(WgerTestCase):slot_config=self.slot_config,iteration=2,value=2.5,- rounding=Decimal('1.25'),need_log_to_apply=True,operation=OperationChoices.PLUS,step=StepChoices.ABSOLUTE,@@ -117,7 +122,6 @@ class SlotConfigTestCase(WgerTestCase):slot_config=self.slot_config,iteration=5,value=42,- rounding=Decimal('5'),replace=True,need_log_to_apply=False,operation=OperationChoices.PLUS,@@ -153,7 +157,7 @@ class SlotConfigTestCase(WgerTestCase):weight=80,weight_rounding=Decimal('2.5'),reps=5,- reps_rounding=1,+ reps_rounding=2,rir=2,rest=120,),@@ -164,10 +168,11 @@ class SlotConfigTestCase(WgerTestCase):SetConfigData(sets=4,weight=80,+ weight_rounding=Decimal('2.5'),reps=5,+ reps_rounding=2,rir=2,rest=120,- weight_rounding=Decimal('2.5'),),)@@ -176,10 +181,11 @@ class SlotConfigTestCase(WgerTestCase):SetConfigData(sets=4,weight=80,+ weight_rounding=Decimal('2.5'),reps=5,+ reps_rounding=2,rir=2,rest=120,- weight_rounding=Decimal('2.5'),),)@@ -188,10 +194,11 @@ class SlotConfigTestCase(WgerTestCase):SetConfigData(sets=4,weight=Decimal(82.5),+ weight_rounding=Decimal('2.5'),reps=5,+ reps_rounding=2,rir=2,rest=120,- weight_rounding=Decimal('1.25'),),)@@ -200,10 +207,11 @@ class SlotConfigTestCase(WgerTestCase):SetConfigData(sets=4,weight=42,+ weight_rounding=Decimal('2.5'),reps=5,+ reps_rounding=2,rir=2,rest=120,- weight_rounding=5,),)@@ -212,10 +220,11 @@ class SlotConfigTestCase(WgerTestCase):SetConfigData(sets=4,weight=42,+ weight_rounding=Decimal('2.5'),reps=5,+ reps_rounding=2,rir=2,rest=120,- weight_rounding=5,),)@@ -246,3 +255,23 @@ class SlotConfigTestCase(WgerTestCase):self.slot_config.get_config(3),SetConfigData(sets=4, weight=42, reps=10, rir=1, rest=90),)++ def test_empty_configs(self):+ """+ Test that the correct config is calculated if there are no configs at all+ """++ self.assertEqual(+ self.slot_config.get_config(1),+ SetConfigData(+ sets=1,+ weight=None,+ weight_rounding=None,+ weight_unit=None,+ reps=None,+ reps_rounding=None,+ reps_unit=None,+ rir=None,+ rest=None,+ ),+ )