]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/controllers/spend.py
Show tags on expenditure deletion page
[bluechips.git] / bluechips / controllers / spend.py
index edc5ea26e5b78610616b202a2ac6efa437a13e26..60afaa7531f6b668888a11939dc36539f65a9f1c 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]
@@ -85,10 +99,9 @@ class SpendController(BaseController):
             c.values = {}
             for ii, user_row in enumerate(c.users):
                 user_id, user = user_row
-                val = 0
-                if user.resident:
-                    val = Decimal(1)
-                c.values['shares-%d.amount' % ii] = val
+                c.values['shares-%d.amount' % ii] = 1 if user.resident else ""
+
+            c.tags = u""
         else:
             c.title = 'Edit an Expenditure'
             c.expenditure = meta.Session.query(model.Expenditure).get(id)
@@ -102,6 +115,8 @@ class SpendController(BaseController):
                 share = shares_by_user.get(user, '')
                 c.values['shares-%d.amount' % ii] = share
 
+            c.tags = ', '.join(c.expenditure.tags)
+
         return render('/spend/index.mako')
 
     @redirect_on_get('edit')
@@ -122,6 +137,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 +149,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 +166,8 @@ class SpendController(BaseController):
                                   'op': op})
         g.handle_notification(involved_users, show, body)
 
+        prune_tags()
+
         return h.redirect_to('/')
 
     def delete(self, id):
@@ -155,6 +175,7 @@ class SpendController(BaseController):
         c.expenditure = meta.Session.query(model.Expenditure).get(id)
         if c.expenditure is None:
             abort(404)
+        c.tags = ', '.join(c.expenditure.tags)
 
         return render('/spend/delete.mako')
 
@@ -180,4 +201,6 @@ class SpendController(BaseController):
                                       'op': 'deleted'})
             g.handle_notification(involved_users, show, body)
 
+            prune_tags()
+
         return h.redirect_to('/')