+class ExpenditureExpression(validators.FancyValidator):
+ goodChars = set('1234567890.+-/*() ')
+
+ def _to_python(self, value, state):
+ if (not set(value) <= self.goodChars or
+ re.search(r'([\+\-\*\/])\1', value)):
+ raise formencode.Invalid("Expression contains illegal characters", value, state)
+
+ if value == '':
+ return value, Decimal("0")
+
+ try:
+ number = eval(value)
+ return value, Decimal(str(number))
+ 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)