]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/controllers/spend.py
Added UI for working with tags
[bluechips.git] / bluechips / controllers / spend.py
index d27ec40d2dc448507b8d50ef1c83a9bddaf61867..43293071bdca31592c60f10d575bd281a6872363 100644 (file)
@@ -6,6 +6,7 @@ from __future__ import division
 import logging
 
 import re
+import string
 from decimal import Decimal, InvalidOperation
 
 from bluechips.lib.base import *
@@ -42,6 +43,12 @@ class ExpenditureExpression(validators.FancyValidator):
         except:
             raise formencode.Invalid("Not a valid mathematical expression", value, state)
 
+class TagValidator(validators.FancyValidator):
+    def _to_python(self, value,state):
+        try:
+            return set(map(string.strip, value.split(',')))
+        except:
+            raise formencode.Invalid("Unable to parse tags", value, state)
 
 class ShareSchema(Schema):
     "Validate individual user shares."
@@ -56,6 +63,12 @@ def validate_state(value_dict, state, validator):
 ValidateNotAllZero = SimpleFormValidator(validate_state)
 
 
+def prune_tags():
+    for tag in meta.Session.query(model.Tag).all():
+        if not tag.expenditures:
+            meta.Session.delete(tag)
+    meta.Session.commit()
+
 class ExpenditureSchema(Schema):
     "Validate an expenditure."
     allow_extra_fields = False
@@ -63,6 +76,7 @@ class ExpenditureSchema(Schema):
     spender_id = validators.Int(not_empty=True)
     amount = model.types.CurrencyValidator(not_empty=True)
     description = validators.UnicodeString(not_empty=True)
+    tags = TagValidator()
     date = validators.DateConverter()
     shares = ForEach(ShareSchema)
     chained_validators = [ValidateNotAllZero]
@@ -87,8 +101,10 @@ class SpendController(BaseController):
                 user_id, user = user_row
                 val = 0
                 if user.resident:
-                    val = Decimal(100) / Decimal(num_residents)
+                    val = Decimal(1)
                 c.values['shares-%d.amount' % ii] = val
+
+            c.values['tags'] = u""
         else:
             c.title = 'Edit an Expenditure'
             c.expenditure = meta.Session.query(model.Expenditure).get(id)
@@ -102,6 +118,8 @@ class SpendController(BaseController):
                 share = shares_by_user.get(user, '')
                 c.values['shares-%d.amount' % ii] = share
 
+            c.values['tags'] = ', '.join(c.expenditure.tags)
+
         return render('/spend/index.mako')
 
     @redirect_on_get('edit')
@@ -122,6 +140,7 @@ class SpendController(BaseController):
         
         # Set the fields that were submitted
         shares = self.form_result.pop('shares')
+        tags = self.form_result.pop('tags') or set()
         update_sar(e, self.form_result)
 
         users = dict(meta.Session.query(model.User.id, model.User).all())
@@ -133,7 +152,9 @@ class SpendController(BaseController):
             split_dict[user] = amount
             split_text_dict[user] = amount_text
         e.split(split_dict, split_text_dict)
-        
+        e.tags.clear()
+        e.tags |= tags
+
         meta.Session.commit()
        
         show = ("Expenditure of %s paid for by %s %s." %
@@ -148,6 +169,8 @@ class SpendController(BaseController):
                                   'op': op})
         g.handle_notification(involved_users, show, body)
 
+        prune_tags()
+
         return h.redirect_to('/')
 
     def delete(self, id):
@@ -180,4 +203,6 @@ class SpendController(BaseController):
                                       'op': 'deleted'})
             g.handle_notification(involved_users, show, body)
 
+            prune_tags()
+
         return h.redirect_to('/')