From d2ffc69db515fdc4d0fc57c44fd1b7902f442147 Mon Sep 17 00:00:00 2001 From: Alexander Haase Date: Sun, 28 Apr 2024 22:28:35 +0200 Subject: [PATCH 1/2] Add device and module type columns A linked count column added to the device and module type list tables allows for quick access to the associated assets, while providing an overview of the total assets managed as opposed to instances in use. This also allows both columns to be exported as CSV. --- netbox_inventory/tables.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/netbox_inventory/tables.py b/netbox_inventory/tables.py index ed4b84f..1efc554 100644 --- a/netbox_inventory/tables.py +++ b/netbox_inventory/tables.py @@ -1,11 +1,15 @@ from django.db.models.functions import Coalesce import django_tables2 as tables +from django.utils.translation import gettext_lazy as _ from netbox.tables import columns, NetBoxTable from tenancy.tables import ContactsColumnMixin from .models import Asset, Delivery, InventoryItemType, InventoryItemGroup, Purchase, Supplier from .template_content import WARRANTY_PROGRESSBAR +from dcim.tables import DeviceTypeTable, ModuleTypeTable +from utilities.tables import register_table_column + __all__ = ( 'AssetTable', 'SupplierTable', @@ -486,3 +490,27 @@ class Meta(NetBoxTable.Meta): 'asset_count', 'inventoryitem_type_count', ) + + +# ======================== +# DCIM model table columns +# ======================== + +asset_count = columns.LinkedCountColumn( + viewname='plugins:netbox_inventory:asset_list', + url_params={'device_type_id': 'pk'}, + verbose_name=_('Assets'), + accessor="assets__count", +) + +register_table_column(asset_count, 'assets', DeviceTypeTable) + + +asset_count = columns.LinkedCountColumn( + viewname='plugins:netbox_inventory:asset_list', + url_params={'module_type_id': 'pk'}, + verbose_name=_('Assets'), + accessor="assets__count", +) + +register_table_column(asset_count, 'assets', ModuleTypeTable) From b104dc8336221c6a414ca9ec4cb51c1eaa84e981 Mon Sep 17 00:00:00 2001 From: Alexander Haase Date: Wed, 29 May 2024 08:45:15 +0200 Subject: [PATCH 2/2] Add reverse relations for assets --- netbox_inventory/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/netbox_inventory/models.py b/netbox_inventory/models.py index 9c6b041..c3301f3 100644 --- a/netbox_inventory/models.py +++ b/netbox_inventory/models.py @@ -65,7 +65,7 @@ class Asset(NetBoxModel, ImageAttachmentsMixin): device_type = models.ForeignKey( to='dcim.DeviceType', on_delete=models.PROTECT, - related_name='+', + related_name='assets', blank=True, null=True, verbose_name='Device Type', @@ -73,7 +73,7 @@ class Asset(NetBoxModel, ImageAttachmentsMixin): module_type = models.ForeignKey( to='dcim.ModuleType', on_delete=models.PROTECT, - related_name='+', + related_name='assets', blank=True, null=True, verbose_name='Module Type',