From ce174c65416a7bd7d91ed7049250c8fe0e662e33 Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 4 Nov 2026 09:12:22 +0100 Subject: [PATCH] feat: switch port PDF covers the whole switch stack Front panels and port lists are drawn per stack member with shared VLAN colours. Co-Authored-By: Claude Opus 5.5 --- netbox_utilities/__init__.py | 2 +- netbox_utilities/switch_port_pdf.py | 42 ++++++++++++++++++--------- netbox_utilities/switch_port_views.py | 12 ++++++-- netbox_utilities/switch_stack.py | 2 +- pyproject.toml | 2 +- 5 files changed, 41 insertions(+), 19 deletions(-) diff --git a/netbox_utilities/__init__.py b/netbox_utilities/__init__.py index 99ebc87..aa09482 100644 --- a/netbox_utilities/__init__.py +++ b/netbox_utilities/__init__.py @@ -1,6 +1,6 @@ from netbox.plugins import PluginConfig, get_plugin_config -__version__ = "0.21.0" +__version__ = "0.22.0" class NetBoxUtilitiesConfig(PluginConfig): diff --git a/netbox_utilities/switch_port_pdf.py b/netbox_utilities/switch_port_pdf.py index 650d87a..2fb6a75 100644 --- a/netbox_utilities/switch_port_pdf.py +++ b/netbox_utilities/switch_port_pdf.py @@ -372,8 +372,12 @@ def _port_table(ports, t, styles): return table -def build_pdf(device, ports, lang="de", generated=None): - """Return the PDF as bytes. ``device`` is a DeviceInfo, ``ports`` a list of switch_ports.Port.""" +def build_pdf(device, ports, lang="de", generated=None, members=None): + """Return the PDF as bytes. ``device`` is a DeviceInfo, ``ports`` a list of switch_ports.Port. + + For a switch stack, ``members`` is [(DeviceInfo, ports)] for every stack member including the + switch itself; front panels and port lists are then drawn per member, ``ports`` covers all of them. + """ t = TEXTS[language(lang)] styles = _styles() generated = generated or datetime.now() @@ -414,18 +418,22 @@ def build_pdf(device, ports, lang="de", generated=None): ] switch_ports.assign_vlan_colors(ports) - groups = switch_ports.build_groups(ports) - if not groups: + units = members or [(device, ports)] + if not any(switch_ports.build_groups(unit_ports) for _, unit_ports in units): story.append(Paragraph(t["none"], styles["meta"])) - for group in groups: - tile_w, chunks = _layout(group.columns, group.optical, width) - panels = [PortPanel(chunk, group.optical, width, tile_w) for chunk in chunks] - title = Paragraph(f"{_escape(group.title)} · {len(group.ports)} {t['ports']}", styles["group"]) - block = [title, Spacer(0, 0.8 * mm)] - for panel in panels: - block.extend([panel, Spacer(0, 1.2 * mm)]) - story.append(KeepTogether(block)) - story.append(Spacer(0, 1.5 * mm)) + for unit, unit_ports in units: + groups = switch_ports.build_groups(unit_ports) + if members: + story.append(Paragraph(_escape(unit.name), styles["group"])) + for group in groups: + tile_w, chunks = _layout(group.columns, group.optical, width) + panels = [PortPanel(chunk, group.optical, width, tile_w) for chunk in chunks] + title = Paragraph(f"{_escape(group.title)} · {len(group.ports)} {t['ports']}", styles["group"]) + block = [title, Spacer(0, 0.8 * mm)] + for panel in panels: + block.extend([panel, Spacer(0, 1.2 * mm)]) + story.append(KeepTogether(block)) + story.append(Spacer(0, 1.5 * mm)) # Keep the explanation and the VLAN legend on one page. story.append( @@ -433,7 +441,13 @@ def build_pdf(device, ports, lang="de", generated=None): ) story.append(CondPageBreak(40 * mm)) story.append(Paragraph(t["list"], styles["h2"])) - story.append(_port_table(ports, t, styles)) + if members: + for unit, unit_ports in members: + story.append(Paragraph(_escape(unit.name), styles["group"])) + story.append(_port_table(unit_ports, t, styles)) + story.append(Spacer(0, 2 * mm)) + else: + story.append(_port_table(ports, t, styles)) document.build(story, onFirstPage=footer, onLaterPages=footer) return buffer.getvalue() diff --git a/netbox_utilities/switch_port_views.py b/netbox_utilities/switch_port_views.py index 4bd065c..1084fb6 100644 --- a/netbox_utilities/switch_port_views.py +++ b/netbox_utilities/switch_port_views.py @@ -131,10 +131,18 @@ class SwitchPortsPDFView(ContentTypePermissionRequiredMixin, View): device = get_object_or_404( Device.objects.restrict(request.user, "view").select_related("site", "rack", "device_type"), pk=pk ) - interfaces = interface_queryset(request, device) + from .switch_stack import stack_members + lang = switch_port_pdf.language(translation.get_language()) + stack = [device, *stack_members(device)] + units = [(device_info(unit), collect_ports(interface_queryset(request, unit))) for unit in stack] + ports = [port for _, unit_ports in units for port in unit_ports] pdf = switch_port_pdf.build_pdf( - device_info(device), collect_ports(interfaces), lang=lang, generated=timezone.localtime() + device_info(device), + ports, + lang=lang, + generated=timezone.localtime(), + members=units if len(stack) > 1 else None, ) filename = f"{switch_port_pdf.TEXTS[lang]['filename']}-{device.name or device.pk}.pdf".replace(" ", "_") response = HttpResponse(pdf, content_type="application/pdf") diff --git a/netbox_utilities/switch_stack.py b/netbox_utilities/switch_stack.py index eaa94f2..edd18c8 100644 --- a/netbox_utilities/switch_stack.py +++ b/netbox_utilities/switch_stack.py @@ -15,7 +15,7 @@ def stack_members(device): stack = SwitchStack.objects.filter(device=device).first() if stack is None: return [] - members = stack.members.exclude(pk=device.pk).select_related("role") + members = stack.members.exclude(pk=device.pk).select_related("role", "site", "rack", "device_type") return sorted(members, key=lambda member: natural_key(member.name or str(member))) diff --git a/pyproject.toml b/pyproject.toml index 82b2289..c8c7d58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "netbox-utilities" -version = "0.21.0" +version = "0.22.0" description = "Navigation, tenant utilities, connection VLANs, partial-width racks, disks, CPU & RAM, switch port export and bulk operations for NetBox 4.6/4.7" readme = "README.md" requires-python = ">=3.12"