askrobots / dbbasic-admin
Public
An admin interface that builds itself from the dbbasic modules you install; its pages follow file paths, like CGI.
admin.py · 4.1 KB · 136 lines
| 1 | """ |
| 2 | Core auto-discovery and navigation building for dbbasic-admin |
| 3 | """ |
| 4 | |
| 5 | import importlib |
| 6 | import pkgutil |
| 7 | from typing import List, Dict, Any |
| 8 | from functools import lru_cache |
| 9 | import time |
| 10 | |
| 11 | # Core admin navigation (always present) |
| 12 | CORE_NAV = [ |
| 13 | {'icon': '📊', 'label': 'Dashboard', 'href': '/admin/', 'order': 0}, |
| 14 | {'icon': '💻', 'label': 'Code', 'href': '/admin/code', 'order': 10}, |
| 15 | {'icon': '🗄️', 'label': 'Database', 'href': '/admin/database', 'order': 11}, |
| 16 | ] |
| 17 | |
| 18 | # System navigation (always at bottom) |
| 19 | SYSTEM_NAV = [ |
| 20 | {'icon': '⏰', 'label': 'Jobs', 'href': '/admin/jobs', 'order': 90}, |
| 21 | {'icon': '📋', 'label': 'Logs', 'href': '/admin/logs', 'order': 91}, |
| 22 | {'icon': '🚀', 'label': 'Deploy', 'href': '/admin/deploy', 'order': 92}, |
| 23 | {'icon': '💾', 'label': 'Backup', 'href': '/admin/backup', 'order': 93}, |
| 24 | {'icon': '⚙️', 'label': 'Settings', 'href': '/admin/settings', 'order': 99}, |
| 25 | ] |
| 26 | |
| 27 | |
| 28 | @lru_cache(maxsize=1) |
| 29 | def discover_modules() -> List[Dict[str, Any]]: |
| 30 | """ |
| 31 | Auto-discover admin modules from installed packages. |
| 32 | |
| 33 | Scans all installed packages for those starting with 'dbbasic_' |
| 34 | and looks for an 'admin.py' module with 'ADMIN_CONFIG' export. |
| 35 | |
| 36 | Returns: |
| 37 | List of dicts with 'name' and 'config' keys |
| 38 | |
| 39 | Example: |
| 40 | [ |
| 41 | { |
| 42 | 'name': 'dbbasic_blog', |
| 43 | 'config': [ |
| 44 | {'icon': '📝', 'label': 'Posts', 'href': '/admin/posts', 'order': 20} |
| 45 | ] |
| 46 | } |
| 47 | ] |
| 48 | """ |
| 49 | modules = [] |
| 50 | |
| 51 | for pkg in pkgutil.iter_modules(): |
| 52 | # Only scan dbbasic_* packages, excluding ourselves |
| 53 | if pkg.name.startswith('dbbasic_') and pkg.name != 'dbbasic_admin': |
| 54 | try: |
| 55 | # Try to import the module's admin.py |
| 56 | admin_mod = importlib.import_module(f'{pkg.name}.admin') |
| 57 | |
| 58 | # Check if it exports ADMIN_CONFIG |
| 59 | if hasattr(admin_mod, 'ADMIN_CONFIG'): |
| 60 | config = admin_mod.ADMIN_CONFIG |
| 61 | |
| 62 | # Validate config is a list |
| 63 | if isinstance(config, list): |
| 64 | modules.append({ |
| 65 | 'name': pkg.name, |
| 66 | 'config': config, |
| 67 | 'discovered_at': time.time() |
| 68 | }) |
| 69 | |
| 70 | except (ImportError, AttributeError): |
| 71 | # Module doesn't have admin interface, skip it |
| 72 | continue |
| 73 | except Exception as e: |
| 74 | # Log error but don't break discovery |
| 75 | print(f"Warning: Error discovering {pkg.name}: {e}") |
| 76 | continue |
| 77 | |
| 78 | return modules |
| 79 | |
| 80 | |
| 81 | def build_nav() -> List[Dict[str, Any]]: |
| 82 | """ |
| 83 | Build complete navigation from core + discovered modules + system. |
| 84 | |
| 85 | Returns: |
| 86 | Sorted list of nav items with all required fields |
| 87 | |
| 88 | Example: |
| 89 | [ |
| 90 | {'icon': '📊', 'label': 'Dashboard', 'href': '/admin/', 'order': 0}, |
| 91 | {'icon': '📝', 'label': 'Posts', 'href': '/admin/posts', 'order': 20}, |
| 92 | {'icon': '⚙️', 'label': 'Settings', 'href': '/admin/settings', 'order': 99}, |
| 93 | ] |
| 94 | """ |
| 95 | nav_items = [] |
| 96 | |
| 97 | # Add core navigation |
| 98 | nav_items.extend(CORE_NAV) |
| 99 | |
| 100 | # Add discovered module navigation |
| 101 | for module in discover_modules(): |
| 102 | nav_items.extend(module['config']) |
| 103 | |
| 104 | # Add system navigation |
| 105 | nav_items.extend(SYSTEM_NAV) |
| 106 | |
| 107 | # Sort by order (default to 50 if not specified) |
| 108 | nav_items.sort(key=lambda x: x.get('order', 50)) |
| 109 | |
| 110 | return nav_items |
| 111 | |
| 112 | |
| 113 | def clear_cache(): |
| 114 | """ |
| 115 | Clear the discovery cache. |
| 116 | |
| 117 | Call this after installing or uninstalling a module to force |
| 118 | re-discovery on the next build_nav() call. |
| 119 | """ |
| 120 | discover_modules.cache_clear() |
| 121 | |
| 122 | |
| 123 | def get_module_info(module_name: str) -> Dict[str, Any]: |
| 124 | """ |
| 125 | Get information about a specific admin module. |
| 126 | |
| 127 | Args: |
| 128 | module_name: Name of the module (e.g., 'dbbasic_blog') |
| 129 | |
| 130 | Returns: |
| 131 | Module info dict or None if not found |
| 132 | """ |
| 133 | for module in discover_modules(): |
| 134 | if module['name'] == module_name: |
| 135 | return module |
| 136 | return None |