Fixed #25441 -- Added support for negative filesize to filesizeformat template filter.

Thanks Andrey Yakovlev for the initial patch.
This commit is contained in:
Sambhav Satija 2015-10-25 07:21:20 +05:30 committed by Tim Graham
parent ea2f48ce8b
commit ce7dd1273e
2 changed files with 10 additions and 0 deletions

View File

@ -858,6 +858,10 @@ def filesizeformat(bytes):
TB = 1 << 40 TB = 1 << 40
PB = 1 << 50 PB = 1 << 50
negative = bytes < 0
if negative:
bytes = -bytes # Allow formatting of negative numbers.
if bytes < KB: if bytes < KB:
value = ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes} value = ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
elif bytes < MB: elif bytes < MB:
@ -871,6 +875,8 @@ def filesizeformat(bytes):
else: else:
value = ugettext("%s PB") % filesize_number_format(bytes / PB) value = ugettext("%s PB") % filesize_number_format(bytes / PB)
if negative:
value = "-%s" % value
return avoid_wrapping(value) return avoid_wrapping(value)

View File

@ -39,3 +39,7 @@ class FunctionTests(SimpleTestCase):
self.assertEqual(filesizeformat(complex(1, -1)), '0\xa0Bytes') self.assertEqual(filesizeformat(complex(1, -1)), '0\xa0Bytes')
self.assertEqual(filesizeformat(""), '0\xa0Bytes') self.assertEqual(filesizeformat(""), '0\xa0Bytes')
self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"), '0\xa0Bytes') self.assertEqual(filesizeformat("\N{GREEK SMALL LETTER ALPHA}"), '0\xa0Bytes')
def test_negative_numbers(self):
self.assertEqual(filesizeformat(-100), '-100\xa0bytes')
self.assertEqual(filesizeformat(-1024 * 1024 * 50), '-50.0\xa0MB')