]> asedeno.scripts.mit.edu Git - bluechips.git/blob - bluechips/lib/helpers.py
Update BlueChips for WebHelpers 1.0 and beyond
[bluechips.git] / bluechips / lib / helpers.py
1 """Helper functions
2
3 Consists of functions to typically be used within templates, but also
4 available to Controllers. This module is available to both as 'h'.
5 """
6 from datetime import date
7 from decimal import Decimal
8
9 from pylons import request
10 from routes import url_for, redirect_to
11 from webhelpers.html import escape, literal, url_escape
12 from webhelpers.html.tags import *
13 from webhelpers.pylonslib.secure_form import *
14
15 from webhelpers.pylonslib import Flash as _Flash
16
17
18 def currency(name, value, *args, **kwargs):
19     if 'class_' not in kwargs:
20         kwargs['class_'] = ''
21     kwargs['class_'] += 'currency'
22     value = "%0.2f" % (int(value) / 100.)
23     return text(name, value, *args, **kwargs)
24
25
26 def grab(obj, attr):
27     if obj:
28         return getattr(obj, attr)
29     else:
30         if attr == 'date':
31             return date.today()
32         elif attr in ('spender_id', 'creditor_id', 'debtor_id'):
33             return request.environ['user'].id
34         elif attr == 'amount':
35             return 0
36         else:
37             return ''
38
39 flash = _Flash()