]> asedeno.scripts.mit.edu Git - bluechips.git/blobdiff - bluechips/controllers/spend.py
Tags that are associated with any expenditures are automatically pruned
[bluechips.git] / bluechips / controllers / spend.py
index 9caa1b390defd714c292efb431cae9ff506a68ff..43293071bdca31592c60f10d575bd281a6872363 100644 (file)
@@ -63,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
@@ -112,7 +118,7 @@ class SpendController(BaseController):
                 share = shares_by_user.get(user, '')
                 c.values['shares-%d.amount' % ii] = share
 
-            c.values['tags'] = ', '.join([tag.tag for tag in c.expenditure.tags])
+            c.values['tags'] = ', '.join(c.expenditure.tags)
 
         return render('/spend/index.mako')
 
@@ -146,7 +152,8 @@ class SpendController(BaseController):
             split_dict[user] = amount
             split_text_dict[user] = amount_text
         e.split(split_dict, split_text_dict)
-        e.tag(tags)
+        e.tags.clear()
+        e.tags |= tags
 
         meta.Session.commit()
        
@@ -162,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):
@@ -194,4 +203,6 @@ class SpendController(BaseController):
                                       'op': 'deleted'})
             g.handle_notification(involved_users, show, body)
 
+            prune_tags()
+
         return h.redirect_to('/')