7 from bluechips.lib.base import *
8 from bluechips.widgets import spend
10 from pylons import request
11 from pylons.decorators.rest import dispatch_on
13 from decimal import Decimal, InvalidOperation
15 log = logging.getLogger(__name__)
17 class SpendController(BaseController):
19 c.title = 'Add a New Expenditure'
21 c.expenditure = dict()
22 c.expenditure['spender'] = request.environ['user']
24 return render('/spend/index.mako')
27 c.title = 'Edit an Expenditure'
29 c.expenditure = meta.Session.query(model.Expenditure).get(id)
31 return render('/spend/index.mako')
33 def update(self, id=None):
34 # Validate the submission
35 if not valid(self, spend.new_spend_form):
41 # Either create a new object, or, if we're editing, get the
44 e = model.Expenditure()
47 e = meta.Session.query(model.Expenditure).get(id)
49 # Set the fields that were submitted
50 update_sar(e, self.form_result)
59 h.flash('Expenditure recorded.')
60 h.flash("""Want to do something unusual?
62 <ul id="expenditure_options">
65 </ul>""" % (h.link_to('Change the split', h.url_for(controller='spend',
68 h.link_to('Spin off a subitem', h.url_for(controller='spend',
72 return h.redirect_to('/')
74 @dispatch_on(POST='_post_split',
79 def _get_split(self, id):
80 c.title = 'Change Expenditure Split'
82 c.expenditure = meta.Session.query(model.Expenditure).get(id)
83 c.users = meta.Session.query(model.User)
85 return render('/spend/split.mako')
87 def _post_split(self, id):
88 c.values = request.params
93 for username, percent in c.values.iteritems():
95 user = meta.Session.query(model.User).\
96 filter(model.User.username==username).one()
97 split_dict[user] = Decimal(percent)
98 except InvalidOperation:
99 c.errors[username] = 'Please enter a number'
100 if c.errors != dict():
101 return self._get_split(id)
103 e = meta.Session.query(model.Expenditure).get(id)
106 meta.Session.commit()
108 h.flash('Expenditure redivided')
110 return h.redirect_to('/')