diff --git a/netbox/dcim/tables.py b/netbox/dcim/tables.py index 0fe2c6834..dfe2544fe 100644 --- a/netbox/dcim/tables.py +++ b/netbox/dcim/tables.py @@ -49,15 +49,8 @@ STATUS_ICON = """ """ UTILIZATION_GRAPH = """ -{% with record.get_utilization as percentage %} -
- {% if percentage < 15 %}{{ percentage }}%{% endif %} -
- {% if percentage >= 15 %}{{ percentage }}%{% endif %} -
-
-{% endwith %} +{% load helpers %} +{% utilization_graph record.get_utilization %} """ diff --git a/netbox/ipam/tables.py b/netbox/ipam/tables.py index f30906255..602462a2e 100644 --- a/netbox/ipam/tables.py +++ b/netbox/ipam/tables.py @@ -11,15 +11,8 @@ RIR_EDIT_LINK = """ """ UTILIZATION_GRAPH = """ -{% with record.get_utilization as percentage %} -
- {% if percentage < 15 %}{{ percentage }}%{% endif %} -
- {% if percentage >= 15 %}{{ percentage }}%{% endif %} -
-
-{% endwith %} +{% load helpers %} +{% utilization_graph record.get_utilization %} """ ROLE_EDIT_LINK = """ diff --git a/netbox/templates/utilities/templatetags/utilization_graph.html b/netbox/templates/utilities/templatetags/utilization_graph.html new file mode 100644 index 000000000..9232da3b8 --- /dev/null +++ b/netbox/templates/utilities/templatetags/utilization_graph.html @@ -0,0 +1,7 @@ +
+ {% if utilization < 30 %}{{ utilization }}%{% endif %} +
+ {% if utilization >= 30 %}{{ utilization }}%{% endif %} +
+
diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index cc0b3b2e4..f26a06488 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -95,3 +95,15 @@ def querystring_toggle(request, multi=True, page_key='page', **kwargs): return '?' + querystring else: return '' + + +@register.inclusion_tag('utilities/templatetags/utilization_graph.html') +def utilization_graph(utilization, warning_threshold=75, danger_threshold=90): + """ + Display a horizontal bar graph indicating a percentage of utilization. + """ + return { + 'utilization': utilization, + 'warning_threshold': warning_threshold, + 'danger_threshold': danger_threshold, + }