def _to_python(self, value, state): value = super(DeprecatedNotOneValidator, self)._to_python( value, state) if value == self.number: raise Invalid(self.message( 'custom', state, number=self.number), value, state) return value
def _attempt_convert(self, value, state, validate): value = super(AllAndNotOneValidator, self)._attempt_convert(value, state, validate) if value == self.number: raise Invalid(self.message('custom', state, number=self.number), value, state) return value
def validate_python(self, value, state): response = captcha.submit(state.POST.get('recaptcha_challenge_field'), value, state.registry.settings['recaptcha.private'], state.environ['REMOTE_ADDR']) if not response.is_valid: raise Invalid(self.message(response.error_code, state), value, state)
def _to_python(self, value, state=None): try: filename = value.filename except AttributeError: filename = None if not filename and self.not_empty: raise Invalid(self.message('notEmpty', state), value, state) return value
def _to_python(self, value, state): """Parse a string and return a datetime object.""" if value and isinstance(value, datetime): return value else: try: format = self.format if callable(format): format = format() tpl = time.strptime(value, format) except ValueError: raise Invalid(self.message('badFormat', state), value, state) # shoudn't use time.mktime() because it can give OverflowError, # depending on the date (e.g. pre 1970) and underlying C library return datetime(year=tpl.tm_year, month=tpl.tm_mon, day=tpl.tm_mday, hour=tpl.tm_hour, minute=tpl.tm_min, second=tpl.tm_sec)
def _validate_python(self, value_dict, state=None): if 'join_type' not in value_dict.keys() and 'join_other_col' not in value_dict.keys() \ and 'join_self_col' not in value_dict.keys(): return None else: extraction = tmpl_context.extraction df = extraction.sample try: dataset = DBSession.query(DataSet).get( int(value_dict['datasetid'])) pd.merge(df, dataset.sample, how=value_dict['join_type'], left_on=value_dict['join_other_col'], right_on=value_dict['join_self_col'], suffixes=('', '_j_' + dataset.name.lower())) return None except ValueError as ex: raise Invalid(ex.__repr__(), value_dict, state, error_dict={})
class email_list_validator(FancyValidator): def __init__(self, *args, **kwargs): FancyValidator.__init__(self, *args, **kwargs) self.email = Email() def _to_python(self, value, state=None): """Validate a comma separated list of email addresses.""" emails = [x.strip() for x in value.split(',')] good_emails = [] messages = [] for addr in emails: try: good_emails.append(self.email.to_python(addr, state)) except Invalid, e: messages.append(str(e)) if messages: raise Invalid("; ".join(messages), value, state) else: return ", ".join(good_emails)
def _validate_python(self, value, state): if value == 4: raise Invalid(self.message('custom', state, number='four'), value, state)
def _validate_other(self, value, state): if value == '3': raise Invalid(self.message('custom', state, number='three'), value, state)
def _convert_from_python(self, value, state): if value == 2: raise Invalid(self.message('custom', state, number='two'), value, state) return str(value)
def _convert_to_python(self, value, state): if value == '1': raise Invalid(self.message('custom', state, number='one'), value, state) return int(value)
def raise_error_bad_url(self, value, state): msg = _('That is not a valid URL.') raise Invalid(msg, value, state)
def _to_python(self, value, state): dbsession = DBSession() if dbsession.query(User).filter(User.email == value).count() != 0: raise Invalid('That email address is already registered', value, state) return value
def _to_python(self, value, state): if value != self.get_hash(): msg = "Form token mismatch! Please try resubmitting the form." raise Invalid(msg, value, state) return None
def _to_python(self, value, state): """Parse a string and return a float or integer.""" try: return format.parse_decimal(value) except ValueError: raise Invalid(self.message('badFormat', state), value, state)
def to_python(self, value, state=None): try: return super(MultipleSelection, self).to_python(value, state) except Invalid: raise Invalid(_("Please select at least a value"), value, state)