Parcourir la source

Moved circuit speed humanization to a template tag

Jeremy Stretch il y a 7 ans
Parent
commit
ae231b1d1b

+ 0 - 28
netbox/circuits/models.py

@@ -13,22 +13,6 @@ from utilities.models import CreatedUpdatedModel
 from .constants import *
 
 
-def humanize_speed(speed):
-    """
-    Humanize speeds given in Kbps (e.g. 10000000 becomes '10 Gbps')
-    """
-    if speed >= 1000000000 and speed % 1000000000 == 0:
-        return '{} Tbps'.format(speed / 1000000000)
-    elif speed >= 1000000 and speed % 1000000 == 0:
-        return '{} Gbps'.format(speed / 1000000)
-    elif speed >= 1000 and speed % 1000 == 0:
-        return '{} Mbps'.format(speed / 1000)
-    elif speed >= 1000:
-        return '{} Mbps'.format(float(speed) / 1000)
-    else:
-        return '{} Kbps'.format(speed)
-
-
 @python_2_unicode_compatible
 class Provider(CreatedUpdatedModel, CustomFieldModel):
     """
@@ -139,10 +123,6 @@ class Circuit(CreatedUpdatedModel, CustomFieldModel):
     def termination_z(self):
         return self._get_termination('Z')
 
-    def commit_rate_human(self):
-        return '' if not self.commit_rate else humanize_speed(self.commit_rate)
-    commit_rate_human.admin_order_field = 'commit_rate'
-
 
 @python_2_unicode_compatible
 class CircuitTermination(models.Model):
@@ -173,11 +153,3 @@ class CircuitTermination(models.Model):
             return CircuitTermination.objects.select_related('site').get(circuit=self.circuit, term_side=peer_side)
         except CircuitTermination.DoesNotExist:
             return None
-
-    def port_speed_human(self):
-        return humanize_speed(self.port_speed)
-    port_speed_human.admin_order_field = 'port_speed'
-
-    def upstream_speed_human(self):
-        return '' if not self.upstream_speed else humanize_speed(self.upstream_speed)
-    upstream_speed_human.admin_order_field = 'upstream_speed'

+ 1 - 1
netbox/templates/circuits/circuit.html

@@ -88,7 +88,7 @@
                     <td>Commit Rate</td>
                     <td>
                         {% if circuit.commit_rate %}
-                            {{ circuit.commit_rate_human }}
+                            {{ circuit.commit_rate|humanize_speed }}
                         {% else %}
                             <span class="text-muted">N/A</span>
                         {% endif %}

+ 5 - 3
netbox/templates/circuits/inc/circuit_termination.html

@@ -1,3 +1,5 @@
+{% load helpers %}
+
 <div class="panel panel-default">
     <div class="panel-heading">
         <div class="pull-right">
@@ -49,10 +51,10 @@
                 <td>Speed</td>
                 <td>
                     {% if termination.upstream_speed %}
-                        <i class="fa fa-arrow-down" title="Downstream"></i> {{ termination.port_speed_human }} &nbsp;
-                        <i class="fa fa-arrow-up" title="Upstream"></i> {{ termination.upstream_speed_human }}
+                        <i class="fa fa-arrow-down" title="Downstream"></i> {{ termination.port_speed|humanize_speed }} &nbsp;
+                        <i class="fa fa-arrow-up" title="Upstream"></i> {{ termination.upstream_speed|humanize_speed }}
                     {% else %}
-                        {{ termination.port_speed_human }}
+                        {{ termination.port_speed|humanize_speed }}
                     {% endif %}
                 </td>
             </tr>

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

@@ -63,6 +63,27 @@ def bettertitle(value):
 
 
 @register.filter()
+def humanize_speed(speed):
+    """
+    Humanize speeds given in Kbps. Examples:
+
+        1544 => "1.544 Mbps"
+        100000 => "100 Mbps"
+        10000000 => "10 Gbps"
+    """
+    if speed >= 1000000000 and speed % 1000000000 == 0:
+        return '{} Tbps'.format(int(speed / 1000000000))
+    elif speed >= 1000000 and speed % 1000000 == 0:
+        return '{} Gbps'.format(int(speed / 1000000))
+    elif speed >= 1000 and speed % 1000 == 0:
+        return '{} Mbps'.format(int(speed / 1000))
+    elif speed >= 1000:
+        return '{} Mbps'.format(float(speed) / 1000)
+    else:
+        return '{} Kbps'.format(speed)
+
+
+@register.filter()
 def example_choices(field, arg=3):
     """
     Returns a number (default: 3) of example choices for a ChoiceFiled (useful for CSV import forms).