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
|
from netbox.plugins import PluginConfig, get_plugin_config
|
||||||
|
|
||||||
__version__ = "0.21.0"
|
__version__ = "0.22.0"
|
||||||
|
|
||||||
|
|
||||||
class NetBoxUtilitiesConfig(PluginConfig):
|
class NetBoxUtilitiesConfig(PluginConfig):
|
||||||
|
|||||||
@@ -372,8 +372,12 @@ def _port_table(ports, t, styles):
|
|||||||
return table
|
return table
|
||||||
|
|
||||||
|
|
||||||
def build_pdf(device, ports, lang="de", generated=None):
|
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."""
|
"""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)]
|
t = TEXTS[language(lang)]
|
||||||
styles = _styles()
|
styles = _styles()
|
||||||
generated = generated or datetime.now()
|
generated = generated or datetime.now()
|
||||||
@@ -414,18 +418,22 @@ def build_pdf(device, ports, lang="de", generated=None):
|
|||||||
]
|
]
|
||||||
|
|
||||||
switch_ports.assign_vlan_colors(ports)
|
switch_ports.assign_vlan_colors(ports)
|
||||||
groups = switch_ports.build_groups(ports)
|
units = members or [(device, ports)]
|
||||||
if not groups:
|
if not any(switch_ports.build_groups(unit_ports) for _, unit_ports in units):
|
||||||
story.append(Paragraph(t["none"], styles["meta"]))
|
story.append(Paragraph(t["none"], styles["meta"]))
|
||||||
for group in groups:
|
for unit, unit_ports in units:
|
||||||
tile_w, chunks = _layout(group.columns, group.optical, width)
|
groups = switch_ports.build_groups(unit_ports)
|
||||||
panels = [PortPanel(chunk, group.optical, width, tile_w) for chunk in chunks]
|
if members:
|
||||||
title = Paragraph(f"{_escape(group.title)} · {len(group.ports)} {t['ports']}", styles["group"])
|
story.append(Paragraph(_escape(unit.name), styles["group"]))
|
||||||
block = [title, Spacer(0, 0.8 * mm)]
|
for group in groups:
|
||||||
for panel in panels:
|
tile_w, chunks = _layout(group.columns, group.optical, width)
|
||||||
block.extend([panel, Spacer(0, 1.2 * mm)])
|
panels = [PortPanel(chunk, group.optical, width, tile_w) for chunk in chunks]
|
||||||
story.append(KeepTogether(block))
|
title = Paragraph(f"{_escape(group.title)} · {len(group.ports)} {t['ports']}", styles["group"])
|
||||||
story.append(Spacer(0, 1.5 * mm))
|
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.
|
# Keep the explanation and the VLAN legend on one page.
|
||||||
story.append(
|
story.append(
|
||||||
@@ -433,7 +441,13 @@ def build_pdf(device, ports, lang="de", generated=None):
|
|||||||
)
|
)
|
||||||
story.append(CondPageBreak(40 * mm))
|
story.append(CondPageBreak(40 * mm))
|
||||||
story.append(Paragraph(t["list"], styles["h2"]))
|
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)
|
document.build(story, onFirstPage=footer, onLaterPages=footer)
|
||||||
return buffer.getvalue()
|
return buffer.getvalue()
|
||||||
@@ -131,10 +131,18 @@ class SwitchPortsPDFView(ContentTypePermissionRequiredMixin, View):
|
|||||||
device = get_object_or_404(
|
device = get_object_or_404(
|
||||||
Device.objects.restrict(request.user, "view").select_related("site", "rack", "device_type"), pk=pk
|
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())
|
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(
|
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(" ", "_")
|
filename = f"{switch_port_pdf.TEXTS[lang]['filename']}-{device.name or device.pk}.pdf".replace(" ", "_")
|
||||||
response = HttpResponse(pdf, content_type="application/pdf")
|
response = HttpResponse(pdf, content_type="application/pdf")
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def stack_members(device):
|
|||||||
stack = SwitchStack.objects.filter(device=device).first()
|
stack = SwitchStack.objects.filter(device=device).first()
|
||||||
if stack is None:
|
if stack is None:
|
||||||
return []
|
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)))
|
return sorted(members, key=lambda member: natural_key(member.name or str(member)))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "netbox-utilities"
|
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"
|
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"
|
readme = "README.md"
|
||||||
requires-python = ">=3.12"
|
requires-python = ">=3.12"
|
||||||
|
|||||||
Reference in new issue
Block a user