From 204bb235a46d7a81fa4e2862f7c00ebbc3c9991e Mon Sep 17 00:00:00 2001 From: Louis Date: Wed, 18 Nov 2026 12:54:57 +0100 Subject: [PATCH] fix: asset tenant column falls back to the hardware's tenant The tenant column of the netbox-inventory asset list stayed empty for assets without their own tenant. It now shows the tenant of the assigned device, module, inventory item or rack instead. reportlab is pinned below 5, which pulled in pillow 13 and broke netbox-documentation's pillow<13 requirement. Co-Authored-By: Claude Opus 5.5 --- netbox_utilities/__init__.py | 2 +- netbox_utilities/inventory_assets.py | 40 +++++++++++++++++-- .../tests/test_inventory_assets.py | 15 +++++++ pyproject.toml | 4 +- 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/netbox_utilities/__init__.py b/netbox_utilities/__init__.py index 58c3b4d..c145258 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.27.0" +__version__ = "0.27.1" class NetBoxUtilitiesConfig(PluginConfig): diff --git a/netbox_utilities/inventory_assets.py b/netbox_utilities/inventory_assets.py index dc46bfc..8b6319e 100644 --- a/netbox_utilities/inventory_assets.py +++ b/netbox_utilities/inventory_assets.py @@ -1,9 +1,38 @@ import django_tables2 as tables +from django.utils.html import format_html from django.utils.translation import gettext_lazy as _ +from .tenant_autofill import ASSET_HARDWARE_RELATIONS + COLUMN_NAME = "tenant" +def asset_tenant(asset): + """Tenant of the asset itself, otherwise the tenant of its assigned hardware (or that hardware's device).""" + if tenant := getattr(asset, "tenant", None): + return tenant + for relation in ASSET_HARDWARE_RELATIONS: + hardware = getattr(asset, relation, None) + if hardware is None: + continue + if tenant := getattr(hardware, "tenant", None): + return tenant + if tenant := getattr(getattr(hardware, "device", None), "tenant", None): + return tenant + return None + + +class AssetTenantColumn(tables.Column): + """Asset tenant, falling back to the tenant of the assigned device, module, inventory item or rack.""" + + def render(self, record): + tenant = asset_tenant(record) + if tenant is None: + return "—" + url = tenant.get_absolute_url() if hasattr(tenant, "get_absolute_url") else None + return format_html('{}', url, tenant) if url else str(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] @@ -19,9 +48,12 @@ def add_to_default_columns(table, column=COLUMN_NAME, after=("status", "asset_ta def install_asset_tenant_column(): """Show the tenant of every netbox-inventory asset in the asset list.""" from netbox_inventory.tables import AssetTable + from utilities.tables import register_table_column - 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) + column = AssetTenantColumn( + accessor=tables.A("pk"), verbose_name=_("Tenant"), empty_values=(), order_by=("tenant__name",) + ) + # Replaces the plugin's own column, which stays empty for assets that only inherit a tenant. + AssetTable.base_columns.pop(COLUMN_NAME, None) + register_table_column(column, 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 index 0f1f9b5..310914b 100644 --- a/netbox_utilities/tests/test_inventory_assets.py +++ b/netbox_utilities/tests/test_inventory_assets.py @@ -23,3 +23,18 @@ class AssetTenantColumnTest(SimpleTestCase): add_to_default_columns(table) self.assertEqual(table.Meta.default_columns, ("pk", "name", "tenant")) + + +class AssetTenantFallbackTest(SimpleTestCase): + def test_own_tenant_wins_then_hardware_then_hardware_device(self): + from netbox_utilities.inventory_assets import asset_tenant + + def asset(**kwargs): + values = dict(tenant=None, device=None, module=None, inventoryitem=None, rack=None) + return SimpleNamespace(**{**values, **kwargs}) + + self.assertEqual(asset_tenant(asset(tenant="A", device=SimpleNamespace(tenant="B"))), "A") + self.assertEqual(asset_tenant(asset(device=SimpleNamespace(tenant="B"))), "B") + module = SimpleNamespace(device=SimpleNamespace(tenant="C")) + self.assertEqual(asset_tenant(asset(module=module)), "C") + self.assertIsNone(asset_tenant(asset())) diff --git a/pyproject.toml b/pyproject.toml index 5efaefd..3937da3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,12 +4,12 @@ build-backend = "hatchling.build" [project] name = "netbox-utilities" -version = "0.27.0" +version = "0.27.1" 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" dependencies = [ - "reportlab>=4.0", + "reportlab>=4.0,<5", ] license = { text = "MIT" } authors = [