feat: integrate site prefix column from Netbox-Prefixes
Device and rack tables show the first word of the site name as the first data column. Replaces the separate netbox_site_prefix plugin and can be disabled with site_prefix_column_enabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -21,6 +21,7 @@ class NetBoxUtilitiesConfig(PluginConfig):
|
||||
"tenant_filter_enabled": True,
|
||||
"tenant_required": True,
|
||||
"offline_svg_export_enabled": True,
|
||||
"site_prefix_column_enabled": True,
|
||||
}
|
||||
middleware = [
|
||||
"netbox_utilities.middleware.GlobalTenantFilterMiddleware",
|
||||
@@ -37,6 +38,10 @@ class NetBoxUtilitiesConfig(PluginConfig):
|
||||
install_tenant_validation()
|
||||
install_patchpanel_automation()
|
||||
install_rack_width_support()
|
||||
if get_plugin_config("netbox_utilities", "site_prefix_column_enabled"):
|
||||
from .site_prefix import install_site_prefix_column
|
||||
|
||||
install_site_prefix_column()
|
||||
if get_plugin_config("netbox_utilities", "connection_vlans_enabled"):
|
||||
from .connection_vlans import install_connection_vlan_support
|
||||
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import django_tables2 as tables
|
||||
|
||||
COLUMN_NAME = "site_prefix"
|
||||
|
||||
|
||||
def first_word(value):
|
||||
"""Return the first whitespace-delimited word of a value."""
|
||||
words = str(value or "").split()
|
||||
return words[0] if words else None
|
||||
|
||||
|
||||
class SitePrefixColumn(tables.Column):
|
||||
"""Render the first word of a record's site name."""
|
||||
|
||||
def render(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
return first_word(value.name)
|
||||
|
||||
|
||||
def prepend_to_default_columns(table):
|
||||
"""Show the prefix column first for users without saved table preferences."""
|
||||
defaults = tuple(table.Meta.default_columns)
|
||||
table.Meta.default_columns = (
|
||||
"pk",
|
||||
COLUMN_NAME,
|
||||
*(name for name in defaults if name not in {"pk", COLUMN_NAME}),
|
||||
)
|
||||
|
||||
|
||||
def install_site_prefix_column():
|
||||
from dcim.tables import DeviceTable, RackTable
|
||||
from utilities.tables import register_table_column
|
||||
|
||||
column = SitePrefixColumn(
|
||||
accessor=tables.A("site"),
|
||||
verbose_name="Prefix",
|
||||
empty_values=(),
|
||||
orderable=False,
|
||||
)
|
||||
register_table_column(column, COLUMN_NAME, DeviceTable, RackTable)
|
||||
prepend_to_default_columns(DeviceTable)
|
||||
prepend_to_default_columns(RackTable)
|
||||
@@ -0,0 +1,36 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from netbox_utilities.site_prefix import COLUMN_NAME, SitePrefixColumn, first_word, prepend_to_default_columns
|
||||
|
||||
|
||||
class SitePrefixTest(SimpleTestCase):
|
||||
def test_first_word(self):
|
||||
self.assertEqual(first_word("Berlin Campus West"), "Berlin")
|
||||
self.assertEqual(first_word(" DC01\tFrankfurt "), "DC01")
|
||||
|
||||
def test_first_word_handles_missing_or_blank_value(self):
|
||||
self.assertIsNone(first_word(None))
|
||||
self.assertIsNone(first_word(" "))
|
||||
|
||||
def test_column_renders_first_word_of_site_name(self):
|
||||
column = SitePrefixColumn()
|
||||
|
||||
self.assertEqual(column.render(SimpleNamespace(name="DC01 Frankfurt")), "DC01")
|
||||
self.assertIsNone(column.render(None))
|
||||
|
||||
def test_prefix_column_is_first_default_column(self):
|
||||
table = SimpleNamespace(Meta=SimpleNamespace(default_columns=("pk", "name", COLUMN_NAME, "status")))
|
||||
|
||||
prepend_to_default_columns(table)
|
||||
|
||||
self.assertEqual(table.Meta.default_columns, ("pk", COLUMN_NAME, "name", "status"))
|
||||
|
||||
def test_device_and_rack_tables_offer_prefix_column(self):
|
||||
from dcim.tables import DeviceTable, RackTable
|
||||
|
||||
self.assertIn(COLUMN_NAME, DeviceTable.base_columns)
|
||||
self.assertIn(COLUMN_NAME, RackTable.base_columns)
|
||||
self.assertEqual(DeviceTable.Meta.default_columns[:2], ("pk", COLUMN_NAME))
|
||||
self.assertEqual(RackTable.Meta.default_columns[:2], ("pk", COLUMN_NAME))
|
||||
Reference in New Issue
Block a user