split_percentage = Decimal(100) / Decimal(residents.count())
self.split(dict((resident, split_percentage) for resident in residents))
- def update_split(self):
- """
- Re-split an expenditure using the same percentages as what is
- currently in the database
- """
-
- old_splits = meta.Session.query(Split).filter(Split.expenditure==self)
- split_dict = dict((s.user, Decimal(int(s.share))) for s in old_splits)
- self.split(split_dict)
-
def split(self, split_dict):
"""
Split up an expenditure.
def involves(self, user):
"Returns True if ``user`` is involved in this expenditure."
- return (meta.Session.query(Split.id).\
- filter(Split.expenditure == self).\
- filter(Split.user == user).\
- filter(Split.share != 0).first() is not None)
+ return (any((split.user == user) and (split.share != 0)
+ for split in self.splits) or
+ (self.spender == user))
def share(self, user):
"Return the share corresponding to ``user``."
- share = meta.Session.query(Split.share).\
- filter(Split.expenditure == self).\
- filter(Split.user == user).scalar()
- if share is None:
- return Currency(0)
- else:
- return share
+ shares = dict((split.user, split.share)
+ for split in self.splits)
+ return shares.get(user, Currency(0))
__all__ = ['Expenditure']