Restore staged uploads after page reload

This commit is contained in:
Krikorios
2026-05-07 18:53:40 +03:00
parent 68d13dbf57
commit 631a4c3e3d
2 changed files with 38 additions and 1 deletions
+26
View File
@@ -205,12 +205,34 @@ async def upload_page(request: Request):
SUM(CASE WHEN status='error' THEN 1 ELSE 0 END) AS errors SUM(CASE WHEN status='error' THEN 1 ELSE 0 END) AS errors
FROM documents WHERE status != 'staged'""" FROM documents WHERE status != 'staged'"""
).fetchone() ).fetchone()
staged_rows = conn.execute(
"""SELECT id, image_path, page_number
FROM documents
WHERE status='staged'
ORDER BY id"""
).fetchall()
staged_documents = []
for row in staged_rows:
image_name = Path(row["image_path"]).name
page_number = row["page_number"]
if page_number:
image_name = f"{image_name} (p{page_number})"
staged_documents.append(
{
"id": row["id"],
"image_path": row["image_path"],
"name": image_name,
}
)
return render_template( return render_template(
templates, templates,
request, request,
"index.html", "index.html",
{ {
"stats": dict(stats) if stats else {}, "stats": dict(stats) if stats else {},
"staged_documents": staged_documents,
"providers": get_available_providers(), "providers": get_available_providers(),
"default_provider": get_default_provider(), "default_provider": get_default_provider(),
}, },
@@ -397,12 +419,16 @@ async def process_staged(
provider = get_default_provider() provider = get_default_provider()
ids = [int(x) for x in doc_ids.split(",") if x.strip().isdigit()] ids = [int(x) for x in doc_ids.split(",") if x.strip().isdigit()]
if not ids:
return RedirectResponse("/?staged=0", status_code=303)
with get_db() as conn: with get_db() as conn:
rows = conn.execute( rows = conn.execute(
f"SELECT id, image_path FROM documents WHERE id IN ({','.join('?' * len(ids))}) AND status='staged'", f"SELECT id, image_path FROM documents WHERE id IN ({','.join('?' * len(ids))}) AND status='staged'",
ids, ids,
).fetchall() ).fetchall()
if not rows:
return RedirectResponse("/?staged=0", status_code=303)
for row in rows: for row in rows:
conn.execute( conn.execute(
"UPDATE documents SET status='pending', provider=?, updated_at=CURRENT_TIMESTAMP WHERE id=?", "UPDATE documents SET status='pending', provider=?, updated_at=CURRENT_TIMESTAMP WHERE id=?",
+12 -1
View File
@@ -124,7 +124,7 @@
{% block scripts %} {% block scripts %}
<script> <script>
// ─── Unified staging workflow ───────────────────────────────── // ─── Unified staging workflow ─────────────────────────────────
const stagedItems = []; // {id, image_path, name} const stagedItems = {{ staged_documents|tojson }}; // {id, image_path, name}
let uploading = false; let uploading = false;
const cameraInput = document.getElementById('cameraInput'); const cameraInput = document.getElementById('cameraInput');
@@ -209,6 +209,14 @@ function finishThumb(thumb, item) {
`; `;
} }
function renderExistingStaged() {
gallery.innerHTML = '';
for (const item of stagedItems) {
const thumb = addPlaceholder(item.name);
finishThumb(thumb, item);
}
}
async function removeStagedItem(id, btn) { async function removeStagedItem(id, btn) {
const thumb = btn.closest('.staging-thumb'); const thumb = btn.closest('.staging-thumb');
thumb.classList.add('removing'); thumb.classList.add('removing');
@@ -266,5 +274,8 @@ async function clearStaged() {
gallery.innerHTML = ''; gallery.innerHTML = '';
refreshUI(); refreshUI();
} }
renderExistingStaged();
refreshUI();
</script> </script>
{% endblock %} {% endblock %}