def decorator(*args, **kwargs):
     val = func(*args, **kwargs)
     if not ins(val):
         raise TypeError
     for e in val:
         if not ins(e) or len(e) != 2:
             raise TypeError
     return tuple(val)
    def _clean_value(self, field, value, **kwargs):
        formfield = None

        # has highest precedence if defined on the class. this is usually NOT
        # necessary
        if self.formfield:
            formfield = self.formfield
        # special case for handling standalone ``None`` or ``bool`` values.
        # this occurs when a field is can be queried as a null value, i.e.
        # '-?isnull' : (True|False) or '-?exact' : None
        elif value is None or type(value) is bool:
            formfield = forms.NullBooleanField

        # create an instance of the formfield "to be" and determine if there is
        # a mapping listed for it. TODO make more elegant
        else:
            formfield = field.formfield

            if formfield() is None:
                name = field.field.__class__.__name__
            else:
                name = formfield().__class__.__name__

            if self.formfield_overrides.has_key(name):
                formfield = self.formfield_overrides[name]

        # TODO since None is considered an empty value by the django validators
        # ``required`` has to be set to False to not raise a ValidationError
        # saying the field is required. There may be a need to more explicitly
        # check to see if the value be passed is only None and not any of the
        # other empty values in ``django.core.validators.EMPTY_VALUES``
        ff = field.formfield(formfield=formfield, required=False, **kwargs)

        # special case for ``None`` values since all form fields seem to handle
        # the conversion differently. simply ignore the cleaning if ``None``,
        # this scenario occurs when a list of values are being queried and one
        # of them is to lookup NULL values
        if ins(value):
            new_value = []
            unique_values = set([])
            for x in value:
                if x in unique_values:
                    continue
                unique_values.add(x)
                if x is not None:
                    new_value.append(ff.clean(x))
                # Django assumes an empty string when given a ``NoneType``
                # for char-based form fields, this is to ensure ``NoneType``
                # are passed through unmodified
                else:
                    new_value.append(None)
            return new_value
        return ff.clean(value)
Exemple #3
0
    def format_seq(self, seq, rules, ftype, formatters, error, null):
        n, toks = 0, []

        for fname, nargs in rules:
            args = seq[n:n+nargs]

            # if the formatter expects one argument, but it is None,
            # skip the formatting and default to None.
            if len(args) == 1 and args[0] is None:
                tok = None
            else:
                try:
                    obj = formatters[fname]
                    tok = obj(ftype, *args)
                except Exception:
                    tok = error

            if ins(tok):
                tok = list(tok)
                for i, t in enumerate(iter(tok)):
                    if t is None:
                        tok[i] = null
                toks.extend(tok)
            else:
                if tok is None:
                    tok = null
                toks.append(tok)

            n += nargs

        # all args have not been processed, therefore mixed formatting may have
        # occurred.
        if n != len(seq):
            raise FormatError, 'The rules "%s" is being applied to a ' \
                'sequence of %d items' % (rules, len(seq))

        return tuple(toks)
 def check(self, value):
     if ins(value):
         return True
     return False
 def check(self, value):
     if ins(value) and len(value) == 2:
         return True
     return False