askrobots / dbbasic-admin
Public
An admin interface that builds itself from the dbbasic modules you install; its pages follow file paths, like CGI.
__init__.py · 3.0 KB · 55 lines
| 1 | """Admin dashboard handler - /admin/""" |
| 2 | from dbbasic_web.responses import html |
| 3 | from dbbasic_admin.admin import build_nav |
| 4 | from dbbasic_admin.discover import discover_tsv_tables |
| 5 | |
| 6 | |
| 7 | def handle(request): |
| 8 | """Render admin dashboard""" |
| 9 | nav_items = build_nav() |
| 10 | tables = discover_tsv_tables() |
| 11 | |
| 12 | total_rows = sum(t['rows'] for t in tables) |
| 13 | |
| 14 | # Build nav HTML |
| 15 | nav_html = "".join(f'<li><a href="{item["href"]}">{item.get("icon", "")} {item["label"]}</a></li>' for item in nav_items) |
| 16 | |
| 17 | html_content = f"""<!DOCTYPE html> |
| 18 | <html> |
| 19 | <head> |
| 20 | <title>Admin Dashboard</title> |
| 21 | <style> |
| 22 | * {{ margin: 0; padding: 0; box-sizing: border-box; }} |
| 23 | body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; display: flex; height: 100vh; background: #f5f5f5; }} |
| 24 | .sidebar {{ width: 250px; background: #2c3e50; color: white; padding: 20px; overflow-y: auto; }} |
| 25 | .sidebar h1 {{ font-size: 24px; margin-bottom: 30px; color: #ecf0f1; }} |
| 26 | .sidebar ul {{ list-style: none; }} |
| 27 | .sidebar li {{ margin-bottom: 10px; }} |
| 28 | .sidebar a {{ color: #ecf0f1; text-decoration: none; display: block; padding: 10px; border-radius: 5px; transition: background 0.2s; }} |
| 29 | .sidebar a:hover {{ background: #34495e; }} |
| 30 | .content {{ flex: 1; padding: 40px; overflow-y: auto; }} |
| 31 | .header {{ background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }} |
| 32 | .header h2 {{ color: #2c3e50; margin-bottom: 10px; }} |
| 33 | .card {{ background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }} |
| 34 | .stats {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-top: 20px; }} |
| 35 | .stat-card {{ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; text-align: center; }} |
| 36 | .stat-value {{ font-size: 32px; font-weight: bold; margin-bottom: 5px; }} |
| 37 | </style> |
| 38 | </head> |
| 39 | <body> |
| 40 | <div class="sidebar"><h1>Admin</h1><ul>{nav_html}</ul></div> |
| 41 | <div class="content"> |
| 42 | <div class="header"><h2>Dashboard</h2><p>Welcome to the admin interface</p></div> |
| 43 | <div class="card"><h3>Quick Stats</h3><div class="stats"> |
| 44 | <div class="stat-card"><div class="stat-value">{len(tables)}</div><div>Tables</div></div> |
| 45 | <div class="stat-card" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);"><div class="stat-value">{total_rows}</div><div>Total Rows</div></div> |
| 46 | <div class="stat-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);"><div class="stat-value">1</div><div>Active Users</div></div> |
| 47 | </div></div> |
| 48 | <div class="card"><h3>Tables</h3><ul> |
| 49 | {"".join(f'<li><a href="/admin/database?table={t["name"]}">{t["name"]}</a> - {t["rows"]} rows</li>' for t in tables)} |
| 50 | </ul></div> |
| 51 | </div> |
| 52 | </body> |
| 53 | </html>""" |
| 54 | |
| 55 | return html(html_content) |