Self hosted FLOSS fitness/workout, nutrition and weight tracker
Derive the session times from the logs where possible
Roland Geider932ae1998489
diff --git a/wger/manager/migrations/0028_alter_workoutsession_options_and_more.py b/wger/manager/migrations/0028_alter_workoutsession_options_and_more.pyindex 6ffc93920..4f2a480e9 100644--- a/wger/manager/migrations/0028_alter_workoutsession_options_and_more.py+++ b/wger/manager/migrations/0028_alter_workoutsession_options_and_more.py@@ -1,26 +1,67 @@# Generated by Django 6.0.5 on 2026-07-06 12:51+import datetime+import django.utils.timezonefrom django.conf import settingsfrom django.db import migrations, models+from django.db.models import Max, Min+from django.utils.timezone import make_aware+++BATCH_SIZE = 2000+++def combine(date, time):+ return make_aware(datetime.datetime.combine(date, time))+++def build_interval(session):+ """+ Map the old date/time_start/time_end triple onto a start and end datetime+ """+ if session.time_start:+ start = combine(session.date, session.time_start)+ end = None+ if session.time_end:+ end = combine(session.date, session.time_end)+ # An end before the start means the session ran over midnight+ if session.time_end < session.time_start:+ end += datetime.timedelta(days=1)+ return start, end++ # An end without a start was never valid, but nothing enforced it+ if session.time_end:+ return combine(session.date, datetime.time(0, 0)), combine(session.date, session.time_end)++ # No times recorded: the logs know when it actually happened+ if session.first_log:+ last = session.last_log+ return session.first_log, last if last > session.first_log else None++ # Nothing to go by, keep the day and leave the session open+ return combine(session.date, datetime.time(0, 0)), Nonedef forward_func(apps, schema_editor):WorkoutSession = apps.get_model('manager', 'WorkoutSession')- import datetime- from django.utils.timezone import make_aware- for session in WorkoutSession.objects.all():- start_time = session.time_start if session.time_start else datetime.time(0, 0)- # Handle cases where date might be None, though default is today, just in case- date = session.date if session.date else datetime.date.today()- session.datetime_start = make_aware(datetime.datetime.combine(date, start_time))+ queryset = WorkoutSession.objects.annotate(+ first_log=Min('logs__date'),+ last_log=Max('logs__date'),+ ).order_by('pk')- if session.time_end:- session.datetime_end = make_aware(datetime.datetime.combine(date, session.time_end))- if session.time_end < start_time:- session.datetime_end += datetime.timedelta(days=1)- session.save()+ batch = []+ for session in queryset.iterator(chunk_size=BATCH_SIZE):+ session.datetime_start, session.datetime_end = build_interval(session)+ batch.append(session)++ if len(batch) >= BATCH_SIZE:+ WorkoutSession.objects.bulk_update(batch, ['datetime_start', 'datetime_end'])+ batch = []++ if batch:+ WorkoutSession.objects.bulk_update(batch, ['datetime_start', 'datetime_end'])class Migration(migrations.Migration):@@ -65,7 +106,7 @@ class Migration(migrations.Migration):fields=['routine', 'datetime_start'], name='manager_wor_routine_46e75a_idx'),),- migrations.RunPython(forward_func),+ migrations.RunPython(forward_func, migrations.RunPython.noop),migrations.RemoveField(model_name='workoutsession',name='date',diff --git a/wger/manager/tests/test_migration_session_datetime.py b/wger/manager/tests/test_migration_session_datetime.pynew file mode 100644index 000000000..bd71c074e--- /dev/null+++ b/wger/manager/tests/test_migration_session_datetime.py@@ -0,0 +1,92 @@+# This file is part of wger Workout Manager.+#+# 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+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# Standard Library+import datetime+from importlib import import_module+from types import SimpleNamespace++# Django+from django.test import SimpleTestCase+from django.utils import timezone+++migration = import_module('wger.manager.migrations.0028_alter_workoutsession_options_and_more')++DAY = datetime.date(2025, 3, 10)+++def local(*args):+ return timezone.make_aware(datetime.datetime(*args))+++def session(time_start=None, time_end=None, first_log=None, last_log=None):+ return SimpleNamespace(+ date=DAY,+ time_start=time_start,+ time_end=time_end,+ first_log=first_log,+ last_log=last_log,+ )+++class BuildIntervalTestCase(SimpleTestCase):+ """+ Test how the data migration maps the old date/time triple onto the new fields+ """++ def test_both_times(self):+ start, end = migration.build_interval(session(datetime.time(10, 0), datetime.time(11, 30)))++ self.assertEqual(start, local(2025, 3, 10, 10, 0))+ self.assertEqual(end, local(2025, 3, 10, 11, 30))++ def test_end_before_start_lands_on_the_next_day(self):+ start, end = migration.build_interval(session(datetime.time(23, 0), datetime.time(1, 30)))++ self.assertEqual(start, local(2025, 3, 10, 23, 0))+ self.assertEqual(end, local(2025, 3, 11, 1, 30))++ def test_only_a_start_stays_open(self):+ start, end = migration.build_interval(session(time_start=datetime.time(10, 0)))++ self.assertEqual(start, local(2025, 3, 10, 10, 0))+ self.assertIsNone(end)++ def test_only_an_end_starts_at_midnight(self):+ start, end = migration.build_interval(session(time_end=datetime.time(11, 30)))++ self.assertEqual(start, local(2025, 3, 10, 0, 0))+ self.assertEqual(end, local(2025, 3, 10, 11, 30))++ def test_no_times_uses_the_logs(self):+ start, end = migration.build_interval(+ session(first_log=local(2025, 3, 10, 18, 15), last_log=local(2025, 3, 10, 19, 40))+ )++ self.assertEqual(start, local(2025, 3, 10, 18, 15))+ self.assertEqual(end, local(2025, 3, 10, 19, 40))++ def test_a_single_log_leaves_the_session_open(self):+ moment = local(2025, 3, 10, 18, 15)+ start, end = migration.build_interval(session(first_log=moment, last_log=moment))++ self.assertEqual(start, moment)+ self.assertIsNone(end)++ def test_no_times_and_no_logs_keeps_only_the_day(self):+ start, end = migration.build_interval(session())++ self.assertEqual(start, local(2025, 3, 10, 0, 0))+ self.assertIsNone(end)