diff --git a/netbox/dcim/tables/racks.py b/netbox/dcim/tables/racks.py index b1194c74b..eb4b28710 100644 --- a/netbox/dcim/tables/racks.py +++ b/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' ) diff --git a/netbox/dcim/tables/template_code.py b/netbox/dcim/tables/template_code.py index 00a5c2184..f634cf426 100644 --- a/netbox/dcim/tables/template_code.py +++ b/netbox/dcim/tables/template_code.py @@ -75,11 +75,6 @@ LOCATION_ELEVATIONS = """ """ -UTILIZATION_GRAPH = """ -{% load helpers %} -{% utilization_graph value %} -""" - # # Device component buttons # diff --git a/netbox/ipam/tables.py b/netbox/ipam/tables.py index 779b37804..6553480c6 100644 --- a/netbox/ipam/tables.py +++ b/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('Available') -UTILIZATION_GRAPH = """ -{% load helpers %} -{% if record.pk %}{% utilization_graph record.get_utilization %}{% else %}—{% 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() diff --git a/netbox/utilities/tables.py b/netbox/utilities/tables.py index 661958712..424cc6cd9 100644 --- a/netbox/utilities/tables.py +++ b/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 %}{% endfor %}""" \ """{{ record.name }}""" @@ -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}%'