def serialize(self, dest, current, attribute, opts): data = None if opts and opts.get('ref'): if not dest.get('relationships'): dest['relationships'] = {} def map_current(item): return self.serialize_ref(item, current, attribute, opts) if _.is_list(current.get(attribute)): data = map(map_current, current[attribute]) else: data = self.serialize_ref(current[attribute], current, attribute, opts) dest['relationships'][self.key_for_attribute(attribute)] = {} if not opts.get('ignore_relationship_data'): dest['relationships'][self.key_for_attribute( attribute)]['data'] = data if opts.get('relationship_links'): dest['relationships'][self.key_for_attribute(attribute)]['links'] = \ self.get_links(current[attribute], opts['relationship_links'], dest) if opts.get('relationship_meta'): dest['relationships'][self.key_for_attribute(attribute)]['meta'] = \ self.get_meta(current['attribute'], opts['relationship_meta']) else: if _.is_list(current[attribute]): if len(current[attribute]) and _.is_dict( current[attribute][0]): def map_current(item): return self.serialize_nested(item, current, attribute, opts) data = map(map_current, current[attribute]) else: data = current[attribute] dest['attributes'][self.key_for_attribute(attribute)] = data elif _.is_dict(current[attribute]): data = self.serialize_nested(current[attribute], current, attribute, opts) dest['attributes'][self.key_for_attribute(attribute)] = data else: dest['attributes'][self.key_for_attribute( attribute)] = current[attribute]
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
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']])
def serialize(self, records): if self.opts.get('top_level_links'): self.payload['links'] = \ self._get_links(self.opts['top_level_links'], records) if self.opts.get('meta'): self.payload['meta'] = self.opts['meta'] if _.is_list(records): return self._collection(records) else: return self._resource(records)
def to_dict(cls): """Creates dict out of list and regex attributes, so it can be passed to angular for frontend validation Returns: dict: """ result = {} for attr_name in _.reject(set(dir(cls)), lambda x: x.startswith('_')): attr = getattr(cls, attr_name) if _.is_list(attr) or _.is_string(attr): result[attr_name] = attr return result
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)
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
def _is_complex_type(self, obj): return _.is_list(obj) or _.is_dict(obj)