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 <[email protected]>
This commit is contained in:
1 parent
f86306e686
commit
204bb235a4
4 files changed
+54
-7
No files matched your search
@@ -1,6 +1,6 @@
|
||||
from netbox.plugins import PluginConfig, get_plugin_config
|
||||
|
||||
__version__ = "0.27.0"
|
||||
__version__ = "0.27.1"
|
||||
|
||||
|
||||
class NetBoxUtilitiesConfig(PluginConfig):
|
||||
|
||||
@@ -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('<a href="{}">{}</a>', 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)
|
||||
@@ -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()))
|
||||
+2
-2
@@ -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 = [
|
||||
|
||||
Reference in new issue
Block a user