Beispiel #1
0
    def _from_python_processing(self):
        # process processors
        value = self.defaultval

        # 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):
            for processor, msg in self.processors:
                value = processor.from_python(value)
        self._displayval = value
Beispiel #2
0
    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
Beispiel #3
0
 def required_empty_test(self, value):
     return is_empty(value)
Beispiel #4
0
 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({''})