Mercurial > public > src > rhodecode
comparison pylons_app/lib/helpers.py @ 97:be0096a02772
added helper for filesize
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 25 Apr 2010 00:12:34 +0200 |
parents | 0bb9391bc287 |
children | 01d0f363f36d |
comparison
equal
deleted
inserted
replaced
96:f24b9a2934cf | 97:be0096a02772 |
---|---|
2 | 2 |
3 Consists of functions to typically be used within templates, but also | 3 Consists of functions to typically be used within templates, but also |
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) |
10 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes, | 11 from webhelpers.html.tags import (auto_discovery_link, checkbox, css_classes, |
11 end_form, file, form, hidden, image, | 12 end_form, file, form, hidden, image, |
41 def __call__(self, field_name, form_errors): | 42 def __call__(self, field_name, form_errors): |
42 tmpl = """<span class="error_msg">%s</span>""" | 43 tmpl = """<span class="error_msg">%s</span>""" |
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() |