Просмотр исходного кода

Introduce TenantColumn to simplify representation

Jeremy Stretch 5 лет назад
Родитель
Сommit
20a85c1ef2

+ 2 - 4
netbox/circuits/tables.py

@@ -1,7 +1,7 @@
 import django_tables2 as tables
 import django_tables2 as tables
 from django_tables2.utils import Accessor
 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 utilities.tables import BaseTable, ButtonsColumn, ChoiceFieldColumn, TagColumn, ToggleColumn
 from .models import Circuit, CircuitType, Provider
 from .models import Circuit, CircuitType, Provider
 
 
@@ -60,9 +60,7 @@ class CircuitTable(BaseTable):
         linkify=True
         linkify=True
     )
     )
     status = ChoiceFieldColumn()
     status = ChoiceFieldColumn()
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     a_side = tables.Column(
     a_side = tables.Column(
         verbose_name='A Side'
         verbose_name='A Side'
     )
     )

+ 3 - 7
netbox/dcim/tables/devices.py

@@ -5,7 +5,7 @@ from dcim.models import (
     ConsolePort, ConsoleServerPort, Device, DeviceBay, DeviceRole, FrontPort, Interface, InventoryItem, Platform,
     ConsolePort, ConsoleServerPort, Device, DeviceBay, DeviceRole, FrontPort, Interface, InventoryItem, Platform,
     PowerOutlet, PowerPort, RearPort, VirtualChassis,
     PowerOutlet, PowerPort, RearPort, VirtualChassis,
 )
 )
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
 from utilities.tables import (
 from utilities.tables import (
     BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn,
     BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn,
     TagColumn, ToggleColumn,
     TagColumn, ToggleColumn,
@@ -109,9 +109,7 @@ class DeviceTable(BaseTable):
         template_code=DEVICE_LINK
         template_code=DEVICE_LINK
     )
     )
     status = ChoiceFieldColumn()
     status = ChoiceFieldColumn()
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     site = tables.Column(
     site = tables.Column(
         linkify=True
         linkify=True
     )
     )
@@ -178,9 +176,7 @@ class DeviceImportTable(BaseTable):
         template_code=DEVICE_LINK
         template_code=DEVICE_LINK
     )
     )
     status = ChoiceFieldColumn()
     status = ChoiceFieldColumn()
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     site = tables.Column(
     site = tables.Column(
         linkify=True
         linkify=True
     )
     )

+ 3 - 7
netbox/dcim/tables/racks.py

@@ -2,7 +2,7 @@ import django_tables2 as tables
 from django_tables2.utils import Accessor
 from django_tables2.utils import Accessor
 
 
 from dcim.models import Rack, Location, RackReservation, RackRole
 from dcim.models import Rack, Location, RackReservation, RackRole
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
 from utilities.tables import (
 from utilities.tables import (
     BaseTable, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, TagColumn,
     BaseTable, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, TagColumn,
     ToggleColumn,
     ToggleColumn,
@@ -79,9 +79,7 @@ class RackTable(BaseTable):
     site = tables.Column(
     site = tables.Column(
         linkify=True
         linkify=True
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     status = ChoiceFieldColumn()
     status = ChoiceFieldColumn()
     role = ColoredLabelColumn()
     role = ColoredLabelColumn()
     u_height = tables.TemplateColumn(
     u_height = tables.TemplateColumn(
@@ -143,9 +141,7 @@ class RackReservationTable(BaseTable):
         accessor=Accessor('rack__site'),
         accessor=Accessor('rack__site'),
         linkify=True
         linkify=True
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     rack = tables.Column(
     rack = tables.Column(
         linkify=True
         linkify=True
     )
     )

+ 2 - 4
netbox/dcim/tables/sites.py

@@ -1,7 +1,7 @@
 import django_tables2 as tables
 import django_tables2 as tables
 
 
 from dcim.models import Region, Site
 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 utilities.tables import BaseTable, ButtonsColumn, ChoiceFieldColumn, TagColumn, ToggleColumn
 from .template_code import MPTT_LINK
 from .template_code import MPTT_LINK
 
 
@@ -46,9 +46,7 @@ class SiteTable(BaseTable):
     region = tables.Column(
     region = tables.Column(
         linkify=True
         linkify=True
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     tags = TagColumn(
     tags = TagColumn(
         url_name='dcim:site_list'
         url_name='dcim:site_list'
     )
     )

+ 12 - 44
netbox/ipam/tables.py

@@ -3,7 +3,7 @@ from django.utils.safestring import mark_safe
 from django_tables2.utils import Accessor
 from django_tables2.utils import Accessor
 
 
 from dcim.models import Interface
 from dcim.models import Interface
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
 from utilities.tables import (
 from utilities.tables import (
     BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, LinkedCountColumn, TagColumn, ToggleColumn,
     BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, LinkedCountColumn, TagColumn, ToggleColumn,
 )
 )
@@ -109,16 +109,6 @@ VLAN_MEMBER_TAGGED = """
 {% endif %}
 {% endif %}
 """
 """
 
 
-TENANT_LINK = """
-{% if record.tenant %}
-    <a href="{{ record.tenant.get_absolute_url }}" title="{{ record.tenant.description }}">{{ record.tenant }}</a>
-{% elif record.vrf.tenant %}
-    <a href="{{ record.vrf.tenant.get_absolute_url }}" title="{{ record.vrf.tenant.description }}">{{ record.vrf.tenant }}</a>*
-{% else %}
-    &mdash;
-{% endif %}
-"""
-
 
 
 #
 #
 # VRFs
 # VRFs
@@ -130,9 +120,7 @@ class VRFTable(BaseTable):
     rd = tables.Column(
     rd = tables.Column(
         verbose_name='RD'
         verbose_name='RD'
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     enforce_unique = BooleanColumn(
     enforce_unique = BooleanColumn(
         verbose_name='Unique'
         verbose_name='Unique'
     )
     )
@@ -163,9 +151,7 @@ class VRFTable(BaseTable):
 class RouteTargetTable(BaseTable):
 class RouteTargetTable(BaseTable):
     pk = ToggleColumn()
     pk = ToggleColumn()
     name = tables.LinkColumn()
     name = tables.LinkColumn()
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     tags = TagColumn(
     tags = TagColumn(
         url_name='ipam:vrf_list'
         url_name='ipam:vrf_list'
     )
     )
@@ -208,9 +194,7 @@ class AggregateTable(BaseTable):
     prefix = tables.LinkColumn(
     prefix = tables.LinkColumn(
         verbose_name='Aggregate'
         verbose_name='Aggregate'
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=TENANT_LINK
-    )
+    tenant = TenantColumn()
     date_added = tables.DateColumn(
     date_added = tables.DateColumn(
         format="Y-m-d",
         format="Y-m-d",
         verbose_name='Added'
         verbose_name='Added'
@@ -279,9 +263,7 @@ class PrefixTable(BaseTable):
         template_code=VRF_LINK,
         template_code=VRF_LINK,
         verbose_name='VRF'
         verbose_name='VRF'
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=TENANT_LINK
-    )
+    tenant = TenantColumn()
     site = tables.Column(
     site = tables.Column(
         linkify=True
         linkify=True
     )
     )
@@ -312,9 +294,7 @@ class PrefixDetailTable(PrefixTable):
         template_code=UTILIZATION_GRAPH,
         template_code=UTILIZATION_GRAPH,
         orderable=False
         orderable=False
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     tags = TagColumn(
     tags = TagColumn(
         url_name='ipam:prefix_list'
         url_name='ipam:prefix_list'
     )
     )
@@ -347,9 +327,7 @@ class IPAddressTable(BaseTable):
         default=AVAILABLE_LABEL
         default=AVAILABLE_LABEL
     )
     )
     role = ChoiceFieldColumn()
     role = ChoiceFieldColumn()
-    tenant = tables.TemplateColumn(
-        template_code=TENANT_LINK
-    )
+    tenant = TenantColumn()
     assigned_object = tables.Column(
     assigned_object = tables.Column(
         linkify=True,
         linkify=True,
         orderable=False,
         orderable=False,
@@ -379,9 +357,7 @@ class IPAddressDetailTable(IPAddressTable):
         orderable=False,
         orderable=False,
         verbose_name='NAT (Inside)'
         verbose_name='NAT (Inside)'
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     assigned = BooleanColumn(
     assigned = BooleanColumn(
         accessor='assigned_object_id',
         accessor='assigned_object_id',
         verbose_name='Assigned'
         verbose_name='Assigned'
@@ -428,9 +404,7 @@ class InterfaceIPAddressTable(BaseTable):
         verbose_name='VRF'
         verbose_name='VRF'
     )
     )
     status = ChoiceFieldColumn()
     status = ChoiceFieldColumn()
-    tenant = tables.TemplateColumn(
-        template_code=TENANT_LINK
-    )
+    tenant = TenantColumn()
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = IPAddress
         model = IPAddress
@@ -480,9 +454,7 @@ class VLANTable(BaseTable):
         viewname='ipam:vlangroup_vlans',
         viewname='ipam:vlangroup_vlans',
         args=[Accessor('group__pk')]
         args=[Accessor('group__pk')]
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     status = ChoiceFieldColumn(
     status = ChoiceFieldColumn(
         default=AVAILABLE_LABEL
         default=AVAILABLE_LABEL
     )
     )
@@ -504,9 +476,7 @@ class VLANDetailTable(VLANTable):
         orderable=False,
         orderable=False,
         verbose_name='Prefixes'
         verbose_name='Prefixes'
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     tags = TagColumn(
     tags = TagColumn(
         url_name='ipam:vlan_list'
         url_name='ipam:vlan_list'
     )
     )
@@ -564,9 +534,7 @@ class InterfaceVLANTable(BaseTable):
         accessor=Accessor('group__name'),
         accessor=Accessor('group__name'),
         verbose_name='Group'
         verbose_name='Group'
     )
     )
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
     status = ChoiceFieldColumn()
     status = ChoiceFieldColumn()
     role = tables.TemplateColumn(
     role = tables.TemplateColumn(
         template_code=VLAN_ROLE_LINK
         template_code=VLAN_ROLE_LINK

+ 24 - 7
netbox/tenancy/tables.py

@@ -10,13 +10,30 @@ MPTT_LINK = """
 <a href="{{ record.get_absolute_url }}">{{ record.name }}</a>
 <a href="{{ record.get_absolute_url }}">{{ record.name }}</a>
 """
 """
 
 
-COL_TENANT = """
-{% if record.tenant %}
-    <a href="{{ record.tenant.get_absolute_url }}" title="{{ record.tenant.description }}">{{ record.tenant }}</a>
-{% else %}
-    &mdash;
-{% endif %}
-"""
+
+#
+# Table columns
+#
+
+class TenantColumn(tables.TemplateColumn):
+    """
+    Render a colored label (e.g. for DeviceRoles).
+    """
+    template_code = """
+    {% if record.tenant %}
+        <a href="{{ record.tenant.get_absolute_url }}" title="{{ record.tenant.description }}">{{ record.tenant }}</a>
+    {% elif record.vrf.tenant %}
+        <a href="{{ record.vrf.tenant.get_absolute_url }}" title="{{ record.vrf.tenant.description }}">{{ record.vrf.tenant }}</a>*
+    {% else %}
+        &mdash;
+    {% endif %}
+    """
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(template_code=self.template_code, *args, **kwargs)
+
+    def value(self, value):
+        return str(value)
 
 
 
 
 #
 #

+ 2 - 4
netbox/virtualization/tables.py

@@ -1,7 +1,7 @@
 import django_tables2 as tables
 import django_tables2 as tables
 
 
 from dcim.tables.devices import BaseInterfaceTable
 from dcim.tables.devices import BaseInterfaceTable
-from tenancy.tables import COL_TENANT
+from tenancy.tables import TenantColumn
 from utilities.tables import (
 from utilities.tables import (
     BaseTable, ButtonsColumn, ChoiceFieldColumn, ColoredLabelColumn, LinkedCountColumn, TagColumn, ToggleColumn,
     BaseTable, ButtonsColumn, ChoiceFieldColumn, ColoredLabelColumn, LinkedCountColumn, TagColumn, ToggleColumn,
 )
 )
@@ -107,9 +107,7 @@ class VirtualMachineTable(BaseTable):
         linkify=True
         linkify=True
     )
     )
     role = ColoredLabelColumn()
     role = ColoredLabelColumn()
-    tenant = tables.TemplateColumn(
-        template_code=COL_TENANT
-    )
+    tenant = TenantColumn()
 
 
     class Meta(BaseTable.Meta):
     class Meta(BaseTable.Meta):
         model = VirtualMachine
         model = VirtualMachine