added users administration tool

This commit is contained in:
knedlik
2025-12-15 08:21:18 +01:00
parent e2091ec677
commit 13d747827d
11 changed files with 602 additions and 76 deletions

41
app/db_users_search.py Normal file
View File

@@ -0,0 +1,41 @@
# db_users_search.py
import sqlite3
from contextlib import contextmanager
DB_PATH = "app.db"
@contextmanager
def get_conn():
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
conn.row_factory = sqlite3.Row
try:
yield conn
conn.commit()
finally:
conn.close()
def search_users(q: str, limit: int, offset: int):
q = (q or "").strip()
like = f"%{q}%"
with get_conn() as conn:
rows = conn.execute("""
SELECT id, username, display_name, email, role, is_active, created_at
FROM users
WHERE (? = '')
OR (username LIKE ? COLLATE NOCASE
OR display_name LIKE ? COLLATE NOCASE
OR email LIKE ? COLLATE NOCASE)
ORDER BY username
LIMIT ? OFFSET ?
""", (q, like, like, like, limit, offset)).fetchall()
total = conn.execute("""
SELECT COUNT(*)
FROM users
WHERE (? = '')
OR (username LIKE ? COLLATE NOCASE
OR display_name LIKE ? COLLATE NOCASE
OR email LIKE ? COLLATE NOCASE)
""", (q, like, like, like)).fetchone()[0]
return [dict(r) for r in rows], int(total)