X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=bluechips%2Fmodel%2Fexpenditure.py;h=21172b9bcb046185e36e7c9ebf369651585623ac;hb=c603bfc958822b4226bdadc9423f5d2ee36074be;hp=acd77852172f595466dd34bd84c07ac2f836d178;hpb=54a4d9536f79008479ef41bfc328038945436a7b;p=bluechips.git diff --git a/bluechips/model/expenditure.py b/bluechips/model/expenditure.py index acd7785..21172b9 100644 --- a/bluechips/model/expenditure.py +++ b/bluechips/model/expenditure.py @@ -28,16 +28,6 @@ class Expenditure(object): 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. @@ -60,8 +50,11 @@ class Expenditure(object): total = sum(split_dict.itervalues()) - for user, share in split_dict.iteritems(): - split_dict[user] = share / total + for user, share in split_dict.items(): + if share == 0: + del split_dict[user] + else: + split_dict[user] = share / total amounts_dict = dict() @@ -75,12 +68,24 @@ class Expenditure(object): winner = random.choice(amounts_dict.keys()) amounts_dict[winner] += Currency(1) elif difference < 0: - for i in xrange(difference): + for i in xrange(-difference): winner = random.choice(amounts_dict.keys()) amounts_dict[winner] -= Currency(1) for user, share in amounts_dict.iteritems(): s = Split(self, user, share) - meta.Session.save(s) + meta.Session.add(s) + + def involves(self, user): + "Returns True if ``user`` is involved in this expenditure." + 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``." + shares = dict((split.user, split.share) + for split in self.splits) + return shares.get(user, Currency(0)) __all__ = ['Expenditure']