]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/model/expenditure.py
added a 'My Share' column to lists of expenditures
[bluechips.git] / bluechips / model / expenditure.py
index 58456fb8a71730d48c0a4936da2eabd1ee1384ca..296ecf04929aa2c3c66790d3f5a86a76cdcd88b9 100644 (file)
@@ -60,12 +60,6 @@ class Expenditure(object):
         
         total = sum(split_dict.itervalues())
         
-        for user, share in split_dict.items():
-            if share == 0:
-                del split_dict[user]
-            else:
-                split_dict[user] = share / total
-        
         for user, share in split_dict.items():
             if share == 0:
                 del split_dict[user]
@@ -84,12 +78,29 @@ 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 (meta.Session.query(Split.id).\
+                filter(Split.expenditure == self).\
+                filter(Split.user == user).\
+                filter(Split.share != 0).first() is not None)
+
+    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
 
 __all__ = ['Expenditure']