7 from decimal import Decimal, InvalidOperation
9 from bluechips.lib.base import *
11 from pylons import request, app_globals as g
12 from pylons.decorators.rest import dispatch_on
13 from pylons.decorators import validate
14 from pylons.controllers.util import abort
16 from formencode import validators, Schema
17 from formencode.foreach import ForEach
18 from formencode.variabledecode import NestedVariables
20 from mailer import Message
22 log = logging.getLogger(__name__)
25 class ShareSchema(Schema):
26 "Validate individual user shares."
27 allow_extra_fields = False
28 user_id = validators.Int(not_empty=True)
29 amount = validators.Number(not_empty=True)
32 class ExpenditureSchema(Schema):
33 "Validate an expenditure."
34 allow_extra_fields = False
35 pre_validators = [NestedVariables()]
36 spender_id = validators.Int(not_empty=True)
37 amount = model.types.CurrencyValidator(not_empty=True)
38 description = validators.UnicodeString()
39 date = validators.DateConverter()
40 shares = ForEach(ShareSchema)
43 class SpendController(BaseController):
47 def edit(self, id=None):
48 c.users = meta.Session.query(model.User.id, model.User)
50 c.title = 'Add a New Expenditure'
51 c.expenditure = model.Expenditure()
52 c.expenditure.spender_id = request.environ['user'].id
54 num_residents = meta.Session.query(model.User).\
55 filter_by(resident=True).count()
56 # Pre-populate split percentages for an even split.
58 for ii, user_row in enumerate(c.users):
59 user_id, user = user_row
61 val = Decimal(100) / Decimal(num_residents)
64 c.values['shares-%d.amount' % ii] = val
66 c.title = 'Edit an Expenditure'
67 c.expenditure = meta.Session.query(model.Expenditure).get(id)
68 if c.expenditure is None:
71 for ii, user_row in enumerate(c.users):
72 user_id, user = user_row
74 share = [s.share for s in c.expenditure.splits
76 percent = (Decimal(100) * Decimal(int(share)) /
77 Decimal(int(c.expenditure.amount))).\
78 quantize(Decimal("0.001"))
81 c.values['shares-%d.amount' % ii] = percent
83 return render('/spend/index.mako')
85 @validate(schema=ExpenditureSchema(), form='edit', variable_decode=True)
86 def update(self, id=None):
87 # Either create a new object, or, if we're editing, get the
90 e = model.Expenditure()
94 e = meta.Session.query(model.Expenditure).get(id)
99 # Set the fields that were submitted
100 shares = self.form_result.pop('shares')
101 update_sar(e, self.form_result)
105 users = dict(meta.Session.query(model.User.id, model.User).all())
107 for share_params in shares:
108 user = users[share_params['user_id']]
109 split_dict[user] = Decimal(str(share_params['amount']))
112 meta.Session.commit()
114 show = ("Expenditure of %s paid for by %s %s." %
115 (e.amount, e.spender, op))
118 # Send email notification to involved users if they have an email set.
119 involved_users = set(sp.user for sp in e.splits if sp.share != 0)
120 involved_users.add(e.spender)
121 body = render('/emails/expenditure.txt',
122 extra_vars={'expenditure': e,
124 g.handle_notification(involved_users, show, body)
126 return h.redirect_to('/')