fix: [BUG-001] TypeError in nutrition_weekly endpoint
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 <noreply@anthropic.com>
This commit is contained in:
parent
f2f089a223
commit
4d9c59ccf7
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user