Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Leave the workout times unset while the timezone is unknown

Roland Geider8aae78491bac
diff --git a/wger/trophies/services/statistics.py b/wger/trophies/services/statistics.py
index 2af4c37f9..5a65ed181 100644
--- a/wger/trophies/services/statistics.py
+++ b/wger/trophies/services/statistics.py
@@ -94,7 +94,8 @@ class UserStatisticsService:
# The zone of the user the sessions belong to, fetched once for the
# whole recalculation: local_day would load the profile per session
- tz = user.userprofile.zone_info
+ profile = user.userprofile
+ tz = profile.zone_info
# Calculate streaks and other date-based stats
workout_dates = sorted({day for session in sessions if (day := session.local_day_in(tz))})
@@ -106,8 +107,14 @@ class UserStatisticsService:
if workout_dates:
stats.last_workout_date = workout_dates[-1]
- # Calculate earliest and latest workout times
- earliest, latest = cls._calculate_workout_times(sessions, tz)
+ # Calculate earliest and latest workout times. A clock time only means
+ # something in the user's own zone: without a reported one the time
+ # trophies stay unawarded rather than be awarded from a guess, since a
+ # trophy is never taken away again.
+ if profile.time_zone:
+ earliest, latest = cls._calculate_workout_times(sessions, tz)
+ else:
+ earliest, latest = None, None
stats.earliest_workout_time = earliest
stats.latest_workout_time = latest
@@ -157,7 +164,8 @@ class UserStatisticsService:
volume = weight_kg * reps
stats.total_weight_lifted += volume
- tz = user.userprofile.zone_info
+ profile = user.userprofile
+ tz = profile.zone_info
# Get the session date
session_date = None
@@ -201,8 +209,9 @@ class UserStatisticsService:
# Update weekend streak
cls._update_weekend_streak_incremental(stats, session_date, tz)
- # Update workout times if session has time info
- if session and session.datetime_end:
+ # Update workout times if session has time info. Skipped without a
+ # reported zone, see update_statistics
+ if session and session.datetime_end and profile.time_zone:
session_time = timezone.localtime(session.datetime_start, timezone=tz).time()
if stats.earliest_workout_time is None or session_time < stats.earliest_workout_time:
stats.earliest_workout_time = session_time
diff --git a/wger/trophies/tests/test_services.py b/wger/trophies/tests/test_services.py
index bfa318105..81e2bda10 100644
--- a/wger/trophies/tests/test_services.py
+++ b/wger/trophies/tests/test_services.py
@@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License
# Standard Library
+import datetime
from decimal import Decimal
from unittest.mock import patch
@@ -69,6 +70,73 @@ class UserStatisticsServiceTestCase(WgerTestCase):
self.assertEqual(stats.pk, existing.pk)
self.assertEqual(stats.total_workouts, 5)
+ def make_session(self, start, end):
+ return WorkoutSession.objects.create(
+ user=self.user,
+ datetime_start=start,
+ datetime_end=end,
+ )
+
+ def test_workout_times_stay_unset_without_a_reported_zone(self):
+ """No time trophies from a guessed timezone: the times stay null"""
+
+ self.make_session(
+ datetime.datetime(2024, 6, 19, 1, 30, tzinfo=datetime.timezone.utc),
+ datetime.datetime(2024, 6, 19, 2, 30, tzinfo=datetime.timezone.utc),
+ )
+
+ stats = UserStatisticsService.update_statistics(self.user)
+
+ self.assertIsNone(stats.earliest_workout_time)
+ self.assertIsNone(stats.latest_workout_time)
+
+ def test_increment_skips_the_times_without_a_zone(self):
+ """The signal path must not set the times from a guess either"""
+
+ self.make_session(
+ datetime.datetime(2024, 6, 19, 1, 30, tzinfo=datetime.timezone.utc),
+ datetime.datetime(2024, 6, 19, 2, 30, tzinfo=datetime.timezone.utc),
+ )
+
+ stats = UserStatistics.objects.get(user=self.user)
+ self.assertIsNone(stats.earliest_workout_time)
+ self.assertIsNone(stats.latest_workout_time)
+
+ def test_workout_times_follow_the_reported_zone(self):
+ """01:30 UTC is an evening workout for a user in Denver, not an early one"""
+
+ profile = self.user.userprofile
+ profile.time_zone = 'America/Denver'
+ profile.save()
+
+ self.make_session(
+ datetime.datetime(2024, 6, 19, 1, 30, tzinfo=datetime.timezone.utc),
+ datetime.datetime(2024, 6, 19, 2, 30, tzinfo=datetime.timezone.utc),
+ )
+
+ stats = UserStatisticsService.update_statistics(self.user)
+
+ self.assertEqual(stats.earliest_workout_time, datetime.time(19, 30))
+ self.assertEqual(stats.latest_workout_time, datetime.time(19, 30))
+
+ def test_a_late_zone_report_backfills_the_times(self):
+ """The first recalculation after the zone arrives fills in the history"""
+
+ self.make_session(
+ datetime.datetime(2024, 6, 19, 1, 30, tzinfo=datetime.timezone.utc),
+ datetime.datetime(2024, 6, 19, 2, 30, tzinfo=datetime.timezone.utc),
+ )
+ stats = UserStatisticsService.update_statistics(self.user)
+ self.assertIsNone(stats.earliest_workout_time)
+
+ profile = self.user.userprofile
+ profile.time_zone = 'America/Denver'
+ profile.save()
+
+ stats = UserStatisticsService.update_statistics(self.user)
+
+ self.assertEqual(stats.earliest_workout_time, datetime.time(19, 30))
+
def test_update_statistics_creates_if_missing(self):
"""Test update_statistics creates statistics if missing"""
self.assertEqual(UserStatistics.objects.filter(user=self.user).count(), 0)