Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Cut the 1RM days in the owner's timezone

Roland Geidere80dbc37af59
diff --git a/wger/measurements/dynamic/types.py b/wger/measurements/dynamic/types.py
index b5917ed6d..e9814a740 100644
--- a/wger/measurements/dynamic/types.py
+++ b/wger/measurements/dynamic/types.py
@@ -20,6 +20,7 @@ from decimal import Decimal
# Django
from django.core.exceptions import ValidationError as DjangoValidationError
+from django.utils import timezone
# wger
from wger.core.models import UserProfile
@@ -200,9 +201,9 @@ DEFAULT_WINDOW_DAYS = 30
MAX_REPS_SCHEMA = {'type': 'integer', 'minimum': 1, 'maximum': 10}
-def daily_best_estimates(user_id: int, exercise_id, max_reps) -> dict:
+def daily_best_estimates(user_id: int, exercise_id, max_reps, tz: datetime.tzinfo) -> dict:
"""
- Per calendar day (UTC) the best Brzycki estimate among the qualifying
+ Per calendar day in ``tz`` the best Brzycki estimate among the qualifying
sets of the exercise, as day -> (value in kg, datetime of the set).
Qualifying means: 1 to max_reps repetitions counted in repetitions, a
@@ -229,7 +230,7 @@ def daily_best_estimates(user_id: int, exercise_id, max_reps) -> dict:
else AbstractWeight(log.weight, 'lb').kg
)
one_rm = brzycki_one_rm(weight, log.repetitions).quantize(TWOPLACES)
- day = log.date.date()
+ day = timezone.localdate(log.date, tz)
if day not in best or (one_rm, log.date) > best[day]:
best[day] = (one_rm, log.date)
return best
@@ -238,8 +239,8 @@ def daily_best_estimates(user_id: int, exercise_id, max_reps) -> dict:
@register
class OneRepMax(DynamicMeasurementType):
"""
- One entry per calendar day (UTC) with qualifying logs of the configured
- exercise: the highest Brzycki estimate of that day, in kg
+ One entry per calendar day in the owner's timezone with qualifying logs
+ of the configured exercise: the highest Brzycki estimate of that day, in kg
"""
slug = Category.DynamicType.ONE_REP_MAX
@@ -267,6 +268,7 @@ class OneRepMax(DynamicMeasurementType):
category.user_id,
params.get('exercise_id'),
params.get('max_reps', DEFAULT_MAX_REPS),
+ category.user.userprofile.zone_info,
)
return [
@@ -323,9 +325,10 @@ class OneRmTotal(DynamicMeasurementType):
params = category.dynamic_params
max_reps = params.get('max_reps', DEFAULT_MAX_REPS)
window = datetime.timedelta(days=params.get('window_days', DEFAULT_WINDOW_DAYS))
+ tz = category.user.userprofile.zone_info
per_exercise = [
- daily_best_estimates(category.user_id, exercise_id, max_reps)
+ daily_best_estimates(category.user_id, exercise_id, max_reps, tz)
for exercise_id in params.get('exercise_ids', [])
]
if not per_exercise:
diff --git a/wger/measurements/tests/test_dynamic.py b/wger/measurements/tests/test_dynamic.py
index 9c6539203..182084765 100644
--- a/wger/measurements/tests/test_dynamic.py
+++ b/wger/measurements/tests/test_dynamic.py
@@ -85,6 +85,7 @@ class DynamicMeasurementTestCase(WgerTestCase):
days_ago: int = 0,
weight_unit: int = 1,
exercise_id: int = 1,
+ date: datetime.datetime = None,
) -> WorkoutLog:
with self.captureOnCommitCallbacks(execute=True):
log = WorkoutLog(
@@ -93,7 +94,7 @@ class DynamicMeasurementTestCase(WgerTestCase):
weight=Decimal(weight),
repetitions=Decimal(reps),
weight_unit_id=weight_unit,
- date=timezone.now() - datetime.timedelta(days=days_ago),
+ date=date or timezone.now() - datetime.timedelta(days=days_ago),
)
log.save()
return log
@@ -1052,6 +1053,27 @@ class OneRepMaxTestCase(DynamicMeasurementTestCase):
self.assertEqual(rows[0].value, Decimal('84.71'))
self.assertEqual(rows[1].value, Decimal('112.51'))
+ def test_days_are_cut_in_the_owners_timezone(self):
+ """
+ Two logs on one UTC day land on two days when the owner's midnight
+ lies between them
+ """
+ profile = self.user.userprofile
+ profile.time_zone = 'Pacific/Auckland'
+ profile.save()
+
+ # Auckland midnight (UTC+12 in August) sits at 12:00 UTC
+ day = datetime.datetime(2026, 8, 10, tzinfo=datetime.timezone.utc)
+ self.make_log('100', 1, date=day.replace(hour=11, minute=30))
+ self.make_log('80', 1, date=day.replace(hour=12, minute=30))
+
+ category = self.enable_one_rm()
+ rows = self.calculated_rows(category)
+
+ self.assertEqual(rows.count(), 2)
+ self.assertEqual(rows[0].value, Decimal('100.00'))
+ self.assertEqual(rows[1].value, Decimal('80.00'))
+
def test_high_rep_sets_are_ignored(self):
"""
Sets over max_reps do not enter the estimate