From cfee3416c74c854e7558d78886019beb58a0d5c7 Mon Sep 17 00:00:00 2001 From: Zhenguo Niu Date: Tue, 17 Dec 2013 13:16:10 +0800 Subject: [PATCH] Fix inappropriate display for inf value of RAM and Storage. Change-Id: Ie00fa5231648ddef359e42e7195836be10f77996 Closes-Bug: #1261462 --- .../horizon/common/_limit_summary.html | 2 +- horizon/templatetags/sizeformat.py | 25 +++++++++---------- horizon/test/tests/templatetags.py | 15 ++++++++++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/horizon/templates/horizon/common/_limit_summary.html b/horizon/templates/horizon/common/_limit_summary.html index 1f172fb697..28129e33ce 100644 --- a/horizon/templates/horizon/common/_limit_summary.html +++ b/horizon/templates/horizon/common/_limit_summary.html @@ -47,7 +47,7 @@
{% trans "Volume Storage" %}
- {% blocktrans with used=usage.limits.totalGigabytesUsed|diskgbformat available=usage.limits.maxTotalVolumeGigabytes|diskgbformat %}Used {{ used }} of {{ available }} {% endblocktrans %} + {% blocktrans with used=usage.limits.totalGigabytesUsed|diskgbformat available=usage.limits.maxTotalVolumeGigabytes|quotainf|diskgbformat %}Used {{ used }} of {{ available }} {% endblocktrans %}
{% endif %} diff --git a/horizon/templatetags/sizeformat.py b/horizon/templatetags/sizeformat.py index 2cae6935f5..cf8e2d1ee7 100644 --- a/horizon/templatetags/sizeformat.py +++ b/horizon/templatetags/sizeformat.py @@ -64,26 +64,25 @@ def filesizeformat(bytes, filesize_number_format): filesize_number_format(bytes / (1024 * 1024 * 1024 * 1024 * 1024)) +def float_cast_filesizeformat(value, multiplier=1, format=int_format): + try: + value = float(value) + value = filesizeformat(value * multiplier, format).replace(' ', '') + except (TypeError, ValueError): + value = value or '0 bytes' + return value + + @register.filter(name='mbformat') def mbformat(mb): - if not mb: - return 0 - return filesizeformat(mb * 1024 * 1024, int_format).replace(' ', '') + return float_cast_filesizeformat(mb, 1024 * 1024, int_format) @register.filter(name='mb_float_format') def mb_float_format(mb): - """Takes a size value in mb, and prints returns the data in a - saner unit. - """ - if not mb: - return 0 - return filesizeformat(mb * 1024 * 1024, float_format) + return float_cast_filesizeformat(mb, 1024 * 1024, float_format) @register.filter(name='diskgbformat') def diskgbformat(gb): - if not gb: - gb = 0 - return filesizeformat(gb * 1024 * 1024 * 1024, - float_format).replace(' ', '') + return float_cast_filesizeformat(gb, 1024 * 1024 * 1024, float_format) diff --git a/horizon/test/tests/templatetags.py b/horizon/test/tests/templatetags.py index a27e8958f1..da85a5f696 100644 --- a/horizon/test/tests/templatetags.py +++ b/horizon/test/tests/templatetags.py @@ -57,7 +57,20 @@ class TemplateTagTests(test.TestCase): size_str = ('5|diskgbformat', '10|diskgbformat', '5555|mb_float_format', '80|mb_float_format', '.5|mbformat', '0.005|mbformat', '0.0005|mbformat') - expected = u' 5.0GB 10.0GB 5.4 GB 80.0 MB 512KB 5KB 524Bytes ' + expected = u' 5.0GB 10.0GB 5.4GB 80.0MB 512KB 5KB 524Bytes ' + + text = '' + for size_filter in size_str: + text += '{{' + size_filter + '}} ' + + rendered_str = self.render_template(tag_require='sizeformat', + template_text=text) + self.assertEqual(rendered_str, expected) + + def test_size_format_filters_with_string(self): + size_str = ('"test"|diskgbformat', '"limit"|mb_float_format', + '"no limit"|mbformat') + expected = u' test limit no limit ' text = '' for size_filter in size_str: