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
This commit is contained in:
@@ -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"}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<div class="downloads-grid"
|
||||
hx-get="/api/downloads?html=true"
|
||||
hx-get="/api/downloads?html=1"
|
||||
hx-trigger="every 2s"
|
||||
hx-swap="innerHTML">
|
||||
{% if tasks %}
|
||||
{% for task_id, task in tasks.items() %}
|
||||
{% for task in tasks %}
|
||||
<div class="download-item task-{{ task.status }}">
|
||||
<div class="download-info">
|
||||
<span class="download-name" title="{{ task.filename }}">{{ task.filename }}</span>
|
||||
@@ -16,29 +16,29 @@
|
||||
|
||||
<div class="download-meta">
|
||||
<span>{{ task.progress | round(1) }}%</span>
|
||||
<span>{{ task.download_speed }}</span>
|
||||
<span>{{ task.eta }}</span>
|
||||
<span>{{ task.speed or '0' }} KB/s</span>
|
||||
<span>{{ task.eta or '' }}</span>
|
||||
</div>
|
||||
|
||||
<div class="download-actions">
|
||||
{% if task.status == 'downloading' or task.status == 'pending' %}
|
||||
<button class="btn-icon" hx-post="/api/downloads/{{ task_id }}/pause" hx-swap="none">
|
||||
<button class="btn-icon" hx-post="/api/downloads/{{ task.id }}/pause" hx-swap="none">
|
||||
<i class="fas fa-pause"></i>
|
||||
</button>
|
||||
{% elif task.status == 'paused' %}
|
||||
<button class="btn-icon" hx-post="/api/downloads/{{ task_id }}/resume" hx-swap="none">
|
||||
<button class="btn-icon" hx-post="/api/downloads/{{ task.id }}/resume" hx-swap="none">
|
||||
<i class="fas fa-play"></i>
|
||||
</button>
|
||||
{% endif %}
|
||||
|
||||
{% if task.status == 'completed' %}
|
||||
<a href="/video/{{ task_id }}" class="btn-icon success">
|
||||
<a href="/video/{{ task.id }}" class="btn-icon success">
|
||||
<i class="fas fa-external-link-alt"></i>
|
||||
</a>
|
||||
{% endif %}
|
||||
|
||||
<button class="btn-icon danger"
|
||||
hx-delete="/api/downloads/{{ task_id }}"
|
||||
hx-delete="/api/downloads/{{ task.id }}"
|
||||
hx-confirm="Supprimer ce téléchargement ?"
|
||||
hx-swap="none">
|
||||
<i class="fas fa-trash"></i>
|
||||
|
||||
Reference in New Issue
Block a user