def test_mime_type_has_charset(): assert not util.mime_type_has_charset(None) assert not util.mime_type_has_charset('foo/bar') assert util.mime_type_has_charset('text/foobar') assert util.mime_type_has_charset('application/xml') assert util.mime_type_has_charset('application/foo+xml') assert util.mime_type_has_charset('application/javascript') assert not util.mime_type_has_charset('application/foobar') assert not util.mime_type_has_charset('application/json')
def render(info, template=None, format=None, headers=None, mapping=None, fragment=False): """Renders data in the desired format. @param info: the data itself @type info: dict @param format: "html", "xml", "text" or "json" @type format: string @param headers: for response headers, primarily the content type @type headers: dict @param fragment: passed through to tell the template if only a fragment of a page is desired @type fragment: bool @param template: name of the template to use @type template: string """ # What's this stuff for? Just for testing? environ = getattr(cherrypy.request, 'wsgi_environ', {}) if environ.get('paste.testing', False): cherrypy.request.wsgi_environ['paste.testing_variables']['raw'] = info template = format == 'json' and 'json' or info.pop("tg_template", template) engine, template, enginename = _choose_engine(template) if format: if format == 'plain': if enginename == 'genshi': format = 'text' elif format == 'text': if enginename == 'kid': format = 'plain' else: format = enginename == 'json' and 'json' or config.get( "%s.outputformat" % enginename, config.get("%s.default_format" % enginename, 'html')) if isinstance(headers, dict): # Determine the proper content type and charset for the response. # We simply derive the content type from the format here # and use the charset specified in the configuration setting. # This could be improved by also examining the engine and the output. content_type = headers.get('Content-Type') if not content_type: if format: content_format = format if isinstance(content_format, (tuple, list)): content_format = content_format[0] if isinstance(content_format, str): content_format = content_format.split()[0].split( '-', 1)[0].lower() else: content_format = 'html' else: content_format = 'html' content_type = get_mime_type_for_format(content_format) if mime_type_has_charset( content_type) and '; charset=' not in content_type: charset = get_template_encoding_default(enginename) if charset: content_type += '; charset=' + charset headers['Content-Type'] = content_type mapping = mapping or dict() return engine(info=info, format=format, fragment=fragment, template=template, mapping=mapping)
def render(info, template=None, format=None, headers=None, mapping=None, fragment=False): """Renders data in the desired format. @param info: the data itself @type info: dict @param format: "html", "xml", "text" or "json" @type format: string @param headers: for response headers, primarily the content type @type headers: dict @param fragment: passed through to tell the template if only a fragment of a page is desired @type fragment: bool @param template: name of the template to use @type template: string """ # What's this stuff for? Just for testing? environ = getattr(cherrypy.request, 'wsgi_environ', {}) if environ.get('paste.testing', False): cherrypy.request.wsgi_environ['paste.testing_variables']['raw'] = info template = format == 'json' and 'json' or info.pop("tg_template", template) engine, template, enginename = _choose_engine(template) if format: if format == 'plain': if enginename == 'genshi': format = 'text' elif format == 'text': if enginename == 'kid': format = 'plain' else: format = enginename == 'json' and 'json' or config.get( "%s.outputformat" % enginename, config.get("%s.default_format" % enginename, 'html')) if isinstance(headers, dict): # Determine the proper content type and charset for the response. # We simply derive the content type from the format here # and use the charset specified in the configuration setting. # This could be improved by also examining the engine and the output. content_type = headers.get('Content-Type') if not content_type: if format: content_format = format if isinstance(content_format, (tuple, list)): content_format = content_format[0] if isinstance(content_format, str): content_format = content_format.split( )[0].split('-' , 1)[0].lower() else: content_format = 'html' else: content_format = 'html' content_type = get_mime_type_for_format(content_format) if mime_type_has_charset( content_type) and '; charset=' not in content_type: charset = get_template_encoding_default(enginename) if charset: content_type += '; charset=' + charset headers['Content-Type'] = content_type mapping = mapping or dict() return engine(info=info, format=format, fragment=fragment, template=template, mapping=mapping)