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 <[email protected]>
This commit is contained in:
1 parent
567daf7963
commit
ce174c6541
5 files changed
+41
-19
No files matched your search
@@ -1,6 +1,6 @@
|
||||
from netbox.plugins import PluginConfig, get_plugin_config
|
||||
|
||||
__version__ = "0.21.0"
|
||||
__version__ = "0.22.0"
|
||||
|
||||
|
||||
class NetBoxUtilitiesConfig(PluginConfig):
|
||||
|
||||
@@ -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()
|
||||
@@ -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")
|
||||
|
||||
@@ -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)))
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user