def handler(self, oldhandler, *args, **kwargs): output = oldhandler(*args, **kwargs) # If not a dict then output has been rendered and there's nothing left # for us to do, so bail out if not isinstance(output, dict): return output get = request.config.get exposes = get('tools.expose.exposes', dict(default={})) accept = request.headers.get('Accept', "").lower() accept = tg_util.simplify_http_accept_header(accept) tg_format = request.tg_format # Select the correct expose to use. First we trust tg_format, then # accept headers, then fallback to default for key in [tg_format, accept, 'default']: if exposes.has_key(key): expose = exposes[key] break # Unpack parameters that were supplied to @expose format = expose.get('format', get('tools.expose.format', None)) template = expose.get('template', get('tools.expose.template', None)) allow_json = expose.get('allow_json', get('tools.expose.allow_json', False)) mapping = expose.get('mapping') fragment = expose.get('fragment') if format == "json" or (format is None and template is None): template = "json" if allow_json and (tg_format == "json" or accept in ("application/json", "text/javascript")): template = "json" if not template: template = format content_type = expose.get('content_type', config.get("tg.content_type", None)) if template and template.startswith("."): template = func.__module__[:func.__module__.rfind('.')]+template output["tg_css"] = tg_util.setlike() headers = {'Content-Type': content_type} output = render(output, template=template, format=format, mapping=mapping, headers=headers, fragment=fragment) content_type = headers['Content-Type'] if content_type: response.headers["Content-Type"] = content_type return output cherrypy.request.handler = handler
def handler(self, oldhandler, *args, **kwargs): content_type = request.headers.get('Content-Type', "") get = request.config.get allow_json = get('tools.elements.allow_json', False) element = get('tools.elements.element', None) if element: elem = element() if cherrypy.request.body: body = cherrypy.request.body.read() else: body = "" if "application/json" in content_type and allow_json: json = simplejson.loads(body) elem.from_dict(json) elif body: elem.from_string(body) # Put back in request params as tag or root element cherrypy.request.params[elem._tag] = elem output = oldhandler(*args, **kwargs) # If output is not a dict then it's nothing more for us to do if not isinstance(output, dict): return output # Get the tool decorator parameters allow_json = get('tools.elements.allow_json', False) as_format = get("tools.elements.as_format") accept_format = get("tools.elements.accept_format") content_type = get("tools.elements.content_type") accept = request.headers.get('Accept', "").lower() accept = tg_util.simplify_http_accept_header(accept) format = None tg_format = request.tg_format if allow_json and (tg_format=="json" or accept in ("application/json", "text/javascript")): format = "json" elif as_format in ("default", tg_format) or accept_format in ("*/*", accept): format = "xml" else: # No format specified return output # Find Element object in output that we can process elem = None for key, value in output.items(): if isinstance(value, elements.Element): elem = value break if not elem: return output # JSON or XML/Atom format if format == "json": output = simplejson.dumps(elem.to_dict()) cherrypy.response.headers["Content-Type"] = "application/json" else: output = elem.to_string() cherrypy.response.headers["Content-Type"] = content_type return output
def handler(self, oldhandler, *args, **kwargs): output = oldhandler(*args, **kwargs) # If not a dict then output has been rendered and there's nothing left # for us to do, so bail out if not isinstance(output, dict): return output get = request.config.get exposes = get('tools.expose.exposes', dict(default={})) accept = request.headers.get('Accept', "").lower() accept = tg_util.simplify_http_accept_header(accept) tg_format = request.tg_format # Select the correct expose to use. First we trust tg_format, then # accept headers, then fallback to default for key in [tg_format, accept, 'default']: if exposes.has_key(key): expose = exposes[key] break # Unpack parameters that were supplied to @expose format = expose.get('format', get('tools.expose.format', None)) template = expose.get('template', get('tools.expose.template', None)) allow_json = expose.get('allow_json', get('tools.expose.allow_json', False)) mapping = expose.get('mapping') fragment = expose.get('fragment') if format == "json" or (format is None and template is None): template = "json" if allow_json and (tg_format == "json" or accept in ("application/json", "text/javascript")): template = "json" if not template: template = format content_type = expose.get('content_type', config.get("tg.content_type", None)) if template and template.startswith("."): template = func.__module__[:func.__module__.rfind('.')] + template output["tg_css"] = tg_util.setlike() headers = {'Content-Type': content_type} output = render(output, template=template, format=format, mapping=mapping, headers=headers, fragment=fragment) content_type = headers['Content-Type'] if content_type: response.headers["Content-Type"] = content_type return output cherrypy.request.handler = handler
def handler(self, oldhandler, *args, **kwargs): content_type = request.headers.get('Content-Type', "") get = request.config.get allow_json = get('tools.elements.allow_json', False) element = get('tools.elements.element', None) if element: elem = element() if cherrypy.request.body: body = cherrypy.request.body.read() else: body = "" if "application/json" in content_type and allow_json: json = simplejson.loads(body) elem.from_dict(json) elif body: elem.from_string(body) # Put back in request params as tag or root element cherrypy.request.params[elem._tag] = elem output = oldhandler(*args, **kwargs) # If output is not a dict then it's nothing more for us to do if not isinstance(output, dict): return output # Get the tool decorator parameters allow_json = get('tools.elements.allow_json', False) as_format = get("tools.elements.as_format") accept_format = get("tools.elements.accept_format") content_type = get("tools.elements.content_type") accept = request.headers.get('Accept', "").lower() accept = tg_util.simplify_http_accept_header(accept) format = None tg_format = request.tg_format if allow_json and (tg_format == "json" or accept in ("application/json", "text/javascript")): format = "json" elif as_format in ("default", tg_format) or accept_format in ("*/*", accept): format = "xml" else: # No format specified return output # Find Element object in output that we can process elem = None for key, value in output.items(): if isinstance(value, elements.Element): elem = value break if not elem: return output # JSON or XML/Atom format if format == "json": output = simplejson.dumps(elem.to_dict()) cherrypy.response.headers["Content-Type"] = "application/json" else: output = elem.to_string() cherrypy.response.headers["Content-Type"] = content_type return output