##// END OF EJS Templates
added helper for filesize
marcink -
r97:be0096a0 default
parent child Browse files
Show More
@@ -4,6 +4,7 b' Consists of functions to typically be us'
4 available to Controllers. This module is available to both as 'h'.
4 available to Controllers. This module is available to both as 'h'.
5 """
5 """
6 from pylons import url
6 from pylons import url
7 from pylons.i18n.translation import _, ungettext
7 from webhelpers.html import (literal, HTML, escape)
8 from webhelpers.html import (literal, HTML, escape)
8 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate
9 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate
9 , mail_to, strip_links, strip_tags, tag_re)
10 , mail_to, strip_links, strip_tags, tag_re)
@@ -43,6 +44,27 b' class _GetError(object):'
43 if form_errors and form_errors.has_key(field_name):
44 if form_errors and form_errors.has_key(field_name):
44 return literal(tmpl % form_errors.get(field_name))
45 return literal(tmpl % form_errors.get(field_name))
45
46
47 class _FileSizeFormat():
48 """
49 Formats the value like a 'human-readable' file size (i.e. 13 KB, 4.1 MB,
50 102 bytes, etc).
51 """
52 def __call__(self, bytes):
53 try:
54 bytes = float(bytes)
55 except TypeError:
56 return u"0 bytes"
57
58 if bytes < 1024:
59 return ungettext("%(size)d byte", "%(size)d bytes", bytes) % {'size': bytes}
60 if bytes < 1024 * 1024:
61 return _("%.1f KB") % (bytes / 1024)
62 if bytes < 1024 * 1024 * 1024:
63 return _("%.1f MB") % (bytes / (1024 * 1024))
64 return _("%.1f GB") % (bytes / (1024 * 1024 * 1024))
65
66
67 filesizeformat = _FileSizeFormat()
46 link = _Link()
68 link = _Link()
47 flash = _Flash()
69 flash = _Flash()
48 get_error = _GetError()
70 get_error = _GetError()
General Comments 0
You need to be logged in to leave comments. Login now