c.values = {}
for ii, user_row in enumerate(c.users):
user_id, user = user_row
+ val = 0
if user.resident:
val = Decimal(100) / Decimal(num_residents)
- else:
- val = 0
c.values['shares-%d.amount' % ii] = val
else:
c.title = 'Edit an Expenditure'
c.values = {}
for ii, user_row in enumerate(c.users):
user_id, user = user_row
- try:
- share = [s.share for s in c.expenditure.splits
- if s.user == user][0]
- if c.expenditure.amount == 0:
- percent = 0
- else:
- percent = (Decimal(100) * Decimal(int(share)) /
- Decimal(int(c.expenditure.amount))).\
- quantize(Decimal("0.001"))
- except IndexError:
+ shares_by_user = dict(((sp.user, sp.share) for sp
+ in c.expenditure.splits))
+ share = shares_by_user.get(user, 0)
+ if c.expenditure.amount == 0:
percent = 0
+ else:
+ percent = (Decimal(100) * Decimal(int(share)) /
+ Decimal(int(c.expenditure.amount))).\
+ quantize(Decimal("0.001"))
c.values['shares-%d.amount' % ii] = percent
return render('/spend/index.mako')
from datetime import date
+from formencode import Invalid
+
from bluechips.tests import *
from bluechips import model
from bluechips.model import meta
+from bluechips.model.types import Currency
+
+from bluechips.controllers.spend import ExpenditureSchema
class TestSpendController(TestController):
filter_by(name=u'Charlie Root').one()
form['spender_id'] = user.id
- form['amount'] = '66.78'
+ form['amount'] = '74.04'
# Make sure date is today.
today = date.today()
assert form['date'].value == today.strftime('%m/%d/%Y')
form['description'] = 'A test expenditure'
form['shares-0.amount'] = '1'
form['shares-1.amount'] = '2'
- form['shares-2.amount'] = '3'
- form['shares-3.amount'] = '4'
+ form['shares-2.amount'] = '2'
+ form['shares-3.amount'] = '1'
+ for ii in range(4):
+ assert int(form['shares-%d.user_id' % ii].value) == ii + 1
response = form.submit()
response = response.follow()
e = meta.Session.query(model.Expenditure).\
order_by(model.Expenditure.id.desc()).first()
assert e.spender.name == u'Charlie Root'
- assert e.amount == 6678
+ assert e.amount == 7404
assert e.date == today
assert e.description == u'A test expenditure'
# Test the split.
+ shares = dict(((sp.user_id, sp.share)
+ for sp in e.splits))
+ assert shares[1] == Currency('12.34')
+ assert shares[2] == Currency('24.68')
+ assert shares[3] == Currency('24.68')
+ assert shares[4] == Currency('12.34')
def test_edit(self):
action='edit',
id=124234), status=404)
+ def test_update_nonexistent(self):
+ response = self.app.post(url_for(controller='spend',
+ action='update',
+ id=14234),
+ params=self.sample_post,
+ status=404)
+
+ def test_all_zero_shares_fails(self):
+ params = self.sample_post.copy()
+ for ii in range(4):
+ params['shares-%d.amount' % ii] = '0'
+ v = ExpenditureSchema()
+ try:
+ v.to_python(params)
+ except Invalid:
+ pass
+
+ def setUp(self):
+ self.sample_post = {
+ 'spender_id': '1',
+ 'amount': '44.12',
+ 'date': '10/5/2008',
+ 'description': 'Example expenditure post data.',
+ 'shares-0.user_id': '1',
+ 'shares-0.amount': '1',
+ 'shares-1.user_id': '2',
+ 'shares-1.amount': '1',
+ 'shares-2.user_id': '3',
+ 'shares-2.amount': '1',
+ 'shares-3.user_id': '4',
+ 'shares-3.amount': '1'}
+
def tearDown(self):
expenditures = meta.Session.query(model.Expenditure).all()
for e in expenditures:
action='edit',
id=21424), status=404)
+ def test_update_nonexistent(self):
+ response = self.app.post(url_for(controller='transfer',
+ action='update',
+ id=21424),
+ params=self.sample_params,
+ status=404)
+
+ def test_update_get_redirects(self):
+ response = self.app.get(url_for(controller='transfer',
+ action='update'),
+ status=302)
+ assert (dict(response.headers)['location'] ==
+ url_for(controller='transfer', action='edit', qualified=True))
+
+ def setUp(self):
+ self.sample_params = {
+ 'debtor_id': '1',
+ 'creditor_id': '2',
+ 'amount': '33.98',
+ 'date': '4/1/2007',
+ 'description': 'Example transfer params.'}
+
def tearDown(self):
transfers = meta.Session.query(model.Transfer).all()
for t in transfers: