from flask import request, url_for from app.core.models import Server, App, Subnet def inject_breadcrumbs(): """Add breadcrumbs to template context based on current route""" breadcrumbs = [] path_parts = request.path.strip('/').split('/') # Skip breadcrumbs for the dashboard home if request.endpoint == 'dashboard.dashboard_home': return {'breadcrumbs': []} # Handle IPAM routes if 'ipam' in path_parts: # Base IPAM breadcrumb breadcrumbs.append({ 'title': 'IPAM', 'url': url_for('ipam.ipam_home') }) # Handle subnet routes within IPAM if 'subnet' in path_parts: try: if 'new' in path_parts: breadcrumbs.append({ 'title': 'New Subnet', 'url': '#' }) else: subnet_id = int(path_parts[path_parts.index('subnet') + 1]) subnet = Subnet.query.get(subnet_id) if subnet: breadcrumbs.append({ 'title': subnet.cidr, 'url': url_for('ipam.subnet_view', subnet_id=subnet.id) }) if 'edit' in path_parts: breadcrumbs.append({ 'title': 'Edit', 'url': '#' }) except (ValueError, IndexError): pass # Handle server routes elif 'server' in path_parts: try: # Add Servers base breadcrumb breadcrumbs.append({ 'title': 'Servers', 'url': url_for('dashboard.server_list') }) if 'new' in path_parts: breadcrumbs.append({ 'title': 'New Server', 'url': '#' }) else: server_id = int(path_parts[path_parts.index('server') + 1]) server = Server.query.get(server_id) if server: # If we're viewing a specific server breadcrumbs.append({ 'title': server.hostname, 'url': url_for('dashboard.server_view', server_id=server.id) }) if 'edit' in path_parts: breadcrumbs.append({ 'title': 'Edit', 'url': '#' }) except (ValueError, IndexError): pass # Handle app routes elif 'app' in path_parts: try: # Add Applications base breadcrumb breadcrumbs.append({ 'title': 'Applications', 'url': url_for('dashboard.app_list') }) # For new app if 'new' in path_parts: breadcrumbs.append({ 'title': 'New Application', 'url': '#' }) else: app_id = int(path_parts[path_parts.index('app') + 1]) app = App.query.get(app_id) if app: server = Server.query.get(app.server_id) if server: breadcrumbs.append({ 'title': server.hostname, 'url': url_for('dashboard.server_view', server_id=server.id) }) # If we're viewing a specific app breadcrumbs.append({ 'title': app.name, 'url': url_for('dashboard.app_view', app_id=app.id) }) if 'edit' in path_parts: breadcrumbs.append({ 'title': 'Edit', 'url': '#' }) except (ValueError, IndexError): pass # Handle overview route elif 'overview' in path_parts: breadcrumbs.append({ 'title': 'Infrastructure Overview', 'url': url_for('dashboard.overview') }) return {'breadcrumbs': breadcrumbs}