def handle_exception(self, exc): def can_handle(error_msg): self._valid = False if is_notgiven(error_msg): error_msg = str(exc) self.add_error(error_msg) return True # try element handlers first for el in self.submittable_els: if el.handle_exception(exc): return True for looking_for, error_msg, exc_type, callback in self._exception_handlers: if not is_notgiven(exc_type): if isinstance(exc_type, six.string_types): if exc.__class__.__name__ != exc_type: continue else: if not isinstance(exc, exc_type): continue if is_notgiven(callback): if is_notgiven(looking_for): return can_handle(error_msg) elif looking_for in str(exc): return can_handle(error_msg) else: return callback(exc) return False
def handle_exception(self, exc): def can_handle(error_msg): self._valid = False if is_notgiven(error_msg): error_msg = str(exc) self.add_error(error_msg) return True for looking_for, error_msg, exc_type, callback in self.exception_handlers: if is_notgiven(callback): if not is_notgiven(exc_type): if isinstance(exc_type, str): if exc.__class__.__name__ != exc_type: continue else: if not isinstance(exc, exc_type): continue if is_notgiven(looking_for): return can_handle(error_msg) elif looking_for in str(exc): return can_handle(error_msg) else: if callback(exc): return can_handle(error_msg) return False
def _to_python_processing(self): """ if "choose" value was chosen, we need to return an emtpy value appropriate to `multi` """ FormFieldElementBase._to_python_processing(self) # multiple select fields should always return a list if self.multiple and not is_notgiven(self._safeval): self._safeval = tolist(self._safeval)
def _to_python_processing(self): # if the value has already been processed, don't process it again if self._valid is not None: return valid = True value = self.submittedval if is_notgiven(value): value = BaseTranslator(None, None, 0) if value.is_uploaded: _, ext = path.splitext(value.file_name) ext = ext.lower() if not ext and (self._allowed_exts or self._denied_exts): valid = False self.add_error('extension requirement exists, but submitted file had no extension') if self._allowed_exts and ext not in self._allowed_exts: valid = False self.add_error('extension "%s" not allowed' % ext) if self._denied_exts and ext in self._denied_exts: valid = False self.add_error('extension "%s" not permitted' % ext) if value.content_type: if self._allowed_types and value.content_type not in self._allowed_types: valid = False self.add_error('content type "%s" not allowed' % value.content_type) if self._denied_types and value.content_type in self._denied_types: valid = False self.add_error('content type "%s" not permitted' % value.content_type) elif value.content_type is not None and (self._allowed_types or self._denied_types): valid = False self.add_error('content-type requirements exist, but submitted file had ' 'no content-type') if self._maxsize: if not value.content_length: valid = False self.add_error('maximum size requirement exists, but submitted file had ' 'no content length') elif value.content_length > self._maxsize: valid = False self.add_error('file too big (%s), max size %s' % (value.content_length, self._maxsize)) elif self.required: valid = False self.add_error('field is required') self._valid = valid if valid: self._safeval = self.submittedval
def test_is_empty(self): assert is_empty(NotGiven) assert is_notgiven(NotGivenIter) assert is_empty(None) assert is_empty('') assert is_empty([]) assert is_empty({}) assert is_empty(set()) assert not is_empty('foo') assert not is_empty(False) assert not is_empty(0) assert not is_empty(0.0) assert not is_empty(Decimal(0)) assert not is_empty([0]) assert not is_empty({'foo': 'bar'}) assert not is_empty({''})
def _to_python(self, value, state): field = state multiple = getattr(field, 'multiple', False) if self.multi_check: if not multiple: if is_iterable(value): raise Invalid(self.message('nonmultiple', state), value, state) # now apply the validator to the value if not multiple or is_notgiven(value) or \ getattr(self.validator, 'handles_multiples', False): return self.validator.to_python(value, state) else: retval = [] for v in tolist(value): retval.append(self.validator.to_python(v, state)) return retval
def displayval(self): if is_notgiven(self.submittedval) or self.fixed: return HasValueElement.displayval.fget(self) return self.submittedval
def can_handle(error_msg): self._valid = False if is_notgiven(error_msg): error_msg = str(exc) self.add_error(error_msg) return True
def _to_python_processing(self): # noqa """ filters, validates, and converts the submitted value based on element settings and processors """ # if the value has already been processed, don't process it again if self._valid is not None: return valid = True value = self.submittedval # strip if necessary if self.strip and isinstance(value, str): value = value.strip() elif self.strip and is_iterable(value): newvalue = [] for item in value: if isinstance(item, str): newvalue.append(item.strip()) else: newvalue.append(item) if newvalue: value = newvalue # if nothing was submitted, but we have an if_missing, substitute if is_notgiven(value) and self.if_missing is not NotGiven: value = self.if_missing # handle empty or missing submit value with if_empty if is_empty(value) and self.if_empty is not NotGiven: value = self.if_empty # standardize all empty values as None if if_empty not given elif is_empty(value) and not is_notgiven(value): value = None # process required if self.required and self.required_empty_test(value): valid = False self.add_error('field is required') # process processors for processor, msg in self.processors: try: processor = MultiValues(processor) ap_value = processor.to_python(value, self) # FormEncode takes "empty" values and returns None # Since NotGiven == '', FormEncode thinks its empty # and returns None on us. We override that here. if ap_value is not None or value is not NotGiven: value = ap_value except formencode.Invalid as e: valid = False self.add_error((msg or str(e))) else: # we rely on MultiValues for this, but if no processor, # it doesn't get called if getattr(self, 'multiple', False) and not is_iterable(value): value = tolist(value) ### # Doing these again in case the processors changed the value ### # handle empty or missing submit value with if_empty if is_empty(value) and self.if_empty is not NotGiven: value = self.if_empty # standardize all empty values as None if if_empty not given elif is_empty(value) and not is_notgiven(value): value = None # process required if self.required and self.required_empty_test(value) and \ 'field is required' not in self.errors: valid = False self.add_error('field is required') # If its empty, there is no reason to run the converters. By default, # the validators don't do anything if the value is empty and they WILL # try to convert our NotGiven value, which we want to avoid. Therefore, # just skip the conversion. if not is_empty(value): # process type conversion if self.vtype is not NotGiven: if self.vtype in ('boolean', 'bool'): tvalidator = formencode.compound.Any(fev.Bool(), fev.StringBool()) elif self.vtype in ('integer', 'int'): tvalidator = fev.Int elif self.vtype in ('number', 'num', 'float'): tvalidator = fev.Number elif self.vtype in ('decimal'): tvalidator = Decimal elif self.vtype in ('str', 'string'): tvalidator = fev.String elif self.vtype in ('unicode', 'uni'): tvalidator = fev.UnicodeString try: tvalidator = MultiValues(tvalidator, multi_check=False) value = tvalidator.to_python(value, self) except formencode.Invalid as e: valid = False self.add_error(str(e)) # save if valid: self._safeval = value self._valid = True else: # is if_invalid if applicable if self.if_invalid is not NotGiven: # we might want to clear error messages too, but the extra # detail probably won't hurt (for now) self._safeval = self.if_invalid self._valid = True else: self._valid = False
def displayval(self): if is_notgiven(self.submittedval): return super(FormFieldElementBase, self).displayval return self.submittedval
def test_is_notgiven(self): assert is_notgiven(NotGiven) assert is_notgiven(NotGivenIter) assert not is_notgiven(None)