##// END OF EJS Templates
added helper for filesize
marcink -
r97:be0096a0 default
parent child Browse files
Show More
@@ -1,48 +1,70 b''
1 1 """Helper functions
2 2
3 3 Consists of functions to typically be used within templates, but also
4 4 available to Controllers. This module is available to both as 'h'.
5 5 """
6 6 from pylons import url
7 from pylons.i18n.translation import _, ungettext
7 8 from webhelpers.html import (literal, HTML, escape)
8 9 from webhelpers.html.tools import (auto_link, button_to, highlight, js_obfuscate
9 10 , mail_to, strip_links, strip_tags, tag_re)
10 11 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes,
11 12 end_form, file, form, hidden, image,
12 13 javascript_link, link_to, link_to_if,
13 14 link_to_unless, ol, required_legend,
14 15 select, stylesheet_link,
15 16 submit, text, password, textarea, title,
16 17 ul, xml_declaration)
17 18 from webhelpers.text import (chop_at, collapse, convert_accented_entities,
18 19 convert_misc_entities, lchop, plural, rchop,
19 20 remove_formatting, replace_whitespace, urlify)
20 21
21 22 from webhelpers.pylonslib import Flash as _Flash
22 23 from webhelpers.pylonslib.secure_form import secure_form
23 24
24 25 #Custom helper here :)
25 26 class _Link(object):
26 27 '''
27 28 Make a url based on label and url with help of url_for
28 29 @param label:name of link if not defined url is used
29 30 @param url: the url for link
30 31 '''
31 32
32 33 def __call__(self, label='', *url_, **urlargs):
33 34 if label is None or '':
34 35 label = url
35 36 link_fn = link_to(label, url(*url_, **urlargs))
36 37 return link_fn
37 38
38 39
39 40 class _GetError(object):
40 41
41 42 def __call__(self, field_name, form_errors):
42 43 tmpl = """<span class="error_msg">%s</span>"""
43 44 if form_errors and form_errors.has_key(field_name):
44 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 68 link = _Link()
47 69 flash = _Flash()
48 70 get_error = _GetError()
General Comments 0
You need to be logged in to leave comments. Login now