From a1fd983173caa10825a7097e77cd03732e1ac37f Mon Sep 17 00:00:00 2001 From: Julio-Oliveira-Encora Date: Thu, 23 May 2024 16:04:08 -0300 Subject: [PATCH] Changed humanize_megabytes --- netbox/templates/virtualization/cluster.html | 2 +- netbox/utilities/templatetags/helpers.py | 29 +++++++++++++++---- .../tests/test_humanize_megabytes_helper.py | 25 ++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 netbox/utilities/tests/test_humanize_megabytes_helper.py diff --git a/netbox/templates/virtualization/cluster.html b/netbox/templates/virtualization/cluster.html index 7628bf876..264a275e9 100644 --- a/netbox/templates/virtualization/cluster.html +++ b/netbox/templates/virtualization/cluster.html @@ -61,7 +61,7 @@ {% if memory_sum %} {{ memory_sum|humanize_megabytes }} {% else %} - {{ ''|placeholder }} + {{ ''|placeholder }} {% endif %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 0c089b246..e2a16d088 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -93,14 +93,31 @@ def humanize_speed(speed): def humanize_megabytes(mb): """ Express a number of megabytes in the most suitable unit (e.g. gigabytes or terabytes). + It considers the mb value as megabytes and converts it to the most suitable unit. """ + + # Factors in bytes + factors = { + "mega": 1024 ** 2, + "giga": 1024 ** 3, + "tera": 1024 ** 4, + } + if not mb: - return '' - if not mb % 1048576: # 1024^2 - return f'{int(mb / 1048576)} TB' - if not mb % 1024: - return f'{int(mb / 1024)} GB' - return f'{int(mb) / (1048576):.2f} MB' + return "" + + print(factors['mega']) + + bytes = int(mb * 1024**2) + + if bytes >= factors["tera"]: + return f"{bytes / factors["tera"]:.2f} TB" + + if bytes >= factors["giga"]: + return f"{bytes / factors["giga"]:.2f} GB" + + if bytes >= factors["mega"]: + return f"{bytes / factors["mega"]:.2f} MB" @register.filter() diff --git a/netbox/utilities/tests/test_humanize_megabytes_helper.py b/netbox/utilities/tests/test_humanize_megabytes_helper.py new file mode 100644 index 000000000..be6e2a29d --- /dev/null +++ b/netbox/utilities/tests/test_humanize_megabytes_helper.py @@ -0,0 +1,25 @@ +from utilities.templatetags.helpers import humanize_megabytes +from utilities.testing import TestCase + + +class TestConvertByteSize(TestCase): + + def test_humanize_megabytes_converts_megabytes(self): + """Test that humanize_megabytes converts megabytes to the most suitable unit.""" + self.assertEqual(humanize_megabytes(1), "1.00 MB") + + def test_humanize_megabytes_converts_to_gigabytes(self): + """Test that humanize_megabytes converts megabytes to gigabytes.""" + self.assertEqual(humanize_megabytes(1024), "1.00 GB") + + def test_humanize_megabytes_converts_to_terabytes(self): + """Test that humanize_megabytes converts megabytes to terabytes.""" + self.assertEqual(humanize_megabytes(1048576), "1.00 TB") + + def test_humanize_megabytes_returns_empty_for_none(self): + """Test that humanize_megabytes returns empty for None.""" + self.assertEqual(humanize_megabytes(None), '') + + def test_humanize_megabytes_without_unit(self): + """Test that humanize_megabytes returns the value without unit.""" + self.assertEqual(humanize_megabytes(123456789), "117.74 TB")