From f426b2c0258c06d70d9fe828f68ecd7e703a068c Mon Sep 17 00:00:00 2001 From: root Date: Tue, 24 Mar 2026 14:18:04 +0000 Subject: [PATCH] fix: ensure HTML response for downloads polling - Added html=1 parameter to downloads HTMX polling - Fixed data structure mismatch between backend and Jinja2 template - Guaranteed TemplateResponse for downloads list when requested --- app/routers/router_downloads.py | 29 +++++++++++++++++++----- templates/components/downloads_list.html | 16 ++++++------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/app/routers/router_downloads.py b/app/routers/router_downloads.py index cfbd918..e5c1779 100644 --- a/app/routers/router_downloads.py +++ b/app/routers/router_downloads.py @@ -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"} diff --git a/templates/components/downloads_list.html b/templates/components/downloads_list.html index f16246f..c6bdce3 100644 --- a/templates/components/downloads_list.html +++ b/templates/components/downloads_list.html @@ -1,9 +1,9 @@
{% if tasks %} - {% for task_id, task in tasks.items() %} + {% for task in tasks %}
{{ task.filename }} @@ -16,29 +16,29 @@
{{ task.progress | round(1) }}% - {{ task.download_speed }} - {{ task.eta }} + {{ task.speed or '0' }} KB/s + {{ task.eta or '' }}
{% if task.status == 'downloading' or task.status == 'pending' %} - {% elif task.status == 'paused' %} - {% endif %} {% if task.status == 'completed' %} - + {% endif %}