diff --git a/main.py b/main.py index bb5f935..ae2a636 100644 --- a/main.py +++ b/main.py @@ -785,6 +785,33 @@ async def admin_verify_endpoint(request: Request): return {"ok": True} +@app.get("/api/admin/file_users") +async def admin_file_users_endpoint(request: Request): + """Юзеры, у которых есть файлы в DATA_DIR. + + Источник — FS, не таблица users: бывают env-only аккаунты (`smeta`) + и удалённые из БД пользователи, чьи файлы остались. Админу нужно + видеть всех с реальными файлами. + """ + _require_admin(request) + data_dir = Path(os.environ.get("DATA_DIR", "./smeta-web-data")) + if not data_dir.exists(): + return {"users": []} + out = [] + for d in sorted(data_dir.iterdir()): + if not d.is_dir() or d.name.startswith(".") or d.name.startswith("_"): + continue + n = 0 + for tab in d.iterdir(): + if tab.is_dir() and not tab.name.startswith("."): + for f in tab.iterdir(): + if f.is_file() and not f.name.startswith("."): + n += 1 + out.append({"username": d.name, "file_count": n}) + out.sort(key=lambda x: -x["file_count"]) + return {"users": out} + + @app.get("/api/admin/files") async def admin_list_files_endpoint(request: Request): """All files uploaded by a specific user (across every tab).""" diff --git a/static/admin.html b/static/admin.html index 5785573..f17045c 100644 --- a/static/admin.html +++ b/static/admin.html @@ -184,8 +184,6 @@ function loadUsers() { fetch('/api/admin/users', {headers: H}).then(r=>r.json()).then(users => { const t = document.getElementById('users'); t.innerHTML = 'EmailРольСтатусСозданДействия'; - const fSel = document.getElementById('files-user'); - if (fSel) fSel.innerHTML = ''; for (const u of users) { const d = new Date(u.created_at < 1e12 ? u.created_at*1000 : u.created_at); const tr = document.createElement('tr'); @@ -200,11 +198,6 @@ function loadUsers() { actions += btn('Удалить', "userAction('delete','"+u.email+"')"); tr.innerHTML = ''+u.email+''+u.role+''+status+''+ts+''+actions+''; t.appendChild(tr); - if (fSel) { - const opt = document.createElement('option'); - opt.value = u.email; opt.textContent = u.email; - fSel.appendChild(opt); - } } }).catch(()=>{}); } @@ -216,6 +209,23 @@ function fmtSize(n){ if (n < 1024*1024) return (n/1024).toFixed(1)+' KB'; return (n/1024/1024).toFixed(1)+' MB'; } +// dropdown файловой страницы наполняется из FS, не из БД-юзеров — +// бывают env-only / удалённые аккаунты, чьи файлы всё ещё на диске +async function loadFileUsers() { + const sel = document.getElementById('files-user'); + if (!sel) return; + const r = await fetch('/api/admin/file_users', {headers: H}); + if (!r.ok) return; + const d = await r.json(); + sel.innerHTML = ''; + for (const u of d.users) { + const opt = document.createElement('option'); + opt.value = u.username; + opt.textContent = u.username + ' (' + u.file_count + ')'; + sel.appendChild(opt); + } +} +loadFileUsers(); async function loadFiles() { const u = document.getElementById('files-user').value; const tbl = document.getElementById('files');