import logging
import re
+import string
from decimal import Decimal, InvalidOperation
from bluechips.lib.base import *
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."
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
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]
if user.resident:
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)
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')
# 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())
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." %
'op': op})
g.handle_notification(involved_users, show, body)
+ prune_tags()
+
return h.redirect_to('/')
def delete(self, id):
'op': 'deleted'})
g.handle_notification(involved_users, show, body)
+ prune_tags()
+
return h.redirect_to('/')