Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Drop the read side of the old session date fields

Roland Geider86e8de3a0c30
diff --git a/wger/manager/api/filtersets.py b/wger/manager/api/filtersets.py
index 9fdce9a21..71be7bb92 100644
--- a/wger/manager/api/filtersets.py
+++ b/wger/manager/api/filtersets.py
@@ -24,9 +24,6 @@ from wger.manager.models import (
class WorkoutSessionFilterSet(filters.FilterSet):
- # Deprecated, remove in 2.8 together with the fields in WorkoutSessionSerializer
- date = filters.DateFilter(field_name='datetime_start', lookup_expr='date')
-
class Meta:
model = WorkoutSession
fields = {
diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py
index 9edf12a5e..99e92950a 100644
--- a/wger/manager/api/serializers.py
+++ b/wger/manager/api/serializers.py
@@ -430,46 +430,23 @@ class WorkoutSessionSerializer(serializers.ModelSerializer):
Workout session serializer
"""
- # Deprecated, remove in 2.8. Derived from datetime_start/datetime_end on read
- # and translated back on write, so that clients written against the pre-2.7
- # API keep working, including PowerSync uploads that were queued by an older
- # app version and only drain after the update.
+ # Write-only compatibility with the pre-2.7 API, remove in 2.8. The app
+ # queues its offline writes with the column names it had at the time, so
+ # uploads written before the update still arrive in the old shape.
LEGACY_FIELDS = ('date', 'time_start', 'time_end')
- date = serializers.SerializerMethodField()
- time_start = serializers.SerializerMethodField()
- time_end = serializers.SerializerMethodField()
-
class Meta:
model = WorkoutSession
fields = (
'id',
'routine',
'day',
- 'date',
'notes',
'impression',
- 'time_start',
- 'time_end',
'datetime_start',
'datetime_end',
)
- def get_date(self, obj: WorkoutSession) -> datetime.date | None:
- start = self._local_start(obj)
- return start.date() if start else None
-
- def get_time_start(self, obj: WorkoutSession) -> datetime.time | None:
- start = self._local_start(obj)
- return start.time() if start else None
-
- def get_time_end(self, obj: WorkoutSession) -> datetime.time | None:
- return timezone.localtime(obj.datetime_end).time() if obj.datetime_end else None
-
- @staticmethod
- def _local_start(obj: WorkoutSession) -> datetime.datetime | None:
- return timezone.localtime(obj.datetime_start) if obj.datetime_start else None
-
def validate(self, attrs):
"""
Run the model validation on the interval the request would end up with
@@ -514,7 +491,11 @@ class WorkoutSessionSerializer(serializers.ModelSerializer):
time_start = self._legacy_value('time_start', data, serializers.TimeField)
time_end = self._legacy_value('time_end', data, serializers.TimeField)
- local_start = self._local_start(self.instance) if self.instance else None
+ local_start = (
+ timezone.localtime(self.instance.datetime_start)
+ if self.instance and self.instance.datetime_start
+ else None
+ )
if date is None:
date = local_start.date() if local_start else timezone.localdate()
if 'time_start' not in data:
diff --git a/wger/manager/tests/test_workout_session.py b/wger/manager/tests/test_workout_session.py
index 4599e6a51..ea6191a82 100644
--- a/wger/manager/tests/test_workout_session.py
+++ b/wger/manager/tests/test_workout_session.py
@@ -100,9 +100,8 @@ class WorkoutSessionDurationTestCase(WgerTestCase):
url,
data={
'notes': 'Still editable',
- 'date': stored['date'],
- 'time_start': stored['time_start'],
- 'time_end': stored['time_end'],
+ 'datetime_start': stored['datetime_start'],
+ 'datetime_end': stored['datetime_end'],
},
content_type='application/json',
)
@@ -126,7 +125,7 @@ class WorkoutSessionDurationTestCase(WgerTestCase):
class WorkoutSessionLegacyFieldsTestCase(WgerTestCase):
"""
- Test the deprecated date/time_start/time_end fields of the session API
+ Test that writes in the pre-2.7 shape still arrive
"""
SESSION = 'bbbbbbbb-bbbb-bbbb-bbbb-000000000005'
@@ -136,18 +135,14 @@ class WorkoutSessionLegacyFieldsTestCase(WgerTestCase):
super().setUp()
self.user_login('test')
- def test_read_derives_the_deprecated_triple(self):
- """The deprecated fields are derived from the new ones, in local time"""
-
- session = WorkoutSession.objects.get(pk=self.SESSION)
- start = timezone.localtime(session.datetime_start)
- end = timezone.localtime(session.datetime_end)
+ def test_the_deprecated_fields_are_not_returned(self):
+ """They are accepted on write, but they are not part of the response"""
response = self.client.get(reverse('workoutsession-detail', kwargs={'pk': self.SESSION}))
- self.assertEqual(response.json()['date'], start.date().isoformat())
- self.assertEqual(response.json()['time_start'], start.time().isoformat())
- self.assertEqual(response.json()['time_end'], end.time().isoformat())
+ self.assertNotIn('date', response.json())
+ self.assertNotIn('time_start', response.json())
+ self.assertNotIn('time_end', response.json())
def test_create_with_the_deprecated_triple(self):
"""The deprecated fields are composed into datetime_start/datetime_end"""
@@ -261,18 +256,20 @@ class WorkoutSessionLegacyFieldsTestCase(WgerTestCase):
datetime.datetime(2025, 3, 12, 10, 0, tzinfo=datetime.timezone.utc),
)
- def test_filter_by_date(self):
- """The deprecated date filter matches on the local day of datetime_start"""
+ def test_filter_by_day(self):
+ """The viewset has a filterset, so a day can be selected without an error"""
session = WorkoutSession.objects.get(pk=self.SESSION)
day = timezone.localtime(session.datetime_start).date()
- response = self.client.get(reverse('workoutsession-list'), {'date': day.isoformat()})
+ response = self.client.get(
+ reverse('workoutsession-list'), {'datetime_start__date': day.isoformat()}
+ )
self.assertEqual([entry['id'] for entry in response.json()['results']], [self.SESSION])
response = self.client.get(
reverse('workoutsession-list'),
- {'date': (day + datetime.timedelta(days=1)).isoformat()},
+ {'datetime_start__date': (day + datetime.timedelta(days=1)).isoformat()},
)
self.assertEqual(response.json()['results'], [])