def generate_link_object_for(self, obj, inclusions, include): """Generate a link object for this object. If there are property paths to be included specified in the ``inclusions`` parameter, those properties will be added to the object representation. If the ``include`` parameter is ``True`` the entire object will be represented in the result. """ if include: return publish(obj, inclusions) result = {'id': obj.id, 'type': type(obj).__name__, 'href': url_for(obj)} for path in inclusions: if type(path) is not str and type(path) is not unicode: attr_name, remaining_path = path[0], path[1:] else: attr_name, remaining_path = path, () result[attr_name] = self.publish_attr(obj, attr_name, remaining_path) return result
def generate_link_object_for( self, obj, inclusions, include, inclusion_filter): """Generate a link object for this object. If there are property paths to be included specified in the ``inclusions`` parameter, those properties will be added to the object representation. If the ``include`` parameter is ``True`` the entire object will be represented in the result. """ if include and inclusion_filter(obj): return publish(obj, inclusions, inclusion_filter) result = {'id': obj.id, 'type': type(obj).__name__, 'href': url_for(obj)} for path in inclusions: if type(path) is not str and type(path) is not unicode: attr_name, remaining_path = path[0], path[1:] else: attr_name, remaining_path = path, () result[attr_name] = self.publish_attr(obj, attr_name, remaining_path) return result
def publish(obj, inclusions=()): """Translate ``obj`` into a valid JSON value. Objects with properties are translated into a ``dict`` object representing a JSON object while simple values are returned unchanged or specially formatted if needed. """ publisher = get_json_builder(obj) if publisher and hasattr(publisher, '_publish_attrs') \ and publisher._publish_attrs: ret = {} self_url = url_for(obj) if self_url: ret['selfLink'] = self_url view_url = view_url_for(obj) if view_url: ret['viewLink'] = view_url ret.update(publisher.publish_contribution(obj, inclusions)) return ret # Otherwise, just return the value itself by default return obj