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

Introduce ChoiceFieldColumn to replace template columns

Jeremy Stretch 5 лет назад
Родитель
Сommit
18a8a91d57
6 измененных файлов с 44 добавлено и 64 удалено
  1. 2 8
      netbox/circuits/tables.py
  2. 9 30
      netbox/dcim/tables.py
  3. 14 16
      netbox/ipam/tables.py
  4. 1 1
      netbox/ipam/utils.py
  5. 16 0
      netbox/utilities/tables.py
  6. 2 9
      netbox/virtualization/tables.py

+ 2 - 8
netbox/circuits/tables.py

@@ -2,13 +2,9 @@ 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 COL_TENANT
-from utilities.tables import BaseTable, ButtonsColumn, TagColumn, ToggleColumn
+from utilities.tables import BaseTable, ButtonsColumn, ChoiceFieldColumn, TagColumn, ToggleColumn
 from .models import Circuit, CircuitType, Provider
 from .models import Circuit, CircuitType, Provider
 
 
-STATUS_LABEL = """
-<span class="label label-{{ record.get_status_class }}">{{ record.get_status_display }}</span>
-"""
-
 
 
 #
 #
 # Providers
 # Providers
@@ -64,9 +60,7 @@ class CircuitTable(BaseTable):
         viewname='circuits:provider',
         viewname='circuits:provider',
         args=[Accessor('provider__slug')]
         args=[Accessor('provider__slug')]
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=COL_TENANT
         template_code=COL_TENANT
     )
     )

+ 9 - 30
netbox/dcim/tables.py

@@ -3,7 +3,8 @@ from django_tables2.utils import Accessor
 
 
 from tenancy.tables import COL_TENANT
 from tenancy.tables import COL_TENANT
 from utilities.tables import (
 from utilities.tables import (
-    BaseTable, BooleanColumn, ButtonsColumn, ColorColumn, ColoredLabelColumn, TagColumn, ToggleColumn,
+    BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, TagColumn,
+    ToggleColumn,
 )
 )
 from .models import (
 from .models import (
     Cable, ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceBay,
     Cable, ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceBay,
@@ -99,14 +100,6 @@ PLATFORM_VM_COUNT = """
 <a href="{% url 'virtualization:virtualmachine_list' %}?platform={{ record.slug }}">{{ value|default:0 }}</a>
 <a href="{% url 'virtualization:virtualmachine_list' %}?platform={{ record.slug }}">{{ value|default:0 }}</a>
 """
 """
 
 
-STATUS_LABEL = """
-<span class="label label-{{ record.get_status_class }}">{{ record.get_status_display }}</span>
-"""
-
-TYPE_LABEL = """
-<span class="label label-{{ record.get_type_class }}">{{ record.get_type_display }}</span>
-"""
-
 DEVICE_PRIMARY_IP = """
 DEVICE_PRIMARY_IP = """
 {{ record.primary_ip6.address.ip|default:"" }}
 {{ record.primary_ip6.address.ip|default:"" }}
 {% if record.primary_ip6 and record.primary_ip4 %}<br />{% endif %}
 {% if record.primary_ip6 and record.primary_ip4 %}<br />{% endif %}
@@ -187,9 +180,7 @@ class SiteTable(BaseTable):
     name = tables.LinkColumn(
     name = tables.LinkColumn(
         order_by=('_name',)
         order_by=('_name',)
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     region = tables.TemplateColumn(
     region = tables.TemplateColumn(
         template_code=SITE_REGION_LINK
         template_code=SITE_REGION_LINK
     )
     )
@@ -272,9 +263,7 @@ class RackTable(BaseTable):
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=COL_TENANT
         template_code=COL_TENANT
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     role = ColoredLabelColumn()
     role = ColoredLabelColumn()
     u_height = tables.TemplateColumn(
     u_height = tables.TemplateColumn(
         template_code="{{ record.u_height }}U",
         template_code="{{ record.u_height }}U",
@@ -595,9 +584,7 @@ class DeviceTable(BaseTable):
         order_by=('_name',),
         order_by=('_name',),
         template_code=DEVICE_LINK
         template_code=DEVICE_LINK
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=COL_TENANT
         template_code=COL_TENANT
     )
     )
@@ -663,9 +650,7 @@ class DeviceImportTable(BaseTable):
     name = tables.TemplateColumn(
     name = tables.TemplateColumn(
         template_code=DEVICE_LINK
         template_code=DEVICE_LINK
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=COL_TENANT
         template_code=COL_TENANT
     )
     )
@@ -876,9 +861,7 @@ class CableTable(BaseTable):
         orderable=False,
         orderable=False,
         verbose_name='Termination B'
         verbose_name='Termination B'
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     length = tables.TemplateColumn(
     length = tables.TemplateColumn(
         template_code=CABLE_LENGTH,
         template_code=CABLE_LENGTH,
         order_by='_abs_length'
         order_by='_abs_length'
@@ -1062,12 +1045,8 @@ class PowerFeedTable(BaseTable):
     rack = tables.Column(
     rack = tables.Column(
         linkify=True
         linkify=True
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
-    type = tables.TemplateColumn(
-        template_code=TYPE_LABEL
-    )
+    status = ChoiceFieldColumn()
+    type = ChoiceFieldColumn()
     max_utilization = tables.TemplateColumn(
     max_utilization = tables.TemplateColumn(
         template_code="{{ value }}%"
         template_code="{{ value }}%"
     )
     )

+ 14 - 16
netbox/ipam/tables.py

@@ -1,12 +1,15 @@
 import django_tables2 as tables
 import django_tables2 as tables
+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 COL_TENANT
-from utilities.tables import BaseTable, BooleanColumn, ButtonsColumn, TagColumn, ToggleColumn
+from utilities.tables import BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, TagColumn, ToggleColumn
 from virtualization.models import VMInterface
 from virtualization.models import VMInterface
 from .models import Aggregate, IPAddress, Prefix, RIR, Role, RouteTarget, Service, VLAN, VLANGroup, VRF
 from .models import Aggregate, IPAddress, Prefix, RIR, Role, RouteTarget, Service, VLAN, VLANGroup, VRF
 
 
+AVAILABLE_LABEL = mark_safe('<span class="label label-success">Available</span>')
+
 RIR_UTILIZATION = """
 RIR_UTILIZATION = """
 <div class="progress">
 <div class="progress">
     {% if record.stats.total %}
     {% if record.stats.total %}
@@ -327,8 +330,8 @@ class PrefixTable(BaseTable):
         template_code=PREFIX_LINK,
         template_code=PREFIX_LINK,
         attrs={'th': {'style': 'padding-left: 17px'}}
         attrs={'th': {'style': 'padding-left: 17px'}}
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
+    status = ChoiceFieldColumn(
+        default=AVAILABLE_LABEL
     )
     )
     vrf = tables.TemplateColumn(
     vrf = tables.TemplateColumn(
         template_code=VRF_LINK,
         template_code=VRF_LINK,
@@ -400,9 +403,10 @@ class IPAddressTable(BaseTable):
         template_code=VRF_LINK,
         template_code=VRF_LINK,
         verbose_name='VRF'
         verbose_name='VRF'
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
+    status = ChoiceFieldColumn(
+        default=AVAILABLE_LABEL
     )
     )
+    role = ChoiceFieldColumn()
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=TENANT_LINK
         template_code=TENANT_LINK
     )
     )
@@ -461,9 +465,7 @@ class IPAddressAssignTable(BaseTable):
         template_code=IPADDRESS_ASSIGN_LINK,
         template_code=IPADDRESS_ASSIGN_LINK,
         verbose_name='IP Address'
         verbose_name='IP Address'
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     assigned_object = tables.Column(
     assigned_object = tables.Column(
         orderable=False
         orderable=False
     )
     )
@@ -485,9 +487,7 @@ class InterfaceIPAddressTable(BaseTable):
         template_code=VRF_LINK,
         template_code=VRF_LINK,
         verbose_name='VRF'
         verbose_name='VRF'
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=TENANT_LINK
         template_code=TENANT_LINK
     )
     )
@@ -543,8 +543,8 @@ class VLANTable(BaseTable):
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=COL_TENANT
         template_code=COL_TENANT
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
+    status = ChoiceFieldColumn(
+        default=AVAILABLE_LABEL
     )
     )
     role = tables.TemplateColumn(
     role = tables.TemplateColumn(
         template_code=VLAN_ROLE_LINK
         template_code=VLAN_ROLE_LINK
@@ -630,9 +630,7 @@ class InterfaceVLANTable(BaseTable):
     tenant = tables.TemplateColumn(
     tenant = tables.TemplateColumn(
         template_code=COL_TENANT
         template_code=COL_TENANT
     )
     )
-    status = tables.TemplateColumn(
-        template_code=STATUS_LABEL
-    )
+    status = ChoiceFieldColumn()
     role = tables.TemplateColumn(
     role = tables.TemplateColumn(
         template_code=VLAN_ROLE_LINK
         template_code=VLAN_ROLE_LINK
     )
     )

+ 1 - 1
netbox/ipam/utils.py

@@ -11,7 +11,7 @@ def add_available_prefixes(parent, prefix_list):
 
 
     # Find all unallocated space
     # Find all unallocated space
     available_prefixes = netaddr.IPSet(parent) ^ netaddr.IPSet([p.prefix for p in prefix_list])
     available_prefixes = netaddr.IPSet(parent) ^ netaddr.IPSet([p.prefix for p in prefix_list])
-    available_prefixes = [Prefix(prefix=p) for p in available_prefixes.iter_cidrs()]
+    available_prefixes = [Prefix(prefix=p, status=None) for p in available_prefixes.iter_cidrs()]
 
 
     # Concatenate and sort complete list of children
     # Concatenate and sort complete list of children
     prefix_list = list(prefix_list) + available_prefixes
     prefix_list = list(prefix_list) + available_prefixes

+ 16 - 0
netbox/utilities/tables.py

@@ -174,6 +174,22 @@ class ButtonsColumn(tables.TemplateColumn):
         return ''
         return ''
 
 
 
 
+class ChoiceFieldColumn(tables.Column):
+    """
+    Render a ChoiceField value inside a <span> indicating a particular CSS class. This is useful for displaying colored
+    choices. The CSS class is derived by calling .get_FOO_class() on the row record.
+    """
+    def render(self, record, bound_column, value):
+        if value:
+            name = bound_column.name
+            css_class = getattr(record, f'get_{name}_class')()
+            label = getattr(record, f'get_{name}_display')()
+            return mark_safe(
+                f'<span class="label label-{css_class}">{label}</span>'
+            )
+        return self.default
+
+
 class ColorColumn(tables.Column):
 class ColorColumn(tables.Column):
     """
     """
     Display a color (#RRGGBB).
     Display a color (#RRGGBB).

+ 2 - 9
netbox/virtualization/tables.py

@@ -1,15 +1,10 @@
 import django_tables2 as tables
 import django_tables2 as tables
-from django_tables2.utils import Accessor
 
 
 from dcim.tables import BaseInterfaceTable
 from dcim.tables import BaseInterfaceTable
 from tenancy.tables import COL_TENANT
 from tenancy.tables import COL_TENANT
-from utilities.tables import BaseTable, ButtonsColumn, ColoredLabelColumn, TagColumn, ToggleColumn
+from utilities.tables import BaseTable, ButtonsColumn, ChoiceFieldColumn, ColoredLabelColumn, TagColumn, ToggleColumn
 from .models import Cluster, ClusterGroup, ClusterType, VirtualMachine, VMInterface
 from .models import Cluster, ClusterGroup, ClusterType, VirtualMachine, VMInterface
 
 
-VIRTUALMACHINE_STATUS = """
-<span class="label label-{{ record.get_status_class }}">{{ record.get_status_display }}</span>
-"""
-
 VIRTUALMACHINE_PRIMARY_IP = """
 VIRTUALMACHINE_PRIMARY_IP = """
 {{ record.primary_ip6.address.ip|default:"" }}
 {{ record.primary_ip6.address.ip|default:"" }}
 {% if record.primary_ip6 and record.primary_ip4 %}<br />{% endif %}
 {% if record.primary_ip6 and record.primary_ip4 %}<br />{% endif %}
@@ -99,9 +94,7 @@ class ClusterTable(BaseTable):
 class VirtualMachineTable(BaseTable):
 class VirtualMachineTable(BaseTable):
     pk = ToggleColumn()
     pk = ToggleColumn()
     name = tables.LinkColumn()
     name = tables.LinkColumn()
-    status = tables.TemplateColumn(
-        template_code=VIRTUALMACHINE_STATUS
-    )
+    status = ChoiceFieldColumn()
     cluster = tables.Column(
     cluster = tables.Column(
         linkify=True
         linkify=True
     )
     )