Skip to content

Commit bbc6817

Browse files
committed
Merge branch 'fix/remove-api-workaounds'
2 parents aba15ef + a720221 commit bbc6817

6 files changed

Lines changed: 6 additions & 499 deletions

File tree

‎wger/manager/api/serializers.py‎

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
# You should have received a copy of the GNU Affero General Public License
1414
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
1515

16-
# Standard Library
17-
import datetime
18-
1916
# Django
2017
from django.core.exceptions import ValidationError as DjangoValidationError
2118
from django.utils import timezone
@@ -24,7 +21,6 @@
2421
from rest_framework import serializers
2522

2623
# wger
27-
from wger.core.models import UserProfile
2824
from wger.manager.api.consts import BASE_CONFIG_FIELDS
2925
from wger.manager.api.fields import DecimalOrIntegerField
3026
from wger.manager.api.validators import validate_requirements
@@ -436,11 +432,6 @@ class WorkoutSessionSerializer(serializers.ModelSerializer):
436432
Workout session serializer
437433
"""
438434

439-
# Write-only compatibility with the pre-2.7 API, remove in 2.8. The app
440-
# queues its offline writes with the column names it had at the time, so
441-
# uploads written before the update still arrive in the old shape.
442-
LEGACY_FIELDS = ('date', 'time_start', 'time_end')
443-
444435
class Meta:
445436
model = WorkoutSession
446437
fields = (
@@ -478,79 +469,6 @@ def validate(self, attrs):
478469

479470
return attrs
480471

481-
def to_internal_value(self, data):
482-
return super().to_internal_value(self._translate_legacy_input(data))
483-
484-
def _translate_legacy_input(self, data):
485-
"""
486-
Compose datetime_start/datetime_end out of the deprecated triple
487-
488-
Only components the request did not send are taken from the instance, so
489-
a PATCH that just sets time_end keeps the day and start time it had. Values
490-
sent in the new format always win. The wall times resolve in the owner's
491-
zone: a queued 07:00 is the user's 07:00, whatever zone the request
492-
happens to run in.
493-
"""
494-
if not any(key in data for key in self.LEGACY_FIELDS):
495-
return data
496-
497-
tz = self._owner_zone()
498-
data = data.copy()
499-
date = self._legacy_value('date', data, serializers.DateField)
500-
time_start = self._legacy_value('time_start', data, serializers.TimeField)
501-
time_end = self._legacy_value('time_end', data, serializers.TimeField)
502-
503-
local_start = (
504-
timezone.localtime(self.instance.datetime_start, tz)
505-
if self.instance and self.instance.datetime_start
506-
else None
507-
)
508-
if date is None:
509-
date = local_start.date() if local_start else timezone.localdate(timezone=tz)
510-
if 'time_start' not in data:
511-
time_start = local_start.time() if local_start else datetime.time()
512-
if 'time_end' not in data and self.instance and self.instance.datetime_end:
513-
time_end = timezone.localtime(self.instance.datetime_end, tz).time()
514-
515-
if 'datetime_start' not in data:
516-
data['datetime_start'] = timezone.make_aware(
517-
datetime.datetime.combine(date, time_start or datetime.time()), tz
518-
)
519-
if 'datetime_end' not in data:
520-
end = None
521-
if time_end:
522-
end = timezone.make_aware(datetime.datetime.combine(date, time_end), tz)
523-
if time_start and time_end < time_start:
524-
end += datetime.timedelta(days=1)
525-
data['datetime_end'] = end
526-
527-
return data
528-
529-
def _owner_zone(self):
530-
"""The owner's timezone, falling back to the active one when unknown"""
531-
if self.instance:
532-
return self.instance.user.userprofile.zone_info
533-
user_id = self.context.get('user_id')
534-
if user_id is None:
535-
request = self.context.get('request')
536-
if request is not None and request.user.is_authenticated:
537-
user_id = request.user.pk
538-
if user_id is None:
539-
return timezone.get_current_timezone()
540-
return UserProfile.objects.get(user_id=user_id).zone_info
541-
542-
@staticmethod
543-
def _legacy_value(key, data, field_class):
544-
"""Parse one deprecated value, reporting errors under its own key"""
545-
546-
if data.get(key) in (None, ''):
547-
return None
548-
549-
try:
550-
return field_class().to_internal_value(data[key])
551-
except serializers.ValidationError as e:
552-
raise serializers.ValidationError({key: e.detail})
553-
554472

555473
class OwnerScopedSessionField(serializers.PrimaryKeyRelatedField):
556474
"""

‎wger/manager/models/log.py‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -346,23 +346,3 @@ def save(self, *args, **kwargs):
346346

347347
# Save to db
348348
super().save(*args, **kwargs)
349-
350-
# The app creates its sessions itself and can leave the day empty, which
351-
# hides them from days with need_logs_to_advance. Only filled in when all
352-
# logs of the session agree on one day, like migration 0028 does: a wrong
353-
# day would open the gate on a day the user never trained.
354-
#
355-
# Can be removed once the app versions that set the day are widely adopted.
356-
if self.session_id and self.slot_entry and self.session.day_id is None:
357-
days = list(
358-
WorkoutLog.objects.filter(
359-
session_id=self.session_id,
360-
slot_entry__isnull=False,
361-
)
362-
.values_list('slot_entry__slot__day_id', 'slot_entry__slot__day__routine_id')
363-
.distinct()
364-
)
365-
366-
if len(days) == 1 and days[0][1] == self.session.routine_id:
367-
self.session.day_id = days[0][0]
368-
self.session.save(update_fields=['day'])

‎wger/manager/powersync.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ class WorkoutSessionHandler(PowerSyncHandler):
5353
model = WorkoutSession
5454
serializer_class = WorkoutSessionSerializer
5555
viewset_class = WorkoutSessionViewSet
56-
# The legacy-triple shim resolves its wall times in the owner's zone
57-
pass_user_id_in_context = True
5856

5957

6058
@register_handler

‎wger/manager/tests/test_log_model.py‎

Lines changed: 0 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
# wger
2424
from wger.core.tests.base_testcase import WgerTestCase
2525
from wger.manager.models import (
26-
Slot,
27-
SlotEntry,
2826
WorkoutLog,
2927
WorkoutSession,
3028
)
@@ -152,27 +150,6 @@ def test_create_session(self):
152150

153151
self.assertEqual(WorkoutSession.objects.count(), 1)
154152

155-
def test_create_session_sets_day(self):
156-
"""
157-
The auto-created session inherits the day of the log's slot entry, so
158-
that need_logs_to_advance days can find it
159-
"""
160-
161-
WorkoutLog.objects.all().delete()
162-
WorkoutSession.objects.all().delete()
163-
164-
WorkoutLog(
165-
user_id=1,
166-
exercise_id=1,
167-
routine_id=1,
168-
slot_entry_id=1,
169-
weight=10,
170-
repetitions=10,
171-
).save()
172-
173-
session = WorkoutSession.objects.get()
174-
self.assertEqual(session.day_id, 1)
175-
176153
def test_create_session_without_slot_entry_has_no_day(self):
177154
"""
178155
A free log without a slot entry can't know its day
@@ -191,28 +168,6 @@ def test_create_session_without_slot_entry_has_no_day(self):
191168
session = WorkoutSession.objects.get()
192169
self.assertIsNone(session.day_id)
193170

194-
def test_fills_missing_day_on_a_client_created_session(self):
195-
"""
196-
A session the client created without a day gets it from the log, so that
197-
need_logs_to_advance days can find it
198-
"""
199-
200-
session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000001')
201-
self.assertIsNone(session.day_id)
202-
203-
WorkoutLog(
204-
user_id=1,
205-
exercise_id=1,
206-
routine_id=1,
207-
slot_entry_id=1,
208-
session=session,
209-
weight=10,
210-
repetitions=10,
211-
).save()
212-
213-
session.refresh_from_db()
214-
self.assertEqual(session.day_id, 1)
215-
216171
def test_does_not_overwrite_the_day_of_a_session(self):
217172
"""
218173
A session that already has a day keeps it, even when the log points
@@ -236,112 +191,6 @@ def test_does_not_overwrite_the_day_of_a_session(self):
236191
session.refresh_from_db()
237192
self.assertEqual(session.day_id, 3)
238193

239-
def test_does_not_fill_the_day_when_the_logs_span_several_days(self):
240-
"""
241-
A session whose logs come from more than one day stays without one, since
242-
no single day is the right answer
243-
"""
244-
245-
session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000001')
246-
self.assertIsNone(session.day_id)
247-
248-
# A second entry, on another day of the same routine
249-
other_slot = Slot.objects.create(day_id=3, order=1)
250-
other_entry = SlotEntry.objects.create(slot=other_slot, exercise_id=1, order=1)
251-
252-
WorkoutLog(
253-
user_id=1,
254-
exercise_id=1,
255-
routine_id=1,
256-
slot_entry_id=1,
257-
session=session,
258-
weight=10,
259-
repetitions=10,
260-
).save()
261-
262-
session.refresh_from_db()
263-
self.assertEqual(session.day_id, 1)
264-
265-
# The day of the first log is no longer the whole truth, but an already
266-
# set day is never taken away again
267-
WorkoutLog(
268-
user_id=1,
269-
exercise_id=1,
270-
routine_id=1,
271-
slot_entry=other_entry,
272-
session=session,
273-
weight=10,
274-
repetitions=10,
275-
).save()
276-
277-
session.refresh_from_db()
278-
self.assertEqual(session.day_id, 1)
279-
280-
def test_does_not_fill_the_day_when_earlier_logs_disagree(self):
281-
"""
282-
Logs that were already there decide as well, not just the one being saved
283-
"""
284-
285-
session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000001')
286-
other_slot = Slot.objects.create(day_id=3, order=1)
287-
other_entry = SlotEntry.objects.create(slot=other_slot, exercise_id=1, order=1)
288-
289-
# Two logs from different days, written without going through save()
290-
for slot_entry_id in (1, other_entry.pk):
291-
WorkoutLog.objects.bulk_create(
292-
[
293-
WorkoutLog(
294-
user_id=1,
295-
exercise_id=1,
296-
routine_id=1,
297-
slot_entry_id=slot_entry_id,
298-
session=session,
299-
weight=10,
300-
repetitions=10,
301-
)
302-
]
303-
)
304-
305-
session.refresh_from_db()
306-
self.assertIsNone(session.day_id)
307-
308-
WorkoutLog(
309-
user_id=1,
310-
exercise_id=1,
311-
routine_id=1,
312-
slot_entry_id=1,
313-
session=session,
314-
weight=20,
315-
repetitions=8,
316-
).save()
317-
318-
session.refresh_from_db()
319-
self.assertIsNone(session.day_id)
320-
321-
def test_does_not_fill_the_day_from_another_routine(self):
322-
"""
323-
The day of a slot entry from a different routine must not leak into the
324-
session
325-
"""
326-
327-
session = WorkoutSession.objects.get(pk='bbbbbbbb-bbbb-bbbb-bbbb-000000000003')
328-
self.assertIsNone(session.day_id)
329-
self.assertEqual(session.routine_id, 2)
330-
331-
# slot entry 1 lives in routine 1, the session in routine 2
332-
WorkoutLog(
333-
user_id=1,
334-
exercise_id=1,
335-
routine_id=1,
336-
slot_entry_id=1,
337-
session=session,
338-
weight=10,
339-
repetitions=10,
340-
).save()
341-
342-
session.refresh_from_db()
343-
self.assertIsNone(session.day_id)
344-
345194
def test_save_reuses_session_when_duplicates_exist(self):
346195
"""
347196
Duplicate routine-less sessions can exist for one (user, date) because the

0 commit comments

Comments
 (0)