]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/model/expenditure.py
brought up to date with latest sqlalchemy conventions
[bluechips.git] / bluechips / model / expenditure.py
index 009ba3a561a0024df244137c829d15c58fd20f11..f7e69344dc2cf88bf09cb7f0fa6ea9a81fdc4cb9 100644 (file)
@@ -28,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.
@@ -50,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()
         
@@ -65,12 +78,12 @@ 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)
 
 __all__ = ['Expenditure']