def stringify(self, value): """Convert value to string This method is used to generate a simple JSON representation of the object (without dereferencing objects etc.) """ # SuperModel -> UID if ISuperModel.providedBy(value): return str(value) # DateTime -> ISO8601 format elif isinstance(value, (DateTime)): return value.ISO8601() # Image/Files -> filename elif safe_hasattr(value, "filename"): return value.filename # Dict -> convert_value_to_string elif isinstance(value, dict): return {k: self.stringify(v) for k, v in value.iteritems()} # List -> convert_value_to_string if isinstance(value, (list, tuple, LazyMap)): return map(self.stringify, value) # Callables elif safe_callable(value): return self.stringify(value()) elif isinstance(value, unicode): value = value.encode("utf8") try: return str(value) except (AttributeError, TypeError, ValueError): logger.warn("Could not convert {} to string".format(repr(value))) return None
def is_model(self, obj): """Check if the given object is a SuperModel """ return ISuperModel.providedBy(obj)
def to_list(self, model_or_collection): if ISuperModel.providedBy(model_or_collection): return [model_or_collection] if isinstance(model_or_collection, Sequence): return model_or_collection raise TypeError("Need a model or collection")