feat: add site prefix column to device and rack tables
- derive prefix from the first word of the site name - display prefix as the first data column - support NetBox 4.6.5 through 4.6.x - add packaging, installation documentation, and tests
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
from netbox.plugins import PluginConfig
|
||||
|
||||
|
||||
class SitePrefixConfig(PluginConfig):
|
||||
name = "netbox_site_prefix"
|
||||
verbose_name = "Site Prefix"
|
||||
author = "LKE"
|
||||
description = "Shows the first word of the site name in device and rack tables."
|
||||
version = "1.0.0"
|
||||
min_version = "4.6.5"
|
||||
max_version = "4.6.99"
|
||||
|
||||
def ready(self):
|
||||
super().ready()
|
||||
|
||||
# Importing the module registers its columns with NetBox.
|
||||
from . import tables # noqa: F401
|
||||
|
||||
|
||||
config = SitePrefixConfig
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import django_tables2 as tables
|
||||
|
||||
from dcim.tables import DeviceTable, RackTable
|
||||
from utilities.tables import register_table_column
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
site_prefix = SitePrefixColumn(
|
||||
accessor=tables.A("site"),
|
||||
verbose_name="Prefix",
|
||||
empty_values=(),
|
||||
orderable=False,
|
||||
)
|
||||
|
||||
register_table_column(site_prefix, COLUMN_NAME, DeviceTable, RackTable)
|
||||
|
||||
|
||||
def _prepend_to_defaults(table):
|
||||
"""Show the registered column first for users without saved 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}),
|
||||
)
|
||||
|
||||
|
||||
_prepend_to_defaults(DeviceTable)
|
||||
_prepend_to_defaults(RackTable)
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
urlpatterns = []
|
||||
|
||||
Reference in New Issue
Block a user