+
+ @validate(schema=ExpenditureSchema(), form='edit', variable_decode=True)
+ def update(self, id=None):
+ # Either create a new object, or, if we're editing, get the
+ # old one
+ if id is None:
+ e = model.Expenditure()
+ meta.Session.add(e)
+ op = 'created'
+ else:
+ e = meta.Session.query(model.Expenditure).get(id)
+ op = 'updated'
+
+ # Set the fields that were submitted
+ shares = self.form_result.pop('shares')
+ update_sar(e, self.form_result)
+ if e.id is not None:
+ e.update_split()
+
+ users = dict(meta.Session.query(model.User.id, model.User).all())
+ split_dict = {}
+ for share_params in shares:
+ user = users[share_params['user_id']]
+ split_dict[user] = Decimal(share_params['amount'])
+ e.split(split_dict)
+
+ meta.Session.commit()
+
+ show = ("Expenditure of %s paid for by %s %s." %
+ (e.amount, e.spender, op))
+ h.flash(show)
+
+ # Send email notification to involved users if they have an email set.
+ involved_users = set(sp.user for sp in e.splits if sp.share != 0)
+ involved_users.add(e.spender)
+ body = render('/emails/expenditure.txt',
+ extra_vars={'expenditure': e,
+ 'op': op})
+ g.handle_notification(involved_users, show, body)
+
+ return h.redirect_to('/')