Esempio n. 1
0
    def key_for_attribute(self, attribute):
        def transform_callback(acc, value, key):
            if self._is_complex_type(value):
                acc[self.key_for_attribute(key)] = self.key_for_attribute(
                    value)
            else:
                acc[self.key_for_attribute(key)] = value
            return acc, value, key

        def map_function(item):
            if self._is_complex_type(item):
                return self.key_for_attribute(item)
            else:
                return item

        if _.is_dict(attribute):
            return _.transform(attribute, transform_callback)

        elif _.is_list(attribute):
            map(map_function, attribute)

        else:
            if _.is_function(self.opts.get('key_for_attribute')):
                return self.opts['key_for_attribute'](attribute)
            else:
                caserized = inflector.caserize(attribute, self.opts)
                return caserized
Esempio n. 2
0
 def get_ref(self, current, item, opts):
     if _.is_function(opts.get('ref')):
         return opts['ref'](current, item)
     elif opts.get('ref') is True:
         if _.is_list(item):
             return map(lambda val: str(val), item)
         elif item:
             return str(item)
     elif isinstance(item, dict) and item.get(opts.get('ref')):
         return str(item[opts['ref']])
Esempio n. 3
0
    def create(cls, name, return_type=None):
        """Creates validation function from given attribute name

        Args:
            name (string): Name of attribute
            required (bool, optional) If false, empty string will be always accepted as valid

        Returns:
            function: validation function
            :param return_type:
        """
        return_type = return_type or str
        if not name:  # no name given -> just return a do nothing function
            def empty_validator_function(value=None, prop=None):
                return return_type(value)

            return empty_validator_function

        ### Date Time validator that converts string datetimes/timestamps to proper datetime objects ###
        if name == SageValidator.DATE_TIME:
            # parse date time
            def date_time_parser(value=None, prop=None):
                temp_datetime = None
                try:  # timestamp approach first
                    temp_datetime = datetime.datetime.fromtimestamp(int(value))
                except ValueError:
                    temp_datetime = None
                if temp_datetime:
                    return temp_datetime

                try:  # iso format next
                    temp_datetime = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
                except ValueError:
                    temp_datetime = None

                if temp_datetime:
                    return temp_datetime
                errors.create(400, message='Invalid date time format')

            return date_time_parser

        attr = getattr(cls, name)
        if _.is_list(attr):
            return util.create_validator(lengths=attr, return_type=return_type)
        elif _.is_function(attr):
            return getattr(cls(), name)
        elif 'regex' in attr and 'lengths' in attr:  # contains both regex and lengths
            return util.create_validator(regex=attr.get('regex'), lengths=attr.get('lengths'),
                                         return_type=return_type)
        else:  # just a regex
            return util.create_validator(regex=attr, return_type=return_type)
Esempio n. 4
0
    def get_type(self, string, attr_val):
        type_ = None
        attr_val = attr_val or {}
        if _.is_function(self.opts.get('type_for_attribute')):
            type_ = self.opts['type_for_attribute'](string, attr_val)

        if (self.opts.get('pluralize_type') is None
                or self.opts.get('pluralize_type')) and type_ is None:
            type_ = inflector.pluralize(string)

        if type_ is None:
            type_ = string

        return type_
Esempio n. 5
0
    def create(cls, name, required=True):
        """Creates validation function from given attribute name

        Args:
            name (string): Name of attribute
            required (bool, optional) If false, empty string will be always accepted as valid

        Returns:
            function: validation function
        """
        attr = getattr(cls, name)
        if _.is_list(attr):
            return util.create_validator(lengths=attr, required=required)
        elif _.is_string(attr):
            return util.create_validator(regex=attr, required=required)
        elif _.is_function(attr):
            return attr
Esempio n. 6
0
 def map_function(item):
     if _.is_function(item):
         return item(self.record, current)
     else:
         return item
Esempio n. 7
0
 def value_mapper(value):
     if _.is_function(value):
         return value(records)
     else:
         return value