Skip to content

Commit

Permalink
Fixed do_filesizeformat to actually calculate correctly, fixes pallet…
Browse files Browse the repository at this point in the history
  • Loading branch information
EnTeQuAk committed Oct 7, 2011
1 parent 898975d commit 7d268be
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 4 additions & 4 deletions jinja2/filters.py
Expand Up @@ -361,10 +361,10 @@ def do_filesizeformat(value, binary=False):
return '%d Bytes' % bytes
else:
for i, prefix in enumerate(prefixes):
unit = base ** (i + 1)
if bytes <= unit:
return '%.1f %s' % ((bytes / unit), prefix)
return '%.1f %s' % ((bytes / unit), prefix)
unit = base ** (i + 2)
if bytes < unit:
return '%.1f %s' % ((base * bytes / unit), prefix)
return '%.1f %s' % ((base * bytes / unit), prefix)


def do_pprint(value, verbose=False):
Expand Down
20 changes: 19 additions & 1 deletion jinja2/testsuite/filters.py
Expand Up @@ -86,9 +86,27 @@ def test_filesizeformat(self):
out = tmpl.render()
self.assert_equal(out, (
'100 Bytes|1.0 kB|1.0 MB|1.0 GB|1.0 TB|100 Bytes|'
'1000 Bytes|1.0 MiB|0.9 GiB|0.9 TiB'
'1000 Bytes|976.6 KiB|953.7 MiB|931.3 GiB'
))

def test_filesizeformat_issue59(self):
tmpl = env.from_string(
'{{ 300|filesizeformat }}|'
'{{ 3000|filesizeformat }}|'
'{{ 3000000|filesizeformat }}|'
'{{ 3000000000|filesizeformat }}|'
'{{ 3000000000000|filesizeformat }}|'
'{{ 300|filesizeformat(true) }}|'
'{{ 3000|filesizeformat(true) }}|'
'{{ 3000000|filesizeformat(true) }}'
)
out = tmpl.render()
self.assert_equal(out, (
'300 Bytes|3.0 kB|3.0 MB|3.0 GB|3.0 TB|300 Bytes|'
'2.9 KiB|2.9 MiB'
))


def test_first(self):
tmpl = env.from_string('{{ foo|first }}')
out = tmpl.render(foo=range(10))
Expand Down

0 comments on commit 7d268be

Please sign in to comment.