Commits

Tim Savage  committed 12cd043 Draft

Added humanize bytes method for file sizes

  • Participants
  • Parent commits 4fe7d14

Comments (0)

Files changed (2)

File django_extras/templatetags/__init__.py

+__author__ = 'tsavage'

File django_extras/templatetags/summary.py

+from django import template
+
+register = template.Library()
+
+
+@register.filter
+def humanize_bytes(bytes, precision=1):
+    """
+    Generate a humanized version of a file size.
+    :param bytes:
+    :param precision:
+    :return:
+    """
+    abbrevs = (
+        (1<<50L, 'PB'),
+        (1<<40L, 'TB'),
+        (1<<30L, 'GB'),
+        (1<<20L, 'MB'),
+        (1<<10L, 'kB'),
+        (1, 'bytes')
+    )
+    if bytes == 1:
+        return '1 byte'
+    for factor, suffix in abbrevs:
+        if bytes >= factor:
+            break
+    return '%.*f %s' % (precision, bytes / factor, suffix)