fix: global tenant filter includes assets with inherited tenant
The tenant_id and tenant_group_id filters of netbox-inventory assets now also match assets without their own tenant via the tenant of the assigned device, module, inventory item or rack, like the tenant column. The global tenant and tenant group filter uses these filters and no longer hides such assets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
204bb235a4
commit
bfcfff60e4
4 files changed
+57
-2
No files matched your search
@@ -1,6 +1,6 @@
|
||||
from netbox.plugins import PluginConfig, get_plugin_config
|
||||
|
||||
__version__ = "0.27.1"
|
||||
__version__ = "0.27.2"
|
||||
|
||||
|
||||
class NetBoxUtilitiesConfig(PluginConfig):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import django_tables2 as tables
|
||||
from django.db.models import Q
|
||||
from django.utils.html import format_html
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@@ -33,6 +34,48 @@ class AssetTenantColumn(tables.Column):
|
||||
return format_html('<a href="{}">{}</a>', url, tenant) if url else str(tenant)
|
||||
|
||||
|
||||
def asset_tenant_query(tenant_ids):
|
||||
"""Assets whose own tenant, or (without own tenant) whose hardware's tenant, is one of tenant_ids."""
|
||||
inherited = Q()
|
||||
for path in ("device", "module__device", "inventoryitem__device", "rack"):
|
||||
inherited |= Q(**{f"{path}__tenant_id__in": tenant_ids})
|
||||
return Q(tenant_id__in=tenant_ids) | (Q(tenant__isnull=True) & inherited)
|
||||
|
||||
|
||||
def _ids(values):
|
||||
return [int(getattr(value, "pk", value)) for value in values or () if value not in (None, "")]
|
||||
|
||||
|
||||
def filter_asset_tenant(queryset, name, value):
|
||||
tenant_ids = _ids(value)
|
||||
return queryset.filter(asset_tenant_query(tenant_ids)) if tenant_ids else queryset
|
||||
|
||||
|
||||
def filter_asset_tenant_group(queryset, name, value):
|
||||
from tenancy.models import Tenant, TenantGroup
|
||||
|
||||
group_ids = set()
|
||||
for group in TenantGroup.objects.filter(pk__in=_ids(value)):
|
||||
group_ids.update(group.get_descendants(include_self=True).values_list("pk", flat=True))
|
||||
if not group_ids:
|
||||
return queryset
|
||||
tenant_ids = list(Tenant.objects.filter(group_id__in=group_ids).values_list("pk", flat=True))
|
||||
return queryset.filter(asset_tenant_query(tenant_ids))
|
||||
|
||||
|
||||
def install_asset_tenant_filters():
|
||||
"""Tenant filters (also used by the global tenant filter) include inherited tenants."""
|
||||
from netbox_inventory.filtersets import AssetFilterSet
|
||||
from utilities.filters import MultiValueNumberFilter
|
||||
|
||||
AssetFilterSet.base_filters["tenant_id"] = MultiValueNumberFilter(
|
||||
method=filter_asset_tenant, label="Tenant (ID), including the tenant of the assigned hardware"
|
||||
)
|
||||
AssetFilterSet.base_filters["tenant_group_id"] = MultiValueNumberFilter(
|
||||
method=filter_asset_tenant_group, label="Tenant group (ID), including the tenant of the assigned hardware"
|
||||
)
|
||||
|
||||
|
||||
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]
|
||||
@@ -57,3 +100,4 @@ def install_asset_tenant_column():
|
||||
AssetTable.base_columns.pop(COLUMN_NAME, None)
|
||||
register_table_column(column, COLUMN_NAME, AssetTable)
|
||||
add_to_default_columns(AssetTable)
|
||||
install_asset_tenant_filters()
|
||||
@@ -38,3 +38,14 @@ class AssetTenantFallbackTest(SimpleTestCase):
|
||||
module = SimpleNamespace(device=SimpleNamespace(tenant="C"))
|
||||
self.assertEqual(asset_tenant(asset(module=module)), "C")
|
||||
self.assertIsNone(asset_tenant(asset()))
|
||||
|
||||
|
||||
class AssetTenantQueryTest(SimpleTestCase):
|
||||
def test_query_includes_inherited_tenants_only_without_own_tenant(self):
|
||||
from netbox_utilities.inventory_assets import asset_tenant_query
|
||||
|
||||
query = str(asset_tenant_query([7]))
|
||||
|
||||
for lookup in ("tenant_id__in", "tenant__isnull", "device__tenant_id__in", "module__device__tenant_id__in",
|
||||
"inventoryitem__device__tenant_id__in", "rack__tenant_id__in"):
|
||||
self.assertIn(f"'{lookup}'", query)
|
||||
+1
-1
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||
|
||||
[project]
|
||||
name = "netbox-utilities"
|
||||
version = "0.27.1"
|
||||
version = "0.27.2"
|
||||
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"
|
||||
|
||||
Reference in new issue
Block a user