Moved circuit speed humanization to a template tag

This commit is contained in:
Jeremy Stretch
2017-08-29 22:42:06 -04:00
parent ecdf66c454
commit ae231b1d1b
4 changed files with 27 additions and 32 deletions

View File

@@ -62,6 +62,27 @@ def bettertitle(value):
return ' '.join([w[0].upper() + w[1:] for w in value.split()])
@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):
"""