def to_json_data(model_instance, property_list): """ Converts a models into dicts for use with JSONResponse. You can either pass a single model instance and get a single dict or a list of models and get a list of dicts. For security reasons only the properties in the property_list will get added. If the value of the property has a json_data function its result will be added, instead. """ if hasattr(model_instance, '__iter__'): return [to_json_data(item, property_list) for item in model_instance] json_data = {} for property in property_list: property_instance = None try: property_instance = getattr(model_instance.__class__, property.split('.', 1)[0]) except: pass key_access = property[len(property.split('.', 1)[0]):] if isinstance(property_instance, db.ReferenceProperty) and \ key_access in ('.key', '.key.name'): key = property_instance.get_value_for_datastore(model_instance) if key_access == '.key': json_data[property] = str(key) else: json_data[property] = key.name() continue value = getattr_by_path(model_instance, property, None) value = getattr_by_path(value, 'json_data', value) json_data[property] = value return json_data
def to_json_data(model_instance, property_list): """ Converts a models into dicts for use with JSONResponse. You can either pass a single model instance and get a single dict or a list of models and get a list of dicts. For security reasons only the properties in the property_list will get added. If the value of the property has a json_data function its result will be added, instead. """ if hasattr(model_instance, '__iter__'): return [to_json_data(item) for item in model_instance] json_data = {} for property in property_list: value = getattr_by_path(model_instance, property, None) value = getattr_by_path(value, 'json_data', value) json_data[property] = value return json_data
def __init__(self, *args, **kwargs): if my_name in kwargs: setattr(self, my_name, kwargs[my_name]) kwargs[property_name] = getattr(self, property_name) for destination, source in myself.integrate.items(): integrate_value = None if kwargs[my_name]: integrate_value = getattr_by_path( kwargs[my_name], source) kwargs[destination] = integrate_value old_init(self, *args, **kwargs)
def __set__(self, instance, value): super(ReferenceProperty, self).__set__(instance, value) for destination, source in self.integrate.items(): integrate_value = None if value: try: property = getattr(value.__class__, source) except: property = None if property and isinstance(property, db.ReferenceProperty): integrate_value = property.get_value_for_datastore(value) else: integrate_value = getattr_by_path(value, source) setattr(instance, destination, integrate_value)
def __set__(self, instance, value): if value and not isinstance(value, db.Model): raise ValueError('You must supply a Model instance.') if not value: key = None elif self.use_key_name: key = value.key().name() else: key = str(value.key()) setattr(instance, '_ref_cache_for_' + self.target_name, value) setattr(instance, self.target_name, key) for destination, source in self.integrate.items(): integrate_value = None if value: integrate_value = getattr_by_path(value, source) setattr(instance, destination, integrate_value)
def __init__(self, *args, **kwargs): if my_name in kwargs: setattr(self, my_name, kwargs[my_name]) kwargs[property_name] = getattr(self, property_name) for destination, source in myself.integrate.items(): integrate_value = None if kwargs[my_name]: try: property = getattr(self.__class__, source) except: property = None if property and isinstance(property, db.ReferenceProperty): integrate_value = property.get_value_for_datastore(self) else: integrate_value = getattr_by_path( kwargs[my_name], source) kwargs[destination] = integrate_value old_init(self, *args, **kwargs)
def get_value_for_datastore(self, model_instance): if self.filters and not self.should_index(DictEmu(model_instance)) \ or self.relation_index: return [] language = self.language if callable(language): language = language(model_instance, property=self) index = [] for property in self.properties: values = getattr_by_path(model_instance, property, None) if not values: values = () elif not isinstance(values, (list, tuple)): values = (values,) for value in values: index.extend(self.splitter(value, indexing=True, language=language)) if self.indexer: index = self.indexer(index, indexing=True, language=language) # Sort index to make debugging easier setattr(model_instance, self.name, sorted(set(index))) return index
def __set__(self, instance, value): if value and not isinstance(value, db.Model): raise ValueError('You must supply a Model instance.') if not value: key = None elif self.use_key_name: key = value.key().name() else: key = str(value.key()) setattr(instance, '_ref_cache_for_' + self.target_name, value) setattr(instance, self.target_name, key) for destination, source in self.integrate.items(): integrate_value = None if value: try: property = getattr(value.__class__, source) except: property = None if property and isinstance(property, db.ReferenceProperty): integrate_value = property.get_value_for_datastore(value) else: integrate_value = getattr_by_path(value, source) setattr(instance, destination, integrate_value)