Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #14002 -- Modified filesize filter to ensure strings are transl…

…atable. Thanks to claudep for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13594 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 842ceb5fe6758b54a22594f120f8180a902fe1cc 1 parent 01720c9
@freakboy3742 freakboy3742 authored
Showing with 10 additions and 12 deletions.
  1. +10 −12 django/template/defaultfilters.py
View
22 django/template/defaultfilters.py
@@ -807,19 +807,17 @@ def filesizeformat(bytes):
except (TypeError,ValueError,UnicodeDecodeError):
return u"0 bytes"
- BYTE_UNITS = (
- ('KB', 1024),
- ('MB', 1024 * 1024),
- ('GB', 1024 * 1024 * 1024),
- ('TB', 1024 * 1024 * 1024 * 1024),
- ('PB', 1024 * 1024 * 1024 * 1024 * 1024)
- )
-
- if bytes < BYTE_UNITS[0][1]:
+ if bytes < 1024:
return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
- for index, (unit, unit_size) in enumerate(BYTE_UNITS):
- if bytes < unit_size * 1024 or index == len(BYTE_UNITS) - 1:
- return ugettext("%.1f %s") % (bytes / unit_size, unit)
+ if bytes < 1024 * 1024:
+ return ugettext("%.1f KB") % (bytes / 1024)
+ if bytes < 1024 * 1024 * 1024:
+ return ugettext("%.1f MB") % (bytes / (1024 * 1024))
+ if bytes < 1024 * 1024 * 1024 * 1024:
+ return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
+ if bytes < 1024 * 1024 * 1024 * 1024 * 1024:
+ return ugettext("%.1f TB") % (bytes / (1024 * 1024 * 1024 * 1024))
+ return ugettext("%.1f PB") % (bytes / (1024 * 1024 * 1024 * 1024 * 1024))
filesizeformat.is_safe = True
def pluralize(value, arg=u's'):
Please sign in to comment.
Something went wrong with that request. Please try again.