Ticket #14002: bigger-filesizeformat.2.diff
File bigger-filesizeformat.2.diff, 1.7 KB (added by , 14 years ago) |
---|
-
django/template/defaultfilters.py
803 803 except (TypeError,ValueError,UnicodeDecodeError): 804 804 return u"0 bytes" 805 805 806 if bytes < 1024: 806 BYTE_UNITS = ( 807 ('KB', 1024), 808 ('MB', 1024 * 1024), 809 ('GB', 1024 * 1024 * 1024), 810 ('TB', 1024 * 1024 * 1024 * 1024), 811 ('PB', 1024 * 1024 * 1024 * 1024 * 1024) 812 ) 813 814 if bytes < BYTE_UNITS[0][1]: 807 815 return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes} 808 if bytes < 1024 * 1024: 809 return ugettext("%.1f KB") % (bytes / 1024) 810 if bytes < 1024 * 1024 * 1024: 811 return ugettext("%.1f MB") % (bytes / (1024 * 1024)) 812 return ugettext("%.1f GB") % (bytes / (1024 * 1024 * 1024)) 816 for index, (unit, unit_size) in enumerate(BYTE_UNITS): 817 if bytes < unit_size * 1024 or index == len(BYTE_UNITS) - 1: 818 return ugettext("%.1f %s") % (bytes / unit_size, unit) 813 819 filesizeformat.is_safe = True 814 820 815 821 def pluralize(value, arg=u's'): -
tests/regressiontests/defaultfilters/tests.py
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