Skip to content

Commit 842ceb5

Browse files
committed
Fixed #14002 -- Modified filesize filter to ensure strings are translatable. Thanks to claudep for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13594 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 01720c9 commit 842ceb5

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

django/template/defaultfilters.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -807,19 +807,17 @@ def filesizeformat(bytes):
807807
except (TypeError,ValueError,UnicodeDecodeError):
808808
return u"0 bytes"
809809

810-
BYTE_UNITS = (
811-
('KB', 1024),
812-
('MB', 1024 * 1024),
813-
('GB', 1024 * 1024 * 1024),
814-
('TB', 1024 * 1024 * 1024 * 1024),
815-
('PB', 1024 * 1024 * 1024 * 1024 * 1024)
816-
)
817-
818-
if bytes < BYTE_UNITS[0][1]:
810+
if bytes < 1024:
819811
return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
820-
for index, (unit, unit_size) in enumerate(BYTE_UNITS):
821-
if bytes < unit_size * 1024 or index == len(BYTE_UNITS) - 1:
822-
return ugettext("%.1f %s") % (bytes / unit_size, unit)
812+
if bytes < 1024 * 1024:
813+
return ugettext("%.1f KB") % (bytes / 1024)
814+
if bytes < 1024 * 1024 * 1024:
815+
return ugettext("%.1f MB") % (bytes / (1024 * 1024))
816+
if bytes < 1024 * 1024 * 1024 * 1024:
817+
return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
818+
if bytes < 1024 * 1024 * 1024 * 1024 * 1024:
819+
return ugettext("%.1f TB") % (bytes / (1024 * 1024 * 1024 * 1024))
820+
return ugettext("%.1f PB") % (bytes / (1024 * 1024 * 1024 * 1024 * 1024))
823821
filesizeformat.is_safe = True
824822

825823
def pluralize(value, arg=u's'):

0 commit comments

Comments
 (0)