Parcourir la source

Created a template tag for displaying utilization graphs

Jeremy Stretch il y a 8 ans
Parent
commit
e1fc78bc44

+ 2 - 9
netbox/dcim/tables.py

@@ -49,15 +49,8 @@ STATUS_ICON = """
 """
 """
 
 
 UTILIZATION_GRAPH = """
 UTILIZATION_GRAPH = """
-{% with record.get_utilization as percentage %}
-<div class="progress text-center">
-    {% if percentage < 15 %}<span style="font-size: 12px;">{{ percentage }}%</span>{% endif %}
-    <div class="progress-bar progress-bar-{% if percentage >= 90 %}danger{% elif percentage >= 75 %}warning{% else %}success{% endif %}"
-        role="progressbar" aria-valuenow="{{ percentage }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ percentage }}%">
-        {% if percentage >= 15 %}{{ percentage }}%{% endif %}
-    </div>
-</div>
-{% endwith %}
+{% load helpers %}
+{% utilization_graph record.get_utilization %}
 """
 """
 
 
 
 

+ 2 - 9
netbox/ipam/tables.py

@@ -11,15 +11,8 @@ RIR_EDIT_LINK = """
 """
 """
 
 
 UTILIZATION_GRAPH = """
 UTILIZATION_GRAPH = """
-{% with record.get_utilization as percentage %}
-<div class="progress text-center">
-    {% if percentage < 15 %}<span style="font-size: 12px;">{{ percentage }}%</span>{% endif %}
-    <div class="progress-bar progress-bar-{% if percentage >= 90 %}danger{% elif percentage >= 75 %}warning{% else %}success{% endif %}"
-        role="progressbar" aria-valuenow="{{ percentage }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ percentage }}%">
-        {% if percentage >= 15 %}{{ percentage }}%{% endif %}
-    </div>
-</div>
-{% endwith %}
+{% load helpers %}
+{% utilization_graph record.get_utilization %}
 """
 """
 
 
 ROLE_EDIT_LINK = """
 ROLE_EDIT_LINK = """

+ 7 - 0
netbox/templates/utilities/templatetags/utilization_graph.html

@@ -0,0 +1,7 @@
+<div class="progress text-center">
+    {% if utilization < 30 %}<span style="font-size: 12px;">{{ utilization }}%</span>{% endif %}
+    <div class="progress-bar progress-bar-{% if utilization >= danger_threshold %}danger{% elif utilization >= warning_threshold %}warning{% else %}success{% endif %}"
+        role="progressbar" aria-valuenow="{{ utilization }}" aria-valuemin="0" aria-valuemax="100" style="width: {{ utilization }}%">
+        {% if utilization >= 30 %}{{ utilization }}%{% endif %}
+    </div>
+</div>

+ 12 - 0
netbox/utilities/templatetags/helpers.py

@@ -95,3 +95,15 @@ def querystring_toggle(request, multi=True, page_key='page', **kwargs):
         return '?' + querystring
         return '?' + querystring
     else:
     else:
         return ''
         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,
+    }