Self hosted FLOSS fitness/workout, nutrition and weight tracker
add escape hatch in form of custom logic used to calculate the sets
Roland Geider2d36272a64dc
diff --git a/wger/manager/config_calculations/__init__.py b/wger/manager/config_calculations/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/wger/manager/config_calculations/calculations.py b/wger/manager/config_calculations/calculations.pynew file mode 100644index 000000000..5da4bc8bf--- /dev/null+++ b/wger/manager/config_calculations/calculations.py@@ -0,0 +1,44 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) wger Team+#+# 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 Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# Standard Library+from abc import (+ ABC,+ abstractmethod,+)+from decimal import Decimal++# wger+from wger.manager.models import AbstractChangeConfig+++class AbstractSetCalculations(ABC):+ iteration: int+ weight_configs: list[AbstractChangeConfig]+ reps_configs: list[AbstractChangeConfig]+ rir_configs: list[AbstractChangeConfig]+ rest_configs: list[AbstractChangeConfig]++ def __init__(self, iteration, weight_configs, reps_configs, rir_configs, rest_configs):+ self.iteration = iteration+ self.weight_configs = weight_configs+ self.reps_configs = reps_configs+ self.rir_configs = rir_configs+ self.rest_configs = rest_configs++ @abstractmethod+ def calculate(self) -> Decimal | int:+ raise NotImplementedError()diff --git a/wger/manager/config_calculations/dummy.py b/wger/manager/config_calculations/dummy.pynew file mode 100644index 000000000..13ec738ed--- /dev/null+++ b/wger/manager/config_calculations/dummy.py@@ -0,0 +1,37 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) wger Team+#+# 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 Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# wger+from wger.manager.config_calculations.calculations import AbstractSetCalculations+from wger.manager.dataclasses import SetConfigData+++class SetCalculations(AbstractSetCalculations):+ def calculate(self):+ if self.iteration == 1:+ return SetConfigData(+ weight=24,+ reps=1,+ rir=2,+ rest=120,+ )+ else:+ return SetConfigData(+ weight=42,+ reps=10,+ rir=1,+ rest=90,+ )diff --git a/wger/manager/dataclasses.py b/wger/manager/dataclasses.pyindex d442d466b..30ef62e34 100644--- a/wger/manager/dataclasses.py+++ b/wger/manager/dataclasses.py@@ -7,7 +7,7 @@ from wger.manager.models import DayNg@dataclass-class WorkoutDay:+class WorkoutDayData:day: DayNgiteration: intdiff --git a/wger/manager/migrations/0018_flexible_routines.py b/wger/manager/migrations/0018_flexible_routines.pyindex 91542c17f..71633e093 100644--- a/wger/manager/migrations/0018_flexible_routines.py+++ b/wger/manager/migrations/0018_flexible_routines.py@@ -139,7 +139,6 @@ class Migration(migrations.Migration):),),('replace', models.BooleanField(default=False)),- ('code', models.CharField(max_length=100)),('set_config',models.ForeignKey(@@ -267,7 +266,6 @@ class Migration(migrations.Migration):),),('replace', models.BooleanField(default=False)),- ('code', models.CharField(max_length=100)),('set_config',models.ForeignKey(@@ -325,7 +323,6 @@ class Migration(migrations.Migration):),),('replace', models.BooleanField(default=False)),- ('code', models.CharField(max_length=100)),('set_config',models.ForeignKey(@@ -383,7 +380,6 @@ class Migration(migrations.Migration):),),('replace', models.BooleanField(default=False)),- ('code', models.CharField(max_length=100)),('set_config',models.ForeignKey(diff --git a/wger/manager/migrations/0021_rename_code_repsconfig_class_name_and_more.py b/wger/manager/migrations/0021_rename_code_repsconfig_class_name_and_more.pynew file mode 100644index 000000000..157829d11--- /dev/null+++ b/wger/manager/migrations/0021_rename_code_repsconfig_class_name_and_more.py@@ -0,0 +1,17 @@+# Generated by Django 4.2.6 on 2024-03-10 13:42++from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ('manager', '0020_workoutlog_iteration_alter_setconfig_comment_and_more'),+ ]++ operations = [+ migrations.AddField(+ model_name='setconfig',+ name='class_name',+ field=models.CharField(blank=True, max_length=100, null=True),+ ),+ ]diff --git a/wger/manager/models/abstract_config.py b/wger/manager/models/abstract_config.pyindex a5e3d1f85..7cd2dda00 100644--- a/wger/manager/models/abstract_config.py+++ b/wger/manager/models/abstract_config.py@@ -101,12 +101,3 @@ class AbstractChangeConfig(models.Model):Only apply the change if the user logged the last weight, otherwiseapply the rules anyway"""-- code = models.CharField(- max_length=100,- )- """- The name of a python class that will take care of the change logic.-- If this is set, all other settings will be ignored.- """diff --git a/wger/manager/models/routine.py b/wger/manager/models/routine.pyindex d3f2ce6f8..8ea9ad212 100644--- a/wger/manager/models/routine.py+++ b/wger/manager/models/routine.py@@ -26,7 +26,7 @@ from django.urls import reversefrom django.utils.translation import gettext_lazy as _# wger-from wger.manager.dataclasses import WorkoutDay+from wger.manager.dataclasses import WorkoutDayDataclass Routine(models.Model):@@ -92,7 +92,7 @@ class Routine(models.Model):if self.name:return self.nameelse:- return f'{_("Routine")} ({self.creation_date})'+ return f'Routine {self.id} - created on {self.creation_date}'def get_owner_object(self):"""@@ -140,8 +140,7 @@ class Routine(models.Model):while current_date <= self.end:counter[current_day] += 1- out[current_date] = WorkoutDay(day=current_day, iteration=counter[current_day])- # out[current_date] = current_day+ out[current_date] = WorkoutDayData(day=current_day, iteration=counter[current_day])if current_day.can_proceed(current_date):current_day = current_day.next_daydiff --git a/wger/manager/models/set_config.py b/wger/manager/models/set_config.pyindex 0381f1773..8f44aeb85 100644--- a/wger/manager/models/set_config.py+++ b/wger/manager/models/set_config.py@@ -13,6 +13,9 @@## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.+# Standard Library+import importlib+from typing import Optional# Djangofrom django.db import models@@ -76,6 +79,17 @@ class SetConfig(models.Model):blank=True,)+ class_name = models.CharField(+ max_length=100,+ null=True,+ blank=True,+ )+ """+ The name of a python class that will take care of the change logic.++ If this is set, all other settings will be ignored.+ """+# Metaclass to set some other propertiesclass Meta:ordering = [@@ -107,6 +121,24 @@ class SetConfig(models.Model):return outdef get_config(self, iteration: int):+ # If there is a custom class set, pass all responsibilities to it+ if self.class_name:+ try:+ module = importlib.import_module(+ f'wger.manager.config_calculations.{self.class_name}'+ )+ except ImportError:+ raise ImportError(f'Class {self.class_name} not found')+ custom_logic = module.SetCalculations(+ iteration=iteration,+ weight_configs=self.weightconfig_set.filter(iteration__lte=iteration),+ reps_configs=self.repsconfig_set.filter(iteration__lte=iteration),+ rir_configs=self.rirconfig_set.filter(iteration__lte=iteration),+ rest_configs=self.restconfig_set.filter(iteration__lte=iteration),+ )++ return custom_logic.calculate()+max_iter_weight = 1max_iter_reps = 1@@ -114,7 +146,6 @@ class SetConfig(models.Model):reps = self.get_reps(max_iter_reps)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 iteration@@ -132,7 +163,6 @@ class SetConfig(models.Model):# proceed. Otherwise, the weight won't changeif log_data.exists():for log in log_data:-if log.weight >= weight and log.reps >= reps:max_iter_weight = imax_iter_reps = idiff --git a/wger/manager/tests/test_routine.py b/wger/manager/tests/test_routine.pyindex c2cd996ea..f94386c63 100644--- a/wger/manager/tests/test_routine.py+++ b/wger/manager/tests/test_routine.py@@ -17,7 +17,7 @@ import datetime# wgerfrom wger.core.tests.base_testcase import WgerTestCase-from wger.manager.dataclasses import WorkoutDay+from wger.manager.dataclasses import WorkoutDayDatafrom wger.manager.models import (DayNg,WorkoutSession,@@ -85,16 +85,16 @@ class RoutineTestCase(WgerTestCase):self.assertEqual(self.routine.date_sequence,{- datetime.date(2024, 1, 1): WorkoutDay(day=self.day1, iteration=1),- datetime.date(2024, 1, 2): WorkoutDay(day=self.day2, iteration=1),- datetime.date(2024, 1, 3): WorkoutDay(day=self.day3, iteration=1),- datetime.date(2024, 1, 4): WorkoutDay(day=self.day1, iteration=2),- datetime.date(2024, 1, 5): WorkoutDay(day=self.day2, iteration=2),- datetime.date(2024, 1, 6): WorkoutDay(day=self.day3, iteration=2),- datetime.date(2024, 1, 7): WorkoutDay(day=self.day1, iteration=3),- datetime.date(2024, 1, 8): WorkoutDay(day=self.day2, iteration=3),- datetime.date(2024, 1, 9): WorkoutDay(day=self.day3, iteration=3),- datetime.date(2024, 1, 10): WorkoutDay(day=self.day1, iteration=4),+ datetime.date(2024, 1, 1): WorkoutDayData(day=self.day1, iteration=1),+ datetime.date(2024, 1, 2): WorkoutDayData(day=self.day2, iteration=1),+ datetime.date(2024, 1, 3): WorkoutDayData(day=self.day3, iteration=1),+ datetime.date(2024, 1, 4): WorkoutDayData(day=self.day1, iteration=2),+ datetime.date(2024, 1, 5): WorkoutDayData(day=self.day2, iteration=2),+ datetime.date(2024, 1, 6): WorkoutDayData(day=self.day3, iteration=2),+ datetime.date(2024, 1, 7): WorkoutDayData(day=self.day1, iteration=3),+ datetime.date(2024, 1, 8): WorkoutDayData(day=self.day2, iteration=3),+ datetime.date(2024, 1, 9): WorkoutDayData(day=self.day3, iteration=3),+ datetime.date(2024, 1, 10): WorkoutDayData(day=self.day1, iteration=4),},)@@ -127,15 +127,15 @@ class RoutineTestCase(WgerTestCase):self.assertEqual(self.routine.date_sequence,{- datetime.date(2024, 1, 1): WorkoutDay(day=self.day1, iteration=1),- datetime.date(2024, 1, 2): WorkoutDay(day=self.day1, iteration=2),- datetime.date(2024, 1, 3): WorkoutDay(day=self.day1, iteration=3),- datetime.date(2024, 1, 4): WorkoutDay(day=self.day1, iteration=4),- datetime.date(2024, 1, 5): WorkoutDay(day=self.day2, iteration=1),- datetime.date(2024, 1, 6): WorkoutDay(day=self.day3, iteration=1),- datetime.date(2024, 1, 7): WorkoutDay(day=self.day1, iteration=5),- datetime.date(2024, 1, 8): WorkoutDay(day=self.day2, iteration=2),- datetime.date(2024, 1, 9): WorkoutDay(day=self.day3, iteration=2),- datetime.date(2024, 1, 10): WorkoutDay(day=self.day1, iteration=6),+ datetime.date(2024, 1, 1): WorkoutDayData(day=self.day1, iteration=1),+ datetime.date(2024, 1, 2): WorkoutDayData(day=self.day1, iteration=2),+ datetime.date(2024, 1, 3): WorkoutDayData(day=self.day1, iteration=3),+ datetime.date(2024, 1, 4): WorkoutDayData(day=self.day1, iteration=4),+ datetime.date(2024, 1, 5): WorkoutDayData(day=self.day2, iteration=1),+ datetime.date(2024, 1, 6): WorkoutDayData(day=self.day3, iteration=1),+ datetime.date(2024, 1, 7): WorkoutDayData(day=self.day1, iteration=5),+ datetime.date(2024, 1, 8): WorkoutDayData(day=self.day2, iteration=2),+ datetime.date(2024, 1, 9): WorkoutDayData(day=self.day3, iteration=2),+ datetime.date(2024, 1, 10): WorkoutDayData(day=self.day1, iteration=6),},)diff --git a/wger/manager/tests/test_set_config.py b/wger/manager/tests/test_set_config.pyindex b380a44b0..9831b3643 100644--- a/wger/manager/tests/test_set_config.py+++ b/wger/manager/tests/test_set_config.py@@ -156,3 +156,34 @@ class SetConfigTestCase(WgerTestCase):self.set_config.get_config(5),SetConfigData(weight=42, reps=5, rir=2, rest=120),)+ self.assertEqual(+ self.set_config.get_config(6),+ SetConfigData(weight=42, reps=5, rir=2, rest=120),+ )++ def test_weight_config_custom_python_class(self):+ """+ Test that the weight is correctly calculated for each step / iteration+ if there is custom python code defined+ """++ # Initial value with custom python code+ self.set_config.class_name = 'dummy'+ self.set_config.save()+ WeightConfig(set_config=self.set_config, iteration=1, value=100, replace=True).save()+ RepsConfig(set_config=self.set_config, iteration=1, value=5).save()+ RestConfig(set_config=self.set_config, iteration=1, value=120).save()+ RiRConfig(set_config=self.set_config, iteration=1, value=2).save()++ self.assertEqual(+ self.set_config.get_config(1),+ SetConfigData(weight=24, reps=1, rir=2, rest=120),+ )+ self.assertEqual(+ self.set_config.get_config(2),+ SetConfigData(weight=42, reps=10, rir=1, rest=90),+ )+ self.assertEqual(+ self.set_config.get_config(3),+ SetConfigData(weight=42, reps=10, rir=1, rest=90),+ )