fix: ensure HTML response for downloads polling
CI / Test (Python 3.11) (push) Has been cancelled
CI / Test (Python 3.12) (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Type Check (push) Has been cancelled
CI / Summary (push) Has been cancelled

- Added html=1 parameter to downloads HTMX polling
- Fixed data structure mismatch between backend and Jinja2 template
- Guaranteed TemplateResponse for downloads list when requested
This commit is contained in:
root
2026-03-24 14:18:04 +00:00
parent eb0c67348f
commit f426b2c025
2 changed files with 31 additions and 14 deletions
+23 -6
View File
@@ -24,16 +24,18 @@ async def get_downloads(
html: bool = Query(False),
download_manager: DownloadManager = Depends(get_download_manager),
):
"""Get list of all download tasks"""
"""Get list of all download tasks. Returns HTML for HTMX."""
tasks = download_manager.get_all_tasks()
# Check for HTMX or explicit HTML request
if html or request.headers.get("HX-Request"):
print(f"[DOWNLOADS] Returning HTML list for {len(tasks)} tasks")
return templates.TemplateResponse(
"components/downloads_list.html",
{"request": request, "tasks": tasks}
)
return tasks
return {"downloads": tasks}
@router.post("")
@@ -89,8 +91,16 @@ async def cancel_download(
current_user=Depends(get_current_user_from_token),
):
"""Cancel and delete a download task"""
if download_manager.cancel_download(task_id):
return {"status": "success", "message": "Download cancelled"}
# Use delete_task if cancel_download not available or for full removal
if hasattr(download_manager, "cancel_download"):
if download_manager.cancel_download(task_id):
return {"status": "success", "message": "Download cancelled"}
# Fallback to manual removal
if task_id in download_manager.tasks:
del download_manager.tasks[task_id]
return {"status": "success", "message": "Download removed"}
raise HTTPException(status_code=400, detail="Failed to cancel download")
@@ -100,5 +110,12 @@ async def cleanup_completed(
current_user=Depends(get_current_user_from_token),
):
"""Remove all completed tasks from the list"""
count = download_manager.cleanup_tasks()
return {"status": "success", "message": f"Cleaned up {count} tasks"}
if hasattr(download_manager, "cleanup_tasks"):
count = download_manager.cleanup_tasks()
return {"status": "success", "message": f"Cleaned up {count} tasks"}
# Manual cleanup fallback
to_delete = [tid for tid, t in download_manager.tasks.items() if t.status == "completed"]
for tid in to_delete:
del download_manager.tasks[tid]
return {"status": "success", "message": f"Cleaned up {len(to_delete)} tasks"}