17686 config option for disk divider (#18011)

This commit is contained in:
Mika Busch
2025-03-07 19:47:27 +01:00
committed by GitHub
parent 6d69c76b83
commit 29c25e39fc
9 changed files with 59 additions and 16 deletions

View File

@@ -8,6 +8,7 @@ from django.urls import NoReverseMatch, reverse
from core.models import ObjectType
from utilities.forms import get_selected_values, TableConfigForm
from utilities.views import get_viewname
from netbox.settings import DISK_BASE_UNIT, RAM_BASE_UNIT
__all__ = (
'applied_filters',
@@ -15,7 +16,8 @@ __all__ = (
'divide',
'get_item',
'get_key',
'humanize_megabytes',
'humanize_disk_megabytes',
'humanize_ram_megabytes',
'humanize_speed',
'icon_from_status',
'kg_to_pounds',
@@ -84,17 +86,16 @@ def humanize_speed(speed):
return '{} Kbps'.format(speed)
@register.filter()
def humanize_megabytes(mb):
def _humanize_megabytes(mb, divisor=1000):
"""
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
"""
if not mb:
return ""
PB_SIZE = 1000000000
TB_SIZE = 1000000
GB_SIZE = 1000
PB_SIZE = divisor**3
TB_SIZE = divisor**2
GB_SIZE = divisor
if mb >= PB_SIZE:
return f"{mb / PB_SIZE:.2f} PB"
@@ -105,6 +106,24 @@ def humanize_megabytes(mb):
return f"{mb} MB"
@register.filter()
def humanize_disk_megabytes(mb):
"""
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
Use the DISK_BASE_UNIT setting to determine the divisor. Default is 1000.
"""
return _humanize_megabytes(mb, DISK_BASE_UNIT)
@register.filter()
def humanize_ram_megabytes(mb):
"""
Express a number of megabytes in the most suitable unit (e.g. gigabytes, terabytes, etc.).
Use the RAM_BASE_UNIT setting to determine the divisor. Default is 1000.
"""
return _humanize_megabytes(mb, RAM_BASE_UNIT)
@register.filter()
def divide(x, y):
"""