Skip to content

Commit e703d9f

Browse files
committed
Introduce UtilizationColumn to render utilization graphs consistently
1 parent 32501c9 commit e703d9f

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

netbox/dcim/tables/racks.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from tenancy.tables import TenantColumn
66
from utilities.tables import (
77
BaseTable, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ColoredLabelColumn, LinkedCountColumn, MPTTColumn,
8-
TagColumn, ToggleColumn,
8+
TagColumn, ToggleColumn, UtilizationColumn,
99
)
10-
from .template_code import LOCATION_ELEVATIONS, UTILIZATION_GRAPH
10+
from .template_code import LOCATION_ELEVATIONS
1111

1212
__all__ = (
1313
'RackTable',
@@ -98,13 +98,10 @@ class RackDetailTable(RackTable):
9898
url_params={'rack_id': 'pk'},
9999
verbose_name='Devices'
100100
)
101-
get_utilization = tables.TemplateColumn(
102-
template_code=UTILIZATION_GRAPH,
103-
orderable=False,
101+
get_utilization = UtilizationColumn(
104102
verbose_name='Space'
105103
)
106-
get_power_utilization = tables.TemplateColumn(
107-
template_code=UTILIZATION_GRAPH,
104+
get_power_utilization = UtilizationColumn(
108105
orderable=False,
109106
verbose_name='Power'
110107
)

netbox/dcim/tables/template_code.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@
7575
</a>
7676
"""
7777

78-
UTILIZATION_GRAPH = """
79-
{% load helpers %}
80-
{% utilization_graph value %}
81-
"""
82-
8378
#
8479
# Device component buttons
8580
#

netbox/ipam/tables.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
from tenancy.tables import TenantColumn
77
from utilities.tables import (
88
BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, LinkedCountColumn, TagColumn, ToggleColumn,
9+
UtilizationColumn,
910
)
1011
from virtualization.models import VMInterface
1112
from .models import Aggregate, IPAddress, Prefix, RIR, Role, RouteTarget, Service, VLAN, VLANGroup, VRF
1213

1314
AVAILABLE_LABEL = mark_safe('<span class="label label-success">Available</span>')
1415

15-
UTILIZATION_GRAPH = """
16-
{% load helpers %}
17-
{% if record.pk %}{% utilization_graph record.get_utilization %}{% else %}&mdash;{% endif %}
18-
"""
19-
2016
PREFIX_LINK = """
2117
{% load helpers %}
2218
{% for i in record.parents|as_range %}
@@ -209,8 +205,8 @@ class AggregateDetailTable(AggregateTable):
209205
child_count = tables.Column(
210206
verbose_name='Prefixes'
211207
)
212-
utilization = tables.TemplateColumn(
213-
template_code=UTILIZATION_GRAPH,
208+
utilization = UtilizationColumn(
209+
accessor='get_utilization',
214210
orderable=False
215211
)
216212
tags = TagColumn(
@@ -290,8 +286,8 @@ class Meta(BaseTable.Meta):
290286

291287

292288
class PrefixDetailTable(PrefixTable):
293-
utilization = tables.TemplateColumn(
294-
template_code=UTILIZATION_GRAPH,
289+
utilization = UtilizationColumn(
290+
accessor='get_utilization',
295291
orderable=False
296292
)
297293
tenant = TenantColumn()

netbox/utilities/tables.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ def value(self, value):
290290

291291

292292
class MPTTColumn(tables.TemplateColumn):
293+
"""
294+
Display a nested hierarchy for MPTT-enabled models.
295+
"""
293296
template_code = """{% for i in record.get_ancestors %}<i class="mdi mdi-circle-small"></i>{% endfor %}""" \
294297
"""<a href="{{ record.get_absolute_url }}">{{ record.name }}</a>"""
295298

@@ -304,3 +307,16 @@ def __init__(self, *args, **kwargs):
304307

305308
def value(self, value):
306309
return value
310+
311+
312+
class UtilizationColumn(tables.TemplateColumn):
313+
"""
314+
Display a colored utilization bar graph.
315+
"""
316+
template_code = """{% load helpers %}{% if record.pk %}{% utilization_graph value %}{% endif %}"""
317+
318+
def __init__(self, *args, **kwargs):
319+
super().__init__(template_code=self.template_code, *args, **kwargs)
320+
321+
def value(self, value):
322+
return f'{value}%'

0 commit comments

Comments
 (0)