diff --git a/netbox/circuits/tables.py b/netbox/circuits/tables.py
index e4b859d3c..efa7e4c49 100644
--- a/netbox/circuits/tables.py
+++ b/netbox/circuits/tables.py
@@ -1,7 +1,7 @@
import django_tables2 as tables
from django_tables2.utils import Accessor
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
from utilities.tables import BaseTable, ButtonsColumn, ChoiceFieldColumn, TagColumn, ToggleColumn
from .models import Circuit, CircuitType, Provider
@@ -60,9 +60,7 @@ class CircuitTable(BaseTable):
linkify=True
)
status = ChoiceFieldColumn()
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
a_side = tables.Column(
verbose_name='A Side'
)
diff --git a/netbox/dcim/tables/devices.py b/netbox/dcim/tables/devices.py
index c844bb004..8a3944a28 100644
--- a/netbox/dcim/tables/devices.py
+++ b/netbox/dcim/tables/devices.py
@@ -5,7 +5,7 @@ from dcim.models import (
ConsolePort, ConsoleServerPort, Device, DeviceBay, DeviceRole, FrontPort, Interface, InventoryItem, Platform,
PowerOutlet, PowerPort, RearPort, VirtualChassis,
)
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
from utilities.tables import (
BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn,
TagColumn, ToggleColumn,
@@ -109,9 +109,7 @@ class DeviceTable(BaseTable):
template_code=DEVICE_LINK
)
status = ChoiceFieldColumn()
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
site = tables.Column(
linkify=True
)
@@ -178,9 +176,7 @@ class DeviceImportTable(BaseTable):
template_code=DEVICE_LINK
)
status = ChoiceFieldColumn()
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
site = tables.Column(
linkify=True
)
diff --git a/netbox/dcim/tables/racks.py b/netbox/dcim/tables/racks.py
index 5cb513faa..11fc946ca 100644
--- a/netbox/dcim/tables/racks.py
+++ b/netbox/dcim/tables/racks.py
@@ -2,7 +2,7 @@ import django_tables2 as tables
from django_tables2.utils import Accessor
from dcim.models import Rack, Location, RackReservation, RackRole
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
from utilities.tables import (
BaseTable, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, TagColumn,
ToggleColumn,
@@ -79,9 +79,7 @@ class RackTable(BaseTable):
site = tables.Column(
linkify=True
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
status = ChoiceFieldColumn()
role = ColoredLabelColumn()
u_height = tables.TemplateColumn(
@@ -143,9 +141,7 @@ class RackReservationTable(BaseTable):
accessor=Accessor('rack__site'),
linkify=True
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
rack = tables.Column(
linkify=True
)
diff --git a/netbox/dcim/tables/sites.py b/netbox/dcim/tables/sites.py
index 50a5e5ec7..58d661fa9 100644
--- a/netbox/dcim/tables/sites.py
+++ b/netbox/dcim/tables/sites.py
@@ -1,7 +1,7 @@
import django_tables2 as tables
from dcim.models import Region, Site
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
from utilities.tables import BaseTable, ButtonsColumn, ChoiceFieldColumn, TagColumn, ToggleColumn
from .template_code import MPTT_LINK
@@ -46,9 +46,7 @@ class SiteTable(BaseTable):
region = tables.Column(
linkify=True
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
tags = TagColumn(
url_name='dcim:site_list'
)
diff --git a/netbox/ipam/tables.py b/netbox/ipam/tables.py
index 8101a581d..779b37804 100644
--- a/netbox/ipam/tables.py
+++ b/netbox/ipam/tables.py
@@ -3,7 +3,7 @@ from django.utils.safestring import mark_safe
from django_tables2.utils import Accessor
from dcim.models import Interface
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
from utilities.tables import (
BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, LinkedCountColumn, TagColumn, ToggleColumn,
)
@@ -109,16 +109,6 @@ VLAN_MEMBER_TAGGED = """
{% endif %}
"""
-TENANT_LINK = """
-{% if record.tenant %}
- {{ record.tenant }}
-{% elif record.vrf.tenant %}
- {{ record.vrf.tenant }}*
-{% else %}
- —
-{% endif %}
-"""
-
#
# VRFs
@@ -130,9 +120,7 @@ class VRFTable(BaseTable):
rd = tables.Column(
verbose_name='RD'
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
enforce_unique = BooleanColumn(
verbose_name='Unique'
)
@@ -163,9 +151,7 @@ class VRFTable(BaseTable):
class RouteTargetTable(BaseTable):
pk = ToggleColumn()
name = tables.LinkColumn()
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
tags = TagColumn(
url_name='ipam:vrf_list'
)
@@ -208,9 +194,7 @@ class AggregateTable(BaseTable):
prefix = tables.LinkColumn(
verbose_name='Aggregate'
)
- tenant = tables.TemplateColumn(
- template_code=TENANT_LINK
- )
+ tenant = TenantColumn()
date_added = tables.DateColumn(
format="Y-m-d",
verbose_name='Added'
@@ -279,9 +263,7 @@ class PrefixTable(BaseTable):
template_code=VRF_LINK,
verbose_name='VRF'
)
- tenant = tables.TemplateColumn(
- template_code=TENANT_LINK
- )
+ tenant = TenantColumn()
site = tables.Column(
linkify=True
)
@@ -312,9 +294,7 @@ class PrefixDetailTable(PrefixTable):
template_code=UTILIZATION_GRAPH,
orderable=False
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
tags = TagColumn(
url_name='ipam:prefix_list'
)
@@ -347,9 +327,7 @@ class IPAddressTable(BaseTable):
default=AVAILABLE_LABEL
)
role = ChoiceFieldColumn()
- tenant = tables.TemplateColumn(
- template_code=TENANT_LINK
- )
+ tenant = TenantColumn()
assigned_object = tables.Column(
linkify=True,
orderable=False,
@@ -379,9 +357,7 @@ class IPAddressDetailTable(IPAddressTable):
orderable=False,
verbose_name='NAT (Inside)'
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
assigned = BooleanColumn(
accessor='assigned_object_id',
verbose_name='Assigned'
@@ -428,9 +404,7 @@ class InterfaceIPAddressTable(BaseTable):
verbose_name='VRF'
)
status = ChoiceFieldColumn()
- tenant = tables.TemplateColumn(
- template_code=TENANT_LINK
- )
+ tenant = TenantColumn()
class Meta(BaseTable.Meta):
model = IPAddress
@@ -480,9 +454,7 @@ class VLANTable(BaseTable):
viewname='ipam:vlangroup_vlans',
args=[Accessor('group__pk')]
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
status = ChoiceFieldColumn(
default=AVAILABLE_LABEL
)
@@ -504,9 +476,7 @@ class VLANDetailTable(VLANTable):
orderable=False,
verbose_name='Prefixes'
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
tags = TagColumn(
url_name='ipam:vlan_list'
)
@@ -564,9 +534,7 @@ class InterfaceVLANTable(BaseTable):
accessor=Accessor('group__name'),
verbose_name='Group'
)
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
status = ChoiceFieldColumn()
role = tables.TemplateColumn(
template_code=VLAN_ROLE_LINK
diff --git a/netbox/tenancy/tables.py b/netbox/tenancy/tables.py
index 8872a7012..0536bbcfc 100644
--- a/netbox/tenancy/tables.py
+++ b/netbox/tenancy/tables.py
@@ -10,13 +10,30 @@ MPTT_LINK = """
{{ record.name }}
"""
-COL_TENANT = """
-{% if record.tenant %}
- {{ record.tenant }}
-{% else %}
- —
-{% endif %}
-"""
+
+#
+# Table columns
+#
+
+class TenantColumn(tables.TemplateColumn):
+ """
+ Render a colored label (e.g. for DeviceRoles).
+ """
+ template_code = """
+ {% if record.tenant %}
+ {{ record.tenant }}
+ {% elif record.vrf.tenant %}
+ {{ record.vrf.tenant }}*
+ {% else %}
+ —
+ {% endif %}
+ """
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(template_code=self.template_code, *args, **kwargs)
+
+ def value(self, value):
+ return str(value)
#
diff --git a/netbox/virtualization/tables.py b/netbox/virtualization/tables.py
index 808832224..1850c7bdd 100644
--- a/netbox/virtualization/tables.py
+++ b/netbox/virtualization/tables.py
@@ -1,7 +1,7 @@
import django_tables2 as tables
from dcim.tables.devices import BaseInterfaceTable
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
from utilities.tables import (
BaseTable, ButtonsColumn, ChoiceFieldColumn, ColoredLabelColumn, LinkedCountColumn, TagColumn, ToggleColumn,
)
@@ -107,9 +107,7 @@ class VirtualMachineTable(BaseTable):
linkify=True
)
role = ColoredLabelColumn()
- tenant = tables.TemplateColumn(
- template_code=COL_TENANT
- )
+ tenant = TenantColumn()
class Meta(BaseTable.Meta):
model = VirtualMachine