diff --git a/README.md b/README.md index d06e113..1b5257c 100644 --- a/README.md +++ b/README.md @@ -790,7 +790,9 @@ a tenant takes the tenant of the device, module, inventory item or rack it is assigned to when it is saved. This keeps the plugin's *assign*, *reassign* and *create hardware from asset* views working with mandatory tenancy, which save the asset without a tenant field. An asset that is in stock and has no -assigned hardware still needs a tenant. +assigned hardware still needs a tenant. The asset list shows a **Tenant** +column next to the status by default; users with a saved table configuration +can add it via *Configure Table*. `tenant_required = False` disables the feature permanently and takes precedence over the UI setting. diff --git a/netbox_utilities/__init__.py b/netbox_utilities/__init__.py index 98e305e..58c3b4d 100644 --- a/netbox_utilities/__init__.py +++ b/netbox_utilities/__init__.py @@ -1,6 +1,6 @@ from netbox.plugins import PluginConfig, get_plugin_config -__version__ = "0.26.2" +__version__ = "0.27.0" class NetBoxUtilitiesConfig(PluginConfig): @@ -76,6 +76,10 @@ class NetBoxUtilitiesConfig(PluginConfig): from .netbox_export import install_netbox_export_rack_width_support install_netbox_export_rack_width_support() + if self.apps.is_installed("netbox_inventory"): + from .inventory_assets import install_asset_tenant_column + + install_asset_tenant_column() if get_plugin_config("netbox_utilities", "reorder_rack_bulk_save_enabled"): from .reorder_rack import install_reorder_rack_bulk_save diff --git a/netbox_utilities/inventory_assets.py b/netbox_utilities/inventory_assets.py new file mode 100644 index 0000000..dc46bfc --- /dev/null +++ b/netbox_utilities/inventory_assets.py @@ -0,0 +1,27 @@ +import django_tables2 as tables +from django.utils.translation import gettext_lazy as _ + +COLUMN_NAME = "tenant" + + +def add_to_default_columns(table, column=COLUMN_NAME, after=("status", "asset_tag", "hardware_type")): + """Show the column by default (users without saved table preferences), next to the status.""" + defaults = [name for name in table.Meta.default_columns if name != column] + for anchor in after: + if anchor in defaults: + defaults.insert(defaults.index(anchor) + 1, column) + break + else: + defaults.append(column) + table.Meta.default_columns = tuple(defaults) + + +def install_asset_tenant_column(): + """Show the tenant of every netbox-inventory asset in the asset list.""" + from netbox_inventory.tables import AssetTable + + if COLUMN_NAME not in AssetTable.base_columns: + from utilities.tables import register_table_column + + register_table_column(tables.Column(linkify=True, verbose_name=_("Tenant")), COLUMN_NAME, AssetTable) + add_to_default_columns(AssetTable) diff --git a/netbox_utilities/tests/test_inventory_assets.py b/netbox_utilities/tests/test_inventory_assets.py new file mode 100644 index 0000000..0f1f9b5 --- /dev/null +++ b/netbox_utilities/tests/test_inventory_assets.py @@ -0,0 +1,25 @@ +from types import SimpleNamespace + +from django.test import SimpleTestCase + +from netbox_utilities.inventory_assets import add_to_default_columns + + +def fake_table(*default_columns): + return SimpleNamespace(Meta=SimpleNamespace(default_columns=default_columns)) + + +class AssetTenantColumnTest(SimpleTestCase): + def test_tenant_is_shown_after_status(self): + table = fake_table("pk", "id", "name", "asset_tag", "status", "hardware", "tags") + + add_to_default_columns(table) + + self.assertEqual(table.Meta.default_columns, ("pk", "id", "name", "asset_tag", "status", "tenant", "hardware", "tags")) + + def test_tenant_is_not_duplicated_and_appended_without_anchor(self): + table = fake_table("pk", "tenant", "name") + + add_to_default_columns(table) + + self.assertEqual(table.Meta.default_columns, ("pk", "name", "tenant")) diff --git a/pyproject.toml b/pyproject.toml index d240383..5efaefd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "netbox-utilities" -version = "0.26.2" +version = "0.27.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"