]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/model/expenditure.py
emphasize transactions that the logged-in user is part of, deemphasize others
[bluechips.git] / bluechips / model / expenditure.py
index b2cf417f2de491dd18d27000660511e5db09e267..dda06c1e0a9c7bf837e3668eef7835dc5cf2ca70 100644 (file)
@@ -3,9 +3,18 @@ from bluechips.model.split import Split
 from bluechips.model import meta
 from bluechips.model.types import Currency
 from decimal import Decimal
+from datetime import datetime
 import random
 
 class Expenditure(object):
+    def __init__(self, spender=None, amount=Currency(0), description=u"",
+                 date=None):
+        self.spender = spender
+        self.amount = amount
+        self.description = description
+        if self.date == None:
+            self.date = datetime.now()
+    
     def __repr__(self):
         return '<Expenditure: spender: %s spent: %s>' % (self.spender,
                                                          self.amount)
@@ -19,6 +28,16 @@ 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.
@@ -41,8 +60,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()
         
@@ -56,15 +78,19 @@ 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()
-            s.expenditure = self
-            s.user = user
-            s.share = share
-            meta.Session.save(s)
+            s = Split(self, user, share)
+            meta.Session.add(s)
+
+    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)
 
 __all__ = ['Expenditure']