From: Evan Broder Date: Fri, 25 Jul 2008 07:07:10 +0000 (+0000) Subject: Add a form to change an expenditure's split X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=commitdiff_plain;h=7c2374b6690fe10cfb8e02d63f0596919ceda85f;p=bluechips.git Add a form to change an expenditure's split --- diff --git a/bluechips/controllers/spend.py b/bluechips/controllers/spend.py index a3f502d..fd88453 100644 --- a/bluechips/controllers/spend.py +++ b/bluechips/controllers/spend.py @@ -8,6 +8,9 @@ from bluechips.lib.base import * from bluechips.widgets import spend from pylons import request +from pylons.decorators.rest import dispatch_on + +from decimal import Decimal, InvalidOperation log = logging.getLogger(__name__) @@ -67,3 +70,41 @@ class SpendController(BaseController): id=e.id)))) return h.redirect_to('/') + + @dispatch_on(POST='_post_split', + GET='_get_split') + def split(self, id): + abort(500) + + def _get_split(self, id): + c.title = 'Change Expenditure Split' + + c.expenditure = meta.Session.query(model.Expenditure).get(id) + c.users = meta.Session.query(model.User) + + return render('/spend/split.mako') + + def _post_split(self, id): + c.values = request.params + c.errors = dict() + + split_dict = dict() + + for username, percent in c.values.iteritems(): + try: + user = meta.Session.query(model.User).\ + filter(model.User.username==username).one() + split_dict[user] = Decimal(percent) + except InvalidOperation: + c.errors[username] = 'Please enter a number' + if c.errors != dict(): + return self._get_split(id) + + e = meta.Session.query(model.Expenditure).get(id) + e.split(split_dict) + + meta.Session.commit() + + h.flash('Expenditure redivided') + + return h.redirect_to('/') diff --git a/bluechips/templates/spend/split.mako b/bluechips/templates/spend/split.mako new file mode 100644 index 0000000..842466d --- /dev/null +++ b/bluechips/templates/spend/split.mako @@ -0,0 +1,51 @@ +<%inherit file="/base.mako"/> + +<%! +import itertools +from decimal import Decimal +%> + +<% +form_cycle = itertools.cycle(['even', 'odd']) +%> + +## ToscaWidgets doesn't support dynamically generating the form fields +## themselves, so I'm mimicing its style but generating the form by +## hand + +

Change how an expenditure is split up. Enter a percentage, or +something like a percentage, for each user. They don't have to add to +100.

+ +

You're editing an expenditure of ${c.expenditure.amount} by +${c.expenditure.spender.name} on ${c.expenditure.date}, described as +"${c.expenditure.description}"

+ +${h.form('', method='post')} +
+ +