]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/controllers/spend.py
ripped out toscawidgets, replaced with formencode, put split editing on main expendit...
[bluechips.git] / bluechips / controllers / spend.py
index a775084d4e8c1de57818e0a0d9792a924b381a88..aa6a59c05f6c8952831d55b3d32a5eefa53475f9 100644 (file)
@@ -4,41 +4,80 @@ Handle expenditures
 
 import logging
 
+from decimal import Decimal, InvalidOperation
+
 from bluechips.lib.base import *
-from bluechips.widgets import spend
-from bluechips.lib.split import *
 
 from pylons import request
+from pylons.decorators.rest import dispatch_on
+from pylons.decorators import validate
+
+from formencode import validators, Schema
+from formencode.foreach import ForEach
+from formencode.variabledecode import NestedVariables
 
 log = logging.getLogger(__name__)
 
+
+class ShareSchema(Schema):
+    "Validate individual user shares."
+    allow_extra_fields = False
+    user_id = validators.Int(not_empty=True)
+    amount = validators.Number(not_empty=True)
+
+
+class ExpenditureSchema(Schema):
+    "Validate an expenditure."
+    allow_extra_fields = False
+    pre_validators = [NestedVariables()]
+    spender_id = validators.Int(not_empty=True)
+    amount = validators.Number(not_empty=True)
+    description = validators.UnicodeString()
+    date = validators.DateConverter()
+    shares = ForEach(ShareSchema)
+    
+
 class SpendController(BaseController):
     def index(self):
-        c.expenditure = dict()
-        c.expenditure['spender'] = request.environ['user']
-        
-        return render('/spend/index.mako')
+        return self.edit()
     
-    @validate(form=spend.new_spend_form, error_handler='index')
-    def new(self):
-        e = model.Expenditure()
+    def edit(self, id=None):
+        c.users = meta.Session.query(model.User.id, model.User)
+        if id is None:
+            c.title = 'Add a New Expenditure'
+            c.expenditure = model.Expenditure()
+            c.expenditure.spender_id = request.environ['user'].id
+        else:
+            c.title = 'Edit an Expenditure'
+            c.expenditure = meta.Session.query(model.Expenditure).get(id)
+        return render('/spend/index.mako')
+
+    @validate(schema=ExpenditureSchema(), form='edit', variable_decode=True)
+    def update(self, id=None):
+        # Either create a new object, or, if we're editing, get the
+        # old one
+        if id is None:
+            e = model.Expenditure()
+            meta.Session.add(e)
+        else:
+            e = meta.Session.query(model.Expenditure).get(id)
+        
+        # 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)
-        meta.Session.save(e)
+        if e.id is not None:
+            e.update_split()
+
+        users = dict(meta.Session.query(model.User.id, model.User).all())
+        split_dict = {}
+        for share_params in shares:
+            user = users[share_params['user_id']]
+            split_dict[user] = Decimal(share_params['amount'])
+        e.split(split_dict)
         
-        even_split(e)
         meta.Session.commit()
         
-        h.flash('Expenditure recorded.')
-        h.flash("""Want to do something unusual?
-
-<ul id="expenditure_options">
-  <li>%s</li>
-  <li>%s</li>
-</ul>""" % (h.link_to('Change the split', h.url_for(controller='spend',
-                                                   action='split',
-                                                   id=e.id)),
-           h.link_to('Spin off a subitem', h.url_for(controller='spend',
-                                                     action='subitem',
-                                                     id=e.id))))
-        
+        h.flash('Expenditure updated.')
+       
         return h.redirect_to('/')