Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Resolve the legacy session triple in the owner's timezone

Roland Geider4ce540e3d3e1
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py
index 99e92950a..5f77eb33b 100644
--- a/wger/manager/api/serializers.py
+++ b/wger/manager/api/serializers.py
@@ -24,6 +24,7 @@ from django.utils import timezone
from rest_framework import serializers
# wger
+from wger.core.models import UserProfile
from wger.manager.api.consts import BASE_CONFIG_FIELDS
from wger.manager.api.fields import DecimalOrIntegerField
from wger.manager.api.validators import validate_requirements
@@ -481,42 +482,58 @@ class WorkoutSessionSerializer(serializers.ModelSerializer):
Only components the request did not send are taken from the instance, so
a PATCH that just sets time_end keeps the day and start time it had. Values
- sent in the new format always win.
+ sent in the new format always win. The wall times resolve in the owner's
+ zone: a queued 07:00 is the user's 07:00, whatever zone the request
+ happens to run in.
"""
if not any(key in data for key in self.LEGACY_FIELDS):
return data
+ tz = self._owner_zone()
data = data.copy()
date = self._legacy_value('date', data, serializers.DateField)
time_start = self._legacy_value('time_start', data, serializers.TimeField)
time_end = self._legacy_value('time_end', data, serializers.TimeField)
local_start = (
- timezone.localtime(self.instance.datetime_start)
+ timezone.localtime(self.instance.datetime_start, tz)
if self.instance and self.instance.datetime_start
else None
)
if date is None:
- date = local_start.date() if local_start else timezone.localdate()
+ date = local_start.date() if local_start else timezone.localdate(timezone=tz)
if 'time_start' not in data:
time_start = local_start.time() if local_start else datetime.time()
if 'time_end' not in data and self.instance and self.instance.datetime_end:
- time_end = timezone.localtime(self.instance.datetime_end).time()
+ time_end = timezone.localtime(self.instance.datetime_end, tz).time()
if 'datetime_start' not in data:
data['datetime_start'] = timezone.make_aware(
- datetime.datetime.combine(date, time_start or datetime.time())
+ datetime.datetime.combine(date, time_start or datetime.time()), tz
)
if 'datetime_end' not in data:
end = None
if time_end:
- end = timezone.make_aware(datetime.datetime.combine(date, time_end))
+ end = timezone.make_aware(datetime.datetime.combine(date, time_end), tz)
if time_start and time_end < time_start:
end += datetime.timedelta(days=1)
data['datetime_end'] = end
return data
+ def _owner_zone(self):
+ """The owner's timezone, falling back to the active one when unknown"""
+ if self.instance:
+ return self.instance.user.userprofile.zone_info
+ user_id = self.context.get('user_id')
+ if user_id is None:
+ request = self.context.get('request')
+ if request is not None and request.user.is_authenticated:
+ user_id = request.user.pk
+ if user_id is None:
+ return timezone.get_current_timezone()
+ return UserProfile.objects.get(user_id=user_id).zone_info
+
@staticmethod
def _legacy_value(key, data, field_class):
"""Parse one deprecated value, reporting errors under its own key"""
diff --git a/wger/manager/powersync.py b/wger/manager/powersync.py
index b72c9c52d..5fb83ad98 100644
--- a/wger/manager/powersync.py
+++ b/wger/manager/powersync.py
@@ -53,6 +53,8 @@ class WorkoutSessionHandler(PowerSyncHandler):
model = WorkoutSession
serializer_class = WorkoutSessionSerializer
viewset_class = WorkoutSessionViewSet
+ # The legacy-triple shim resolves its wall times in the owner's zone
+ pass_user_id_in_context = True
@register_handler
diff --git a/wger/manager/tests/test_powersync.py b/wger/manager/tests/test_powersync.py
index 1a16b140c..81cc381a8 100644
--- a/wger/manager/tests/test_powersync.py
+++ b/wger/manager/tests/test_powersync.py
@@ -14,6 +14,7 @@
# Standard Library
import datetime
+import zoneinfo
# Django
from django.utils import timezone
@@ -22,6 +23,7 @@ from django.utils import timezone
from rest_framework import status
# wger
+from wger.core.models import UserProfile
from wger.core.tests import powersync_base_test
from wger.manager.models import (
Routine,
@@ -130,6 +132,32 @@ class WorkoutSessionPowerSyncTestCase(powersync_base_test.PowerSyncResourceTestC
timezone.make_aware(datetime.datetime(2030, 1, 16, 1, 30)),
)
+ def test_deprecated_fields_resolve_in_the_owners_timezone(self):
+ """The queued wall time is the user's, not the zone the request runs in"""
+
+ profile = UserProfile.objects.get(user__username=self.user_access)
+ profile.time_zone = 'Pacific/Auckland'
+ profile.save()
+
+ self.authenticate()
+ response = self.push(
+ 'PUT',
+ {
+ 'id': 'bbbbbbbb-bbbb-bbbb-bbbb-000000000097',
+ 'date': '2030-01-15',
+ 'time_start': '07:00:00',
+ 'routine': ROUTINE_OWNED,
+ 'impression': '2',
+ },
+ )
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+ session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000097')
+ self.assertEqual(
+ session.datetime_start,
+ datetime.datetime(2030, 1, 15, 7, 0, tzinfo=zoneinfo.ZoneInfo('Pacific/Auckland')),
+ )
+
class RoutinePowerSyncTestCase(
powersync_base_test.PowerSyncBaseTestCase,
diff --git a/wger/manager/tests/test_workout_session.py b/wger/manager/tests/test_workout_session.py
index 859952bbf..c679b4cfb 100644
--- a/wger/manager/tests/test_workout_session.py
+++ b/wger/manager/tests/test_workout_session.py
@@ -262,6 +262,31 @@ class WorkoutSessionLegacyFieldsTestCase(WgerTestCase):
timezone.make_aware(datetime.datetime(2025, 3, 12, 11, 30)),
)
+ def test_the_triple_is_composed_in_the_owners_timezone(self):
+ """A queued 07:00 is the owner's 07:00, not the zone the request runs in"""
+
+ profile = User.objects.get(username='test').userprofile
+ profile.time_zone = 'Pacific/Auckland'
+ profile.save()
+
+ response = self.client.post(
+ reverse('workoutsession-list'),
+ data={
+ 'routine': self.ROUTINE,
+ 'impression': '2',
+ 'date': '2025-03-12',
+ 'time_start': '07:00',
+ },
+ content_type='application/json',
+ )
+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)
+
+ session = WorkoutSession.objects.get(pk=response.json()['id'])
+ self.assertEqual(
+ session.datetime_start,
+ datetime.datetime(2025, 3, 12, 7, 0, tzinfo=zoneinfo.ZoneInfo('Pacific/Auckland')),
+ )
+
def test_create_over_midnight_ends_on_the_next_day(self):
"""An end time before the start time means the session ended the day after"""