/api/user/summary: streak.activeToday and streak.lost for the app's lit / at-risk / frozen flame

activeToday: activity recorded today (Tehran date). lost: length of a streak that broke (last active day
before yesterday), so the app can show it frozen. Same rule as effective_current_streak; the weekly report
keeps its own two-field streak serializer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 18:27:14 +03:30
parent 2e87a81cc5
commit 9024c6b8d8
4 changed files with 48 additions and 4 deletions

View File

@@ -57,3 +57,21 @@ def effective_current_streak(streak, today=None):
if last is None or last < today - timedelta(days=1):
return 0
return streak.current_streak
def streak_state(streak, today=None):
"""
وضعیتِ زنجیره برای نمایش: (current, active_today, lost).
lost طولِ زنجیره‌ای است که شکسته (آخرین روزِ فعال پیش از دیروز)؛ current_streakِ دیتابیس تا فعالیتِ بعدی
همان عددِ قبلی را نگه می‌دارد (record_activity آن را به ۱ برمی‌گرداند)، پس همین عدد است.
"""
if streak is None:
return 0, False, 0
today = today or timezone.localdate()
last = streak.active_days.order_by('-date').values_list('date', flat=True).first()
if last is None:
return 0, False, 0
if last < today - timedelta(days=1):
return 0, False, streak.current_streak
return streak.current_streak, last == today, 0