Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Drop the caller's ordering from the daily value counts

Roland Geider11e42d3702b4
diff --git a/wger/measurements/api/aggregates.py b/wger/measurements/api/aggregates.py
index 7e8bd0edb..b0a3a09f3 100644
--- a/wger/measurements/api/aggregates.py
+++ b/wger/measurements/api/aggregates.py
@@ -168,10 +168,13 @@ def value_count_rows(queryset, tz, summed_per_day: bool = False) -> list[dict]:
if summed_per_day:
# A day is one value here, so the totals are counted in python: the
# database has already grouped them into days
+ # order_by() drops the caller's ordering: a sort column outside the
+ # grouping lands in the GROUP BY and splits the totals up again
daily = (
queryset.annotate(day=TruncDay('date', tzinfo=tz), unit=F('extra_data__unit'))
.values('category', 'day', 'unit')
.annotate(value=Sum('value'), newest=Max('date'))
+ .order_by()
)
counted: dict[tuple, dict] = {}
for row in daily:
diff --git a/wger/measurements/tests/test_aggregates.py b/wger/measurements/tests/test_aggregates.py
index 2f655e4d4..c57d5d765 100644
--- a/wger/measurements/tests/test_aggregates.py
+++ b/wger/measurements/tests/test_aggregates.py
@@ -191,6 +191,16 @@ class AggregateApiTestCase(WgerTestCase):
self.assertEqual(rows[0]['value'], '5000.00')
self.assertEqual(rows[0]['count'], 2)
+ def test_daily_totals_survive_an_ordering_parameter(self):
+ for day, hour, value in ((4, 8, 3000), (4, 20, 2000), (5, 8, 5000)):
+ self.add(timezone.make_aware(datetime.datetime(2026, 5, day, hour)), value)
+
+ rows = self.value_counts(summed_per_day='true', tz='UTC', ordering='date')
+
+ self.assertEqual(len(rows), 1)
+ self.assertEqual(rows[0]['value'], '5000.00')
+ self.assertEqual(rows[0]['count'], 2)
+
def test_a_bound_stored_as_a_string_still_reads(self):
# Postgres refuses to cast a JSON string to numeric, even a numeric
# one, so a row predating the write validation would otherwise take