fix: dropdown «Файлы» на FS, а не на таблице users

В таблице users БД нет env-only аккаунтов (smeta) и удалённых
пользователей, у которых на диске остались файлы — они не
показывались в админке. Новый endpoint /api/admin/file_users
сканирует DATA_DIR, возвращает имена и счётчики файлов.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-05-06 18:00:25 +10:00
co-authored by Claude Opus 4.7
parent 7447626a33
commit 53b4aa3ff5
2 changed files with 44 additions and 7 deletions
+27
View File
@@ -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)."""
+17 -7
View File
@@ -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 = '<tr><th>Email</th><th>Роль</th><th>Статус</th><th>Создан</th><th>Действия</th></tr>';
const fSel = document.getElementById('files-user');
if (fSel) fSel.innerHTML = '<option value="">— выберите пользователя —</option>';
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 = '<td>'+u.email+'</td><td>'+u.role+'</td><td>'+status+'</td><td class="time">'+ts+'</td><td>'+actions+'</td>';
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 = '<option value="">— выберите пользователя —</option>';
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');