]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/controllers/spend.py
show different flash messages for created v. updated
[bluechips.git] / bluechips / controllers / spend.py
index aa6a59c05f6c8952831d55b3d32a5eefa53475f9..749d73039a4c34550d5baaadf607315f098b65b8 100644 (file)
@@ -31,7 +31,7 @@ class ExpenditureSchema(Schema):
     allow_extra_fields = False
     pre_validators = [NestedVariables()]
     spender_id = validators.Int(not_empty=True)
-    amount = validators.Number(not_empty=True)
+    amount = model.types.CurrencyValidator(not_empty=True)
     description = validators.UnicodeString()
     date = validators.DateConverter()
     shares = ForEach(ShareSchema)
@@ -47,6 +47,18 @@ class SpendController(BaseController):
             c.title = 'Add a New Expenditure'
             c.expenditure = model.Expenditure()
             c.expenditure.spender_id = request.environ['user'].id
+
+            num_residents = meta.Session.query(model.User).\
+                    filter_by(resident=True).count()
+            # Pre-populate split percentages for an even split.
+            c.values = {}
+            for ii, user_row in enumerate(c.users):
+                user_id, user = user_row
+                if user.resident:
+                    val = Decimal(100) / Decimal(num_residents)
+                else:
+                    val = 0
+                c.values['shares-%d.amount' % ii] = val
         else:
             c.title = 'Edit an Expenditure'
             c.expenditure = meta.Session.query(model.Expenditure).get(id)
@@ -64,7 +76,6 @@ class SpendController(BaseController):
         
         # Set the fields that were submitted
         shares = self.form_result.pop('shares')
-        e.amount = Decimal(self.form_result.pop('amount') * 100)
         update_sar(e, self.form_result)
         if e.id is not None:
             e.update_split()
@@ -78,6 +89,9 @@ class SpendController(BaseController):
         
         meta.Session.commit()
         
-        h.flash('Expenditure updated.')
+        if id is None:
+            h.flash("Expenditure created.")
+        else:
+            h.flash('Expenditure updated.')
        
         return h.redirect_to('/')