Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14002 -- Updated filesize filter to support terabyte and petab…

…yte file sizes. Thanks to Aaron T. Myers for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13584 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 047d161164b2459434e6a17891f079f5f80d4cef 1 parent 4835d86
Russell Keith-Magee authored August 14, 2010
1  AUTHORS
@@ -344,6 +344,7 @@ answer newbie questions, and generally made Django that much better:
344 344
     James Murty
345 345
     msundstr
346 346
     Robert Myers <myer0052@gmail.com>
  347
+    Aaron T. Myers <atmyers@gmail.com>
347 348
     Alexander Myodov <alex@myodov.com>
348 349
     Nebojša Dorđević
349 350
     Doug Napoleone <doug@dougma.com>
18  django/template/defaultfilters.py
@@ -807,13 +807,19 @@ def filesizeformat(bytes):
807 807
     except (TypeError,ValueError,UnicodeDecodeError):
808 808
         return u"0 bytes"
809 809
 
810  
-    if bytes < 1024:
  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]:
811 819
         return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
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  
-    return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024))
  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)
817 823
 filesizeformat.is_safe = True
818 824
 
819 825
 def pluralize(value, arg=u's'):
9  tests/regressiontests/defaultfilters/tests.py
@@ -476,6 +476,15 @@
476 476
 >>> filesizeformat(1024*1024*1024)
477 477
 u'1.0 GB'
478 478
 
  479
+>>> filesizeformat(1024*1024*1024*1024)
  480
+u'1.0 TB'
  481
+
  482
+>>> filesizeformat(1024*1024*1024*1024*1024)
  483
+u'1.0 PB'
  484
+
  485
+>>> filesizeformat(1024*1024*1024*1024*1024*2000)
  486
+u'2000.0 PB'
  487
+
479 488
 >>> filesizeformat(complex(1,-1))
480 489
 u'0 bytes'
481 490
 

0 notes on commit 047d161

Please sign in to comment.
Something went wrong with that request. Please try again.