Author: russellm
Date: 2010-08-14 07:45:15 -0500 (Sat, 14 Aug 2010)
New Revision: 13584

Modified:
   django/trunk/AUTHORS
   django/trunk/django/template/defaultfilters.py
   django/trunk/tests/regressiontests/defaultfilters/tests.py
Log:
Fixed #14002 -- Updated filesize filter to support terabyte and petabyte file 
sizes. Thanks to Aaron T. Myers for the patch.

Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2010-08-14 12:33:31 UTC (rev 13583)
+++ django/trunk/AUTHORS        2010-08-14 12:45:15 UTC (rev 13584)
@@ -344,6 +344,7 @@
     James Murty
     msundstr
     Robert Myers <[email protected]>
+    Aaron T. Myers <[email protected]>
     Alexander Myodov <[email protected]>
     Nebojša Dorđević
     Doug Napoleone <[email protected]>

Modified: django/trunk/django/template/defaultfilters.py
===================================================================
--- django/trunk/django/template/defaultfilters.py      2010-08-14 12:33:31 UTC 
(rev 13583)
+++ django/trunk/django/template/defaultfilters.py      2010-08-14 12:45:15 UTC 
(rev 13584)
@@ -807,13 +807,19 @@
     except (TypeError,ValueError,UnicodeDecodeError):
         return u"0 bytes"
 
-    if bytes < 1024:
+    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]:
         return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': 
bytes}
-    if bytes < 1024 * 1024:
-        return ugettext("%.1f KB") % (bytes / 1024)
-    if bytes < 1024 * 1024 * 1024:
-        return ugettext("%.1f MB") % (bytes / (1024 * 1024))
-    return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
+    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)
 filesizeformat.is_safe = True
 
 def pluralize(value, arg=u's'):

Modified: django/trunk/tests/regressiontests/defaultfilters/tests.py
===================================================================
--- django/trunk/tests/regressiontests/defaultfilters/tests.py  2010-08-14 
12:33:31 UTC (rev 13583)
+++ django/trunk/tests/regressiontests/defaultfilters/tests.py  2010-08-14 
12:45:15 UTC (rev 13584)
@@ -476,6 +476,15 @@
 >>> filesizeformat(1024*1024*1024)
 u'1.0 GB'
 
+>>> filesizeformat(1024*1024*1024*1024)
+u'1.0 TB'
+
+>>> filesizeformat(1024*1024*1024*1024*1024)
+u'1.0 PB'
+
+>>> filesizeformat(1024*1024*1024*1024*1024*2000)
+u'2000.0 PB'
+
 >>> filesizeformat(complex(1,-1))
 u'0 bytes'
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to