Fix datetime comparison error in victories API
This commit is contained in:
@@ -32,7 +32,6 @@ def _auth(request):
|
||||
|
||||
|
||||
def register(app):
|
||||
|
||||
@app.route("/api/victories", methods=["GET"])
|
||||
def api_getVictories():
|
||||
"""Compute noteworthy achievements. Query: ?days=30"""
|
||||
@@ -42,11 +41,12 @@ def register(app):
|
||||
|
||||
days = flask.request.args.get("days", 30, type=int)
|
||||
cutoff = tz.user_now() - timedelta(days=days)
|
||||
# Convert to naive datetime for comparison with database timestamps
|
||||
cutoff = cutoff.replace(tzinfo=None)
|
||||
|
||||
sessions = postgres.select("routine_sessions", {"user_uuid": user_uuid})
|
||||
recent = [
|
||||
s for s in sessions
|
||||
if s.get("created_at") and s["created_at"] >= cutoff
|
||||
s for s in sessions if s.get("created_at") and s["created_at"] >= cutoff
|
||||
]
|
||||
completed = [s for s in recent if s.get("status") == "completed"]
|
||||
|
||||
@@ -60,28 +60,38 @@ def register(app):
|
||||
curr = sorted_completed[i]["created_at"]
|
||||
gap = (curr - prev).days
|
||||
if gap >= 2:
|
||||
victories.append({
|
||||
"type": "comeback",
|
||||
"message": f"Came back after {gap} days — that takes real strength",
|
||||
"date": curr.isoformat() if hasattr(curr, 'isoformat') else str(curr),
|
||||
})
|
||||
victories.append(
|
||||
{
|
||||
"type": "comeback",
|
||||
"message": f"Came back after {gap} days — that takes real strength",
|
||||
"date": curr.isoformat()
|
||||
if hasattr(curr, "isoformat")
|
||||
else str(curr),
|
||||
}
|
||||
)
|
||||
|
||||
# Weekend completion
|
||||
for s in completed:
|
||||
created = s["created_at"]
|
||||
if hasattr(created, 'weekday') and created.weekday() >= 5: # Saturday=5, Sunday=6
|
||||
victories.append({
|
||||
"type": "weekend",
|
||||
"message": "Completed a routine on the weekend",
|
||||
"date": created.isoformat() if hasattr(created, 'isoformat') else str(created),
|
||||
})
|
||||
if (
|
||||
hasattr(created, "weekday") and created.weekday() >= 5
|
||||
): # Saturday=5, Sunday=6
|
||||
victories.append(
|
||||
{
|
||||
"type": "weekend",
|
||||
"message": "Completed a routine on the weekend",
|
||||
"date": created.isoformat()
|
||||
if hasattr(created, "isoformat")
|
||||
else str(created),
|
||||
}
|
||||
)
|
||||
break # Only show once
|
||||
|
||||
# Variety: 3+ different routines in a week
|
||||
routine_ids_by_week = {}
|
||||
for s in completed:
|
||||
created = s["created_at"]
|
||||
if hasattr(created, 'isocalendar'):
|
||||
if hasattr(created, "isocalendar"):
|
||||
week_key = created.isocalendar()[:2]
|
||||
if week_key not in routine_ids_by_week:
|
||||
routine_ids_by_week[week_key] = set()
|
||||
@@ -89,11 +99,13 @@ def register(app):
|
||||
|
||||
for week_key, routine_ids in routine_ids_by_week.items():
|
||||
if len(routine_ids) >= 3:
|
||||
victories.append({
|
||||
"type": "variety",
|
||||
"message": f"Completed {len(routine_ids)} different routines in one week",
|
||||
"date": None,
|
||||
})
|
||||
victories.append(
|
||||
{
|
||||
"type": "variety",
|
||||
"message": f"Completed {len(routine_ids)} different routines in one week",
|
||||
"date": None,
|
||||
}
|
||||
)
|
||||
break
|
||||
|
||||
# Full week consistency: completed every day for 7 consecutive days
|
||||
@@ -101,25 +113,27 @@ def register(app):
|
||||
dates_set = set()
|
||||
for s in completed:
|
||||
created = s["created_at"]
|
||||
if hasattr(created, 'date'):
|
||||
if hasattr(created, "date"):
|
||||
dates_set.add(created.date())
|
||||
|
||||
sorted_dates = sorted(dates_set)
|
||||
max_streak = 1
|
||||
current_streak = 1
|
||||
for i in range(1, len(sorted_dates)):
|
||||
if (sorted_dates[i] - sorted_dates[i-1]).days == 1:
|
||||
if (sorted_dates[i] - sorted_dates[i - 1]).days == 1:
|
||||
current_streak += 1
|
||||
max_streak = max(max_streak, current_streak)
|
||||
else:
|
||||
current_streak = 1
|
||||
|
||||
if max_streak >= 7:
|
||||
victories.append({
|
||||
"type": "consistency",
|
||||
"message": f"Completed routines every day for {max_streak} days straight",
|
||||
"date": None,
|
||||
})
|
||||
victories.append(
|
||||
{
|
||||
"type": "consistency",
|
||||
"message": f"Completed routines every day for {max_streak} days straight",
|
||||
"date": None,
|
||||
}
|
||||
)
|
||||
|
||||
# Limit and deduplicate
|
||||
seen_types = set()
|
||||
|
||||
Reference in New Issue
Block a user