Files
Netbox-Utilities/netbox_utilities/switch_port_views.py
T
MrBlakeandClaude Opus 5.5 8eb075db9f feat: export switch port usage as PDF instead of a Ports tab
Switches get an "Export switch port usage" button that downloads an A4
landscape PDF: key figures, a front panel with one colour per VLAN label
(full colour = connected, pale = free, dashed with a cross = disabled,
text colour chosen for contrast), a VLAN legend and the full port list
with peers. The PDF follows the language of the user's NetBox
preferences (German or English). The Ports tab with its script and
styles is removed. ReportLab becomes a dependency.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 09:25:29 +01:00

92 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from dcim.models import Device, Interface
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils import timezone, translation
from django.views import View
from utilities.views import ContentTypePermissionRequiredMixin
from . import switch_port_pdf, switch_ports
def _describe(endpoint):
device = getattr(endpoint, "device", None) or getattr(endpoint, "parent_object", None)
if device is not None and device != endpoint:
return f"{device} – {endpoint}"
return str(endpoint)
def _peer(interface):
"""Far end through patch panels if the path is complete, otherwise the directly cabled object."""
try:
endpoints = interface.connected_endpoints if interface._path_id else []
except Exception: # noqa: BLE001 - a broken path must not break the export
endpoints = []
if not endpoints and interface.cable_id:
endpoints = interface.link_peers
return ", ".join(_describe(endpoint) for endpoint in endpoints or [])
def collect_ports(interfaces):
ports = []
for interface in interfaces:
peer = _peer(interface)
if not peer and interface.mark_connected:
peer = switch_ports.MARKED_CONNECTED
ports.append(
switch_ports.Port(
pk=interface.pk,
name=interface.name,
label=interface.label,
type=interface.type,
type_display=str(interface.get_type_display()),
enabled=interface.enabled,
connected=bool(interface.cable_id or interface.mark_connected),
mgmt_only=interface.mgmt_only,
description=interface.description,
peer=peer,
lag=interface.lag.name if interface.lag_id else "",
virtual=interface.type in switch_ports.VIRTUAL_TYPES,
)
)
return ports
def device_info(device):
rack = ""
if device.rack_id:
rack = str(device.rack)
if device.position:
rack += f" / U{device.position:g}"
return switch_port_pdf.DeviceInfo(
name=device.name or str(device),
site=str(device.site) if device.site_id else "",
rack=rack,
model=str(device.device_type),
)
class SwitchPortsPDFView(ContentTypePermissionRequiredMixin, View):
"""PDF of the port usage of a switch, in the language of the user's NetBox preferences."""
def get_required_permission(self):
return "dcim.view_interface"
def get(self, request, pk):
device = get_object_or_404(
Device.objects.restrict(request.user, "view").select_related("site", "rack", "device_type"), pk=pk
)
interfaces = (
Interface.objects.restrict(request.user, "view")
.filter(device=device)
.select_related("lag", "cable", "_path")
.order_by("_name")
)
lang = switch_port_pdf.language(translation.get_language())
pdf = switch_port_pdf.build_pdf(
device_info(device), collect_ports(interfaces), lang=lang, generated=timezone.localtime()
)
filename = f"{switch_port_pdf.TEXTS[lang]['filename']}-{device.name or device.pk}.pdf".replace(" ", "_")
response = HttpResponse(pdf, content_type="application/pdf")
response["Content-Disposition"] = f'attachment; filename="{filename}"'
return response