]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/model/expenditure.py
Make navigation links work with edit pages
[bluechips.git] / bluechips / model / expenditure.py
index b2cf417f2de491dd18d27000660511e5db09e267..acd77852172f595466dd34bd84c07ac2f836d178 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.
@@ -61,10 +80,7 @@ class Expenditure(object):
                 amounts_dict[winner] -= Currency(1)
         
         for user, share in amounts_dict.iteritems():
-            s = Split()
-            s.expenditure = self
-            s.user = user
-            s.share = share
+            s = Split(self, user, share)
             meta.Session.save(s)
 
 __all__ = ['Expenditure']