Add admin link IDs, tabs for extend/traffic, and pagination

This commit is contained in:
orohi
2026-07-25 01:15:54 +03:00
parent 663e9b1d99
commit 7842ddf1b8
4 changed files with 194 additions and 18 deletions
+77 -11
View File
@@ -64,20 +64,62 @@ async def admin_dashboard(request: Request):
return RedirectResponse("/admin/login", status_code=303)
settings = get_settings()
tab = (request.query_params.get("type") or "extend").strip().lower()
if tab not in ("extend", "traffic", "all"):
tab = "extend"
try:
page = max(1, int(request.query_params.get("page") or 1))
except ValueError:
page = 1
per_page = 10
offset = (page - 1) * per_page
db = await get_db()
try:
cursor = await db.execute(
if tab == "all":
count_sql = "SELECT COUNT(*) AS c FROM guest_links"
list_sql = """
SELECT l.*,
(SELECT COUNT(*) FROM guest_claims c WHERE c.link_id = l.id) AS claims
FROM guest_links l
ORDER BY l.id DESC
LIMIT ? OFFSET ?
"""
SELECT l.*,
(SELECT COUNT(*) FROM guest_claims c WHERE c.link_id = l.id) AS claims
FROM guest_links l
ORDER BY l.id DESC
count_args: tuple = ()
list_args: tuple = (per_page, offset)
else:
count_sql = "SELECT COUNT(*) AS c FROM guest_links WHERE link_type = ?"
list_sql = """
SELECT l.*,
(SELECT COUNT(*) FROM guest_claims c WHERE c.link_id = l.id) AS claims
FROM guest_links l
WHERE l.link_type = ?
ORDER BY l.id DESC
LIMIT ? OFFSET ?
"""
)
count_args = (tab,)
list_args = (tab, per_page, offset)
cursor = await db.execute(count_sql, count_args)
total = int((await cursor.fetchone())["c"])
cursor = await db.execute(list_sql, list_args)
links = [dict(row) for row in await cursor.fetchall()]
cursor = await db.execute(
"SELECT link_type, COUNT(*) AS c FROM guest_links GROUP BY link_type"
)
counts = {"extend": 0, "traffic": 0, "all": 0}
for row in await cursor.fetchall():
counts[row["link_type"]] = int(row["c"])
counts["all"] += int(row["c"])
finally:
await db.close()
total_pages = max(1, (total + per_page - 1) // per_page)
if page > total_pages:
page = total_pages
site_base = public_base_url(request)
return templates.TemplateResponse(
"admin/dashboard.html",
@@ -87,6 +129,12 @@ async def admin_dashboard(request: Request):
"links": links,
"site_base": site_base,
"flash": request.query_params.get("flash"),
"tab": tab,
"page": page,
"per_page": per_page,
"total": total,
"total_pages": total_pages,
"counts": counts,
},
)
@@ -159,11 +207,19 @@ async def create_link(
finally:
await db.close()
return RedirectResponse("/admin?flash=created", status_code=303)
return RedirectResponse(
f"/admin?type={link_type}&flash=created", status_code=303
)
@router.post("/links/{link_id}/toggle")
async def toggle_link(link_id: int, _admin: str = Depends(get_current_admin)):
async def toggle_link(
request: Request,
link_id: int,
_admin: str = Depends(get_current_admin),
):
tab = (request.query_params.get("type") or "extend").strip()
page = request.query_params.get("page") or "1"
db = await get_db()
try:
await db.execute(
@@ -173,18 +229,28 @@ async def toggle_link(link_id: int, _admin: str = Depends(get_current_admin)):
await db.commit()
finally:
await db.close()
return RedirectResponse("/admin?flash=toggled", status_code=303)
return RedirectResponse(
f"/admin?type={tab}&page={page}&flash=toggled", status_code=303
)
@router.post("/links/{link_id}/delete")
async def delete_link(link_id: int, _admin: str = Depends(get_current_admin)):
async def delete_link(
request: Request,
link_id: int,
_admin: str = Depends(get_current_admin),
):
tab = (request.query_params.get("type") or "extend").strip()
page = request.query_params.get("page") or "1"
db = await get_db()
try:
await db.execute("DELETE FROM guest_links WHERE id = ?", (link_id,))
await db.commit()
finally:
await db.close()
return RedirectResponse("/admin?flash=deleted", status_code=303)
return RedirectResponse(
f"/admin?type={tab}&page={page}&flash=deleted", status_code=303
)
@router.get("/links/{link_id}", response_class=HTMLResponse)