Group payments into ISO-week buckets

Python · Python · advanced · greenfield

Adds the ISO-week bucketing for the revenue report. The key comes straight from `date.isocalendar()` — both the week and, crucially, the ISO week-year, so the December/January boundary lands in the week finance expects. Keys are zero-padded so lexicographic order is chronological order, and the result dict is built in sorted key order.

Requirements

Files touched

--- app/reports/weekly_revenue.py
+from collections import defaultdict
+
+
+def iso_week_key(day):
+    """ISO-8601 week key for a date, e.g. ``2026-W03``."""
+    year, week, _ = day.isocalendar()
+    return f"{year:04d}-W{week:02d}"
+
+
+def weekly_totals(payments):
+    """Sum payment amounts (integer cents) into ISO-week buckets, oldest first."""
+    totals = defaultdict(int)
+    for payment in payments:
+        totals[iso_week_key(payment["paid_on"])] += payment["amount_cents"]
+    return dict(sorted(totals.items()))
+

Review this PR

Python practice