Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix type error in rules handling

Roland Geiderb24d2d3701ac
diff --git a/wger/manager/models/slot_entry.py b/wger/manager/models/slot_entry.py
index 711008f3e..362a061dd 100644
--- a/wger/manager/models/slot_entry.py
+++ b/wger/manager/models/slot_entry.py
@@ -1,5 +1,5 @@
# This file is part of wger Workout Manager <https://github.com/wger-project>.
-# Copyright (C) 2013 - 2021 wger Team
+# Copyright (C) 2013 - 2026 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
@@ -21,6 +21,7 @@ import importlib
import logging
from decimal import Decimal
from typing import (
+ Callable,
Dict,
List,
)
@@ -44,6 +45,7 @@ from wger.manager.dataclasses import (
SetConfigData,
round_value,
)
+from wger.manager.models import WorkoutLog
from wger.manager.models.abstract_config import (
AbstractChangeConfig,
OperationChoices,
@@ -376,6 +378,19 @@ class SlotEntry(models.Model):
'max_sets': 1,
}
+ def _requirement_met(log: WorkoutLog, field_name: str) -> bool:
+ """Checks if the requirements for a single field are met for this log"""
+
+ log_value = getattr(log, field_name, None)
+ if log_value is None:
+ return False
+
+ min_value = min_values.get(field_name)
+ if min_value is None:
+ return False
+
+ return log_value >= min_value
+
for i in range(1, iteration + 1):
configs = self.load_all_configs(i)['last']
@@ -391,23 +406,40 @@ class SlotEntry(models.Model):
if not requirements:
max_iterations[field] = i
- # logger.debug(f'No requirements for {field} in iteration {i}')
continue
+ # Precompute threshold values for all required fields once per iteration
+ # to avoid recalculating them for every log entry.
+ min_values: Dict[str, Decimal | None] = {}
+ for req_field in requirements.rules:
+ calc_fn: Callable[[int], Decimal | None] | None = getattr(
+ self, f'calculate_{req_field}', None
+ )
+ if not callable(calc_fn):
+ logger.error(
+ f'Missing method calculate_{req_field} on SlotEntry {self.id}',
+ )
+ min_values[req_field] = None
+ continue
+
+ try:
+ min_values[req_field] = calc_fn(max_iterations[req_field])
+ except Exception as e:
+ logger.error(
+ f'Error during calculate_{req_field} for SlotEntry {self.id}: {e}',
+ )
+ min_values[req_field] = None
+
# Field has requirements, check if they are met
# logger.debug(f'Requirements for {field} in iteration {i}: {requirements.rules}')
for log in log_data:
+ # If any log satisfies all required fields for the config, the field is ready
all_fields_met = all(
- getattr(log, req_field) is not None
- and getattr(log, req_field)
- >= getattr(self, f'calculate_{req_field}')(max_iterations[req_field])
- for req_field in requirements.rules
+ _requirement_met(log, req_field) for req_field in requirements.rules
)
- # logger.debug(f'all_fields_met for {field} in iteration {i}: {all_fields_met}')
-
if all_fields_met:
max_iterations[field] = i
- continue
+ break
sets = self.calculate_sets(max_iterations['sets'])
max_sets = self.calculate_maxsets(max_iterations['max_sets'])
@@ -467,8 +499,6 @@ class SlotEntry(models.Model):
# Note: don't rename these methods, they are accessed in get_config via getattr
#
def calculate_sets(self, iteration: int) -> Decimal | None:
- # logger.debug(f'calculate_sets for slot entry {self.id} and iteration {iteration}')
-
return self.calculate_config_value(
self.duplicate_configs(
iteration,
diff --git a/wger/manager/tests/test_slot_entry.py b/wger/manager/tests/test_slot_entry.py
index c305af87f..e7edef4c9 100644
--- a/wger/manager/tests/test_slot_entry.py
+++ b/wger/manager/tests/test_slot_entry.py
@@ -481,6 +481,53 @@ class SlotEntryTestCase(WgerTestCase):
),
)
+ def test_requirements_sets_null_values(self):
+ """
+ Test that the sets are correctly calculated if there are requirements but
+ some values are null (e.g. there is a rule to check for RiR but there is no
+ RiR config)
+ """
+
+ self.slot_entry.weight_rounding = 2.5
+ self.slot_entry.repetition_rounding = 2
+ self.slot_entry.save()
+
+ # Initial values
+ SetsConfig(slot_entry=self.slot_entry, iteration=1, value=4).save()
+ RepetitionsConfig(slot_entry=self.slot_entry, iteration=1, value=5).save()
+ WeightConfig(
+ slot_entry=self.slot_entry,
+ iteration=1,
+ value=80,
+ ).save()
+
+ # Increase weight by 2.5 at iteration 2, depends on RiR
+ WeightConfig(
+ slot_entry=self.slot_entry,
+ iteration=2,
+ value=2.5,
+ operation=OperationChoices.PLUS,
+ step=StepChoices.ABSOLUTE,
+ requirements={'rules': ['rir']},
+ ).save()
+
+ # Logs
+ WorkoutLog(
+ exercise_id=1,
+ user_id=1,
+ routine_id=1,
+ slot_entry=self.slot_entry,
+ iteration=1,
+ weight=None,
+ rest=80,
+ repetitions=4,
+ rir=2,
+ ).save()
+
+ config_data = self.slot_entry.get_config_data(2)
+ self.assertEqual(config_data.rir, None)
+ self.assertEqual(config_data.weight, 80)
+
def test_weight_config_with_logs_and_range(self):
"""
Test that the weight is correctly calculated for each step / iteration