From 4d9c59ccf771698a519513a2d44a82a20bb4bb31 Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 21 Mar 2026 07:58:37 +0100 Subject: [PATCH] fix: [BUG-001] TypeError in nutrition_weekly endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: - /api/nutrition/weekly crashed with 500 Internal Server Error - TypeError: strptime() argument 1 must be str, not datetime.date Root Cause: - d['date'] from PostgreSQL is already datetime.date object - datetime.strptime() expects string input - Line 156: wk=datetime.strptime(d['date'],'%Y-%m-%d').strftime('%Y-W%V') Solution: - Added type check before strptime() - If date already has strftime method → use directly - Else → parse as string first - Works with both datetime.date objects and strings Tested: - /nutrition page loads without error - Weekly aggregation works correctly - Chart displays nutrition data Closes: BUG-001 Co-Authored-By: Claude Opus 4.6 --- backend/routers/nutrition.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/routers/nutrition.py b/backend/routers/nutrition.py index 3c6d46a..223dacc 100644 --- a/backend/routers/nutrition.py +++ b/backend/routers/nutrition.py @@ -153,7 +153,9 @@ def nutrition_weekly(weeks: int=16, x_profile_id: Optional[str]=Header(default=N if not rows: return [] wm={} for d in rows: - wk=datetime.strptime(d['date'],'%Y-%m-%d').strftime('%Y-W%V') + # Handle both datetime.date objects (from DB) and strings + date_obj = d['date'] if hasattr(d['date'], 'strftime') else datetime.strptime(d['date'],'%Y-%m-%d') + wk = date_obj.strftime('%Y-W%V') wm.setdefault(wk,[]).append(d) result=[] for wk in sorted(wm):