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

Introduce UtilizationColumn to render utilization graphs consistently

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

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

@@ -5,9 +5,9 @@ from dcim.models import Rack, Location, RackReservation, RackRole
 from tenancy.tables import TenantColumn
 from utilities.tables import (
     BaseTable, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, MPTTColumn,
-    TagColumn, ToggleColumn,
+    TagColumn, ToggleColumn, UtilizationColumn,
 )
-from .template_code import LOCATION_ELEVATIONS, UTILIZATION_GRAPH
+from .template_code import LOCATION_ELEVATIONS
 
 __all__ = (
     'RackTable',
@@ -98,13 +98,10 @@ class RackDetailTable(RackTable):
         url_params={'rack_id': 'pk'},
         verbose_name='Devices'
     )
-    get_utilization = tables.TemplateColumn(
-        template_code=UTILIZATION_GRAPH,
-        orderable=False,
+    get_utilization = UtilizationColumn(
         verbose_name='Space'
     )
-    get_power_utilization = tables.TemplateColumn(
-        template_code=UTILIZATION_GRAPH,
+    get_power_utilization = UtilizationColumn(
         orderable=False,
         verbose_name='Power'
     )

+ 0 - 5
netbox/dcim/tables/template_code.py

@@ -75,11 +75,6 @@ LOCATION_ELEVATIONS = """
 </a>
 """
 
-UTILIZATION_GRAPH = """
-{% load helpers %}
-{% utilization_graph value %}
-"""
-
 #
 # Device component buttons
 #

+ 5 - 9
netbox/ipam/tables.py

@@ -6,17 +6,13 @@ from dcim.models import Interface
 from tenancy.tables import TenantColumn
 from utilities.tables import (
     BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, LinkedCountColumn, TagColumn, ToggleColumn,
+    UtilizationColumn,
 )
 from virtualization.models import VMInterface
 from .models import Aggregate, IPAddress, Prefix, RIR, Role, RouteTarget, Service, VLAN, VLANGroup, VRF
 
 AVAILABLE_LABEL = mark_safe('<span class="label label-success">Available</span>')
 
-UTILIZATION_GRAPH = """
-{% load helpers %}
-{% if record.pk %}{% utilization_graph record.get_utilization %}{% else %}&mdash;{% endif %}
-"""
-
 PREFIX_LINK = """
 {% load helpers %}
 {% for i in record.parents|as_range %}
@@ -209,8 +205,8 @@ class AggregateDetailTable(AggregateTable):
     child_count = tables.Column(
         verbose_name='Prefixes'
     )
-    utilization = tables.TemplateColumn(
-        template_code=UTILIZATION_GRAPH,
+    utilization = UtilizationColumn(
+        accessor='get_utilization',
         orderable=False
     )
     tags = TagColumn(
@@ -290,8 +286,8 @@ class PrefixTable(BaseTable):
 
 
 class PrefixDetailTable(PrefixTable):
-    utilization = tables.TemplateColumn(
-        template_code=UTILIZATION_GRAPH,
+    utilization = UtilizationColumn(
+        accessor='get_utilization',
         orderable=False
     )
     tenant = TenantColumn()

+ 16 - 0
netbox/utilities/tables.py

@@ -290,6 +290,9 @@ class TagColumn(tables.TemplateColumn):
 
 
 class MPTTColumn(tables.TemplateColumn):
+    """
+    Display a nested hierarchy for MPTT-enabled models.
+    """
     template_code = """{% for i in record.get_ancestors %}<i class="mdi mdi-circle-small"></i>{% endfor %}""" \
                     """<a href="{{ record.get_absolute_url }}">{{ record.name }}</a>"""
 
@@ -304,3 +307,16 @@ class MPTTColumn(tables.TemplateColumn):
 
     def value(self, value):
         return value
+
+
+class UtilizationColumn(tables.TemplateColumn):
+    """
+    Display a colored utilization bar graph.
+    """
+    template_code = """{% load helpers %}{% if record.pk %}{% utilization_graph value %}{% endif %}"""
+
+    def __init__(self, *args, **kwargs):
+        super().__init__(template_code=self.template_code, *args, **kwargs)
+
+    def value(self, value):
+        return f'{value}%'