## Controller Python Script "validate_txn" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind state=state ##bind subpath=traverse_subpath ##parameters=post_date='', description='', amount='', users=[] ##title=validates a new transaction if post_date != '' and context.restrict_post_dates: ok, msg = context.validate_new_txn_date(post_date) if not ok: state.setError("post_date", msg) if description == '': state.setError("description", "No description specified.") try: amount = float(amount) except ValueError: state.setError("amount", "Not a number.") except TypeError: state.setError("amount", "No amount specified.") if amount == '': state.setError("amount", "No amount specified.") if amount <= 0: state.setError('amount', "Not a positive amount.") if users is None or len(users) == 0: state.setError("users", "At least one user must be selected.") else: any_weight = False for u in users: wid = 'users_' + u + '_weight' weight = context.REQUEST.form.get(wid) if weight is not None and weight != '': any_weight = True empty_weight_errs = False if any_weight: for u in users: wid = 'users_' + u + '_weight' weight = context.REQUEST.form.get(wid) try: weight = float(weight) except ValueError: state.setError(wid, "Not a number.") except TypeError: state.setError(wid, "No proportion specified.") empty_weight_errs = True if weight == '': state.setError(wid, "No proportion specified.") empty_weight_errs = True if weight <= 0: state.setError(wid, "Not a positive proportion.") if empty_weight_errs: state.setError("users", """ If a proportion is specified for any selected user, a proportion must be specifed for all selected users. """) if state.getErrors(): return state.set(portal_status_message="Please correct the errors.", status="failure") else: return state.set(portal_status_message="Transaction is valid.")