Пример #1
0
 def test_default_output_encoding(self):
     info = dict(someval="someval")
     # default encoding is utf-8
     val = view.render(info, template="turbogears.tests.simple")
     assert 'utf-8' in view.cherrypy.response.headers["Content-Type"]
     config.update({'tg.defaultview':'kid', 'kid.encoding':'iso-8859-1'})
     val = view.render(info, template="turbogears.tests.simple")
     assert 'iso-8859-1' in view.cherrypy.response.headers["Content-Type"]
Пример #2
0
def try_render(template_args, folder_paths=None, **kwargs):
    if folder_paths:
        temp = kwargs['template']
        for path in folder_paths:
            try:
                kwargs['template'] = path + temp
                return render(template_args, **kwargs)
            except ImportError:
                raise "module "+path+ " doesn't exist"
            except TemplateNotFound:
                pass
        raise "template not found " + kwargs['template']
    else:
        return render(template_args, **kwargs)
Пример #3
0
    def test_default_set_encoding(self):
        """Test we can set Genshi encoding per rendering and headers are set correctly."""

        info = dict(someval=u"Español")
        template = "genshi:turbogears.tests.simple"
        headers = {}
        # default encoding is utf-8
        val = view.render(info, template, headers=headers)
        assert headers.get('Content-Type') == 'text/html; charset=utf-8'
        # encoding can be changed and will be added to existing content type
        headers['Content-Type'] = 'text/html'
        val = view.render(info, template, headers=headers, encoding='iso-8859-1')
        assert headers.get('Content-Type') == 'text/html; charset=iso-8859-1'
        self.assertRaises(UnicodeDecodeError, val.decode, 'utf-8')
        assert u"Paging all Español" in val.decode('iso-8859-1')
Пример #4
0
 def test_default_output_encoding(self):
     info = dict(someval="someval")
     template = "turbogears.tests.simple"
     headers = {}
     # default encoding is utf-8
     val = view.render(info, template, headers=headers)
     assert headers.get('Content-Type') == 'text/html; charset=utf-8'
     # encoding can be changed and will be added to existing content type
     try:
         config.update({'kid.encoding': 'iso-8859-1'})
         headers['Content-Type'] = 'text/html'
         view.render(info, template, headers=headers)
         assert headers.get('Content-Type') == 'text/html; charset=iso-8859-1'
     finally:
         config.update({'kid.encoding': 'utf-8'})
Пример #5
0
    def test_UnicodeValueAppearingInATemplateIsFine(self):
        """Test that unicode values passed to template are UTF-8 encoded in output."""

        ustr = u"micro-eXtreme Programming ( µ XP): Embedding XP Within Standard Projects"
        info = dict(someval=ustr)
        val = view.render(info, template="genshi:turbogears.tests.simple")
        assert u"Paging all " + ustr in val.decode("utf-8")
Пример #6
0
    def test_new_text_format(self):
        """Verify that our view is able to send new text format to Genshi."""

        # make sure we have asked for new format in the config
        new_syntax = config.get("genshi.new_text_syntax", False)
        config.update({"genshi.new_text_syntax": True})
        # we need to reload the engines for the config changes to take effect
        view.load_engines()

        # load our expected result in memory
        expected_result = 'Dear Florent Aide\nYour items:\n  * Apples\n  * Bananas\n  * Cherries\n\n'

        info = dict(
            name=u"Florent Aide",
            itemlist=[u'Apples', u'Bananas', u'Cherries']
        )
        template = "genshi-text:turbogears.tests.genshi_new_text_format"
        headers = {}
        val = view.render(info, template, headers=headers, format="text")
        config.update({"genshi.new_text_syntax": new_syntax})

        print "Got this:"
        print "*" * 35
        print "%r" % val
        print "*" * 35
        print "Expected that:"
        print "*" * 35
        print "%r" % expected_result
        print "*" * 35
        assert val == expected_result
Пример #7
0
def render_tg_template(template_name, data):
    """
    Helper for Flask handlers to render a Kid template in 
    a TurboGears-compatible way. This reimplements the bare minimum TG magic 
    bits needed to successfully render a template inheriting from master.kid.
    """
    from turbogears.view import render
    # These are the widgets which are always included on every page (as defined
    # in tg.include_widgets) minus Mochikit because we are not using that in any
    # new code.
    from bkr.server.widgets import jquery, beaker_js, beaker_css
    data['tg_css'] = beaker_css.retrieve_css()
    data['tg_js_head'] = jquery.retrieve_javascript(
    ) + beaker_js.retrieve_javascript()
    data['tg_js_bodytop'] = []
    data['tg_js_bodybottom'] = []
    # If the worker has previously handled a CherryPy request then
    # cherrypy.request will be left behind from that, and
    # turbogears.util.request_available will be fooled into thinking we are
    # actually inside CherryPy. The base implementation of
    # turbogears.widgets.Widget#display tries to check tg_template_enginename if
    # it thinks a CherryPy request is available, so we just set it here to
    # avoid AttributeErrors in that code (since we really aren't inside
    # a CherryPy request at this point).
    import cherrypy
    try:
        cherrypy.request.tg_template_enginename = 'kid'
    except AttributeError:
        pass
    return render(data, template_name)
Пример #8
0
def render_tg_template(template_name, data):
    """
    Helper for Flask handlers to render a Kid template in 
    a TurboGears-compatible way. This reimplements the bare minimum TG magic 
    bits needed to successfully render a template inheriting from master.kid.
    """
    from turbogears.view import render
    # These are the widgets which are always included on every page (as defined 
    # in tg.include_widgets) minus Mochikit because we are not using that in any 
    # new code.
    from bkr.server.widgets import jquery, beaker_js, beaker_css
    data['tg_css'] = beaker_css.retrieve_css()
    data['tg_js_head'] = jquery.retrieve_javascript() + beaker_js.retrieve_javascript()
    data['tg_js_bodytop'] = []
    data['tg_js_bodybottom'] = []
    # If the worker has previously handled a CherryPy request then 
    # cherrypy.request will be left behind from that, and 
    # turbogears.util.request_available will be fooled into thinking we are 
    # actually inside CherryPy. The base implementation of 
    # turbogears.widgets.Widget#display tries to check tg_template_enginename if 
    # it thinks a CherryPy request is available, so we just set it here to 
    # avoid AttributeErrors in that code (since we really aren't inside 
    # a CherryPy request at this point).
    import cherrypy
    try:
        cherrypy.request.tg_template_enginename = 'kid'
    except AttributeError:
        pass
    return render(data, template_name)
Пример #9
0
 def test_genshi_default_doctypes(self):
     """Test that Genshi uses the correct doctype with default settings.
     """
     template = "genshi:turbogears.tests.simple"
     info = dict(someval="dumbo")
     headers = {}
     val = view.render(info, template, headers=headers)
     assert val.lstrip().startswith('<!DOCTYPE')
     assert html_strict_url in val
     val = view.render(info, template, headers=headers, format='xhtml')
     assert val.lstrip().startswith('<!DOCTYPE')
     assert xhtml_strict_url in val
     val = view.render(info, template, headers=headers, fragment=True, format='xhtml')
     assert not val.lstrip().startswith('<!DOCTYPE')
     assert xhtml_strict_url not in val
     val = view.render(info, template, headers=headers, format='xml')
     assert not val.lstrip().startswith('<!DOCTYPE')
Пример #10
0
 def test_genshi_set_doctypes(self):
     """Test that we can set the doctpye Genshi uses for each rendering.
     """
     template = "genshi:turbogears.tests.simple"
     info = dict(someval="dumbo")
     headers = {}
     val = view.render(info, template, headers=headers, doctype='html')
     assert val.lstrip().startswith('<!DOCTYPE')
     assert html_strict_url in val
     val = view.render(info, template, headers=headers,
                       doctype='html-strict')
     assert html_strict_url in val
     val = view.render(info, template, headers=headers,
                       doctype='html-transitional')
     assert html_transitional_url in val
     # This mismatch is intended, top check that genshi does not use
     # the default doctype.
     val = view.render(info, template, headers=headers, format='xhtml',
                       doctype='html')
     assert html_strict_url in val
     val = view.render(info, template, headers=headers, format='xhtml',
                       doctype='xhtml-strict')
     assert val.lstrip().startswith('<!DOCTYPE')
     assert xhtml_strict_url in val
     val = view.render(info, template, headers=headers, format='xhtml',
                       fragment=True, doctype='xhtml-strict')
     assert not val.lstrip().startswith('<!DOCTYPE')
     assert xhtml_strict_url not in val
     val = view.render(info, template, headers=headers, format='xhtml',
                       doctype='xhtml-transitional')
     assert xhtml_transitional_url in val
Пример #11
0
 def test_plain_format(self):
     info = dict(someval="dumbos")
     template = "turbogears.tests.simple"
     headers = {}
     plain = view.render(info, template, headers=headers, format='plain')
     assert headers.get('Content-Type') == 'text/plain; charset=utf-8'
     assert plain.strip() == ('This is the groovy test template.'
         ' Paging all dumbos.')
     headers = {}
     text = view.render(info, template, headers=headers, format='text')
     assert headers.get('Content-Type') == 'text/plain; charset=utf-8'
     assert text == plain
     try:
         view.render(info, template, headers=headers, format='dumbo')
     except KeyError:
         pass
     else:
         assert False, "'dumbo' should not be accepted as format"
Пример #12
0
    def test_default_output_encoding(self):
        """Test we can set Genshi default encoding and headers are set correctly."""

        info = dict(someval=u"Español")
        template = "genshi:turbogears.tests.simple"
        headers = {}
        # default encoding is utf-8
        val = view.render(info, template, headers=headers)
        assert headers.get('Content-Type') == 'text/html; charset=utf-8'
        # encoding can be changed and will be added to existing content type
        try:
            genshi_encoding = config.get('genshi.default_encoding', "utf-8")
            config.update({'genshi.default_encoding': 'iso-8859-1'})
            # we need to reload the engines for the config change to take effect
            view.load_engines()
            headers['Content-Type'] = 'text/html'
            val = view.render(info, template, headers=headers)
            assert headers.get('Content-Type') == 'text/html; charset=iso-8859-1'
            self.assertRaises(UnicodeDecodeError, val.decode, 'utf-8')
            assert u"Paging all Español" in val.decode('iso-8859-1')
        finally:
            config.update({'genshi.default_encoding': genshi_encoding})
Пример #13
0
    def test_view_pass_options(self):
        """Test that view.render correctly passes options to template engine."""

        options = dict(strict=True, cache=True)
        view.engines['testplugin'] = TestTemplateEngine(options=options)
        data = dict(fruit='apple', beverage='beer')
        template = 'testplugin:test'
        options = dict(strict=False, renderer='fast')
        output = view.render(data, template, **options)
        info, options = loads(output.decode('utf-8'))
        assert info['fruit'] == 'apple', \
            "Data dict passed to view.render() not included in output."
        assert options['cache'] is True, \
            "Options passed to __init__() not used."
        assert options['renderer'] == 'fast', \
            "Options passed to render() not used."
        assert options['strict'] is not True, \
            "Options passed to render() do not override options passed to __init__()."
Пример #14
0
def _process_output(output, template, format, content_type,
        mapping, fragment=False):
    """Produce final output form from data returned from a controller method.

    See the expose() arguments for more info since they are the same.

    """
    if isinstance(output, dict):
        from turbogears.widgets import js_location

        css = tg_util.setlike()
        js = dict(izip(js_location, iter(tg_util.setlike, None)))
        include_widgets = {}
        include_widgets_lst = config.get("tg.include_widgets", [])

        if config.get("tg.mochikit_all", False):
            include_widgets_lst.insert(0, 'turbogears.mochikit')

        for i in include_widgets_lst:
            widget = tg_util.load_class(i)
            if isclass(widget):
                widget = widget()
            include_widgets["tg_%s" % i.split(".")[-1]] = widget
            for script in widget.retrieve_javascript():
                if hasattr(script, "location"):
                    js[script.location].add(script)
                else:
                    js[js_location.head].add(script)
            css.add_all(widget.retrieve_css())

        for value in output.itervalues():
            if hasattr(value, "retrieve_css"):
                retrieve = getattr(value, "retrieve_css")
                if callable(retrieve):
                    css.add_all(value.retrieve_css())
            if hasattr(value, "retrieve_javascript"):
                retrieve = getattr(value, "retrieve_javascript")
                if callable(retrieve):
                    for script in value.retrieve_javascript():
                        if hasattr(script, "location"):
                            js[script.location].add(script)
                        else:
                            js[js_location.head].add(script)
        output.update(include_widgets)
        output["tg_css"] = css
        for location in iter(js_location):
            output["tg_js_%s" % str(location)] = js[location]

        tg_flash = _get_flash()
        if tg_flash:
            output["tg_flash"] = tg_flash

        headers = {'Content-Type': content_type}
        output = view.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
    else:
        content_type = response.headers.get("Content-Type", 'text/plain')

    if content_type.startswith("text/"):
        if isinstance(output, unicode):
            output = output.encode(tg_util.get_template_encoding_default())

    return output
Пример #15
0
    def test_content_types(self):
        """Test that setting view format sets correct content-type header."""

        view.engines['testplugin'] = TestTemplateEngine()
        template = "testplugin:foo"
        info = dict()
        headers = {}
        view.render(info, template, headers=headers)
        assert headers.get('Content-Type') == 'text/html; charset=utf-8'
        headers = {}
        view.render(info, template, headers=headers, format='html')
        assert headers.get('Content-Type') == 'text/html; charset=utf-8'
        headers = {}
        view.render(info, template, headers=headers, format='html-strict')
        assert headers.get('Content-Type') == 'text/html; charset=utf-8'
        headers = {}
        view.render(info, template, headers=headers, format='xhtml')
        assert headers.get('Content-Type') == 'text/html; charset=utf-8'
        headers = {}
        view.render(info, template, headers=headers, format='xhtml-strict')
        assert headers.get('Content-Type') == 'text/html; charset=utf-8'
        headers = {}
        view.render(info, template, headers=headers, format='xml')
        assert headers.get('Content-Type') == 'text/xml; charset=utf-8'
        headers = {}
        view.render(info, template, headers=headers, format='json')
        assert headers.get('Content-Type') == 'application/json'
        mime_types = config.get('tg.format_mime_types', {})
        config.update({'global':
            {'tg.format_mime_types': {'xhtml': 'application/xhtml+xml'}}})
        headers = {}
        view.render(info, template, headers=headers, format='xhtml')
        assert headers.get('Content-Type') == 'application/xhtml+xml; charset=utf-8'
        headers = {}
        view.render(info, template, headers=headers, format='xhtml-strict')
        assert headers.get('Content-Type') == 'application/xhtml+xml; charset=utf-8'
        config.update({'global': {'tg.format_mime_types': mime_types}})
Пример #16
0
 def test_content_types(self):
     info = dict(someval="someval")
     template = "turbogears.tests.simple"
     headers = {}
     view.render(info, template, headers=headers)
     assert headers.get('Content-Type') == 'text/html; charset=utf-8'
     headers = {}
     view.render(info, template, headers=headers, format='html')
     assert headers.get('Content-Type') == 'text/html; charset=utf-8'
     headers = {}
     view.render(info, template, headers=headers, format='html-strict')
     assert headers.get('Content-Type') == 'text/html; charset=utf-8'
     headers = {}
     view.render(info, template, headers=headers, format='xhtml')
     assert headers.get('Content-Type') == 'text/html; charset=utf-8'
     headers = {}
     view.render(info, template, headers=headers, format='xhtml-strict')
     assert headers.get('Content-Type') == 'text/html; charset=utf-8'
     headers = {}
     view.render(info, template, headers=headers, format='xml')
     assert headers.get('Content-Type') == 'text/xml; charset=utf-8'
     headers = {}
     view.render(info, template, headers=headers, format='json')
     assert headers.get('Content-Type') == 'application/json'
     config.update({'global':
         {'tg.format_mime_types': {'xhtml': 'application/xhtml+xml'}}})
     headers = {}
     view.render(info, template, headers=headers, format='xhtml')
     assert headers.get('Content-Type') == 'application/xhtml+xml; charset=utf-8'
     headers = {}
     view.render(info, template, headers=headers, format='xhtml-strict')
     assert headers.get('Content-Type') == 'application/xhtml+xml; charset=utf-8'
     config.update({'global': {'tg.format_mime_types': {}}})
Пример #17
0
 def test_UnicodeValueAppearingInATemplateIsFine(self):
     ustr = u"micro-eXtreme Programming ( µ XP): Embedding XP Within Standard Projects"
     info = dict(someval=ustr)
     val = view.render(info, template="turbogears.tests.simple")
     self.failUnless(u"Paging all " + ustr in val.decode("utf-8"))
Пример #18
0
def _process_output(output, template, format, content_type, mapping, fragment=False):
    """Produces final output form from the data returned from a
    controller method.

    @param tg_format: format of desired output (html or json)
    @param output: the output returned by the controller
    @param template: HTML template to use
    """
    if isinstance(output, dict):
        from turbogears.widgets import js_location

        css = tg_util.setlike()
        js = dict(izip(js_location, iter(tg_util.setlike, None)))
        include_widgets = {}
        include_widgets_lst = config.get("tg.include_widgets", [])

        if config.get("tg.mochikit_all", False):
            include_widgets_lst.insert(0, 'turbogears.mochikit')
            
        for i in include_widgets_lst:
            widget = tg_util.load_class(i)  
            if isclass(widget):
                widget = widget()
            include_widgets["tg_%s" % i.split(".")[-1]] = widget
            for script in widget.retrieve_javascript():
                if hasattr(script, "location"):
                    js[script.location].add(script)
                else:
                    js[js_location.head].add(script)
            css.add_all(widget.retrieve_css())

        for value in output.itervalues():
            if hasattr(value, "retrieve_css"):
                retrieve = getattr(value, "retrieve_css")
                if callable(retrieve):
                    css.add_all(value.retrieve_css())
            if hasattr(value, "retrieve_javascript"):
                retrieve = getattr(value, "retrieve_javascript")
                if callable(retrieve):
                    for script in value.retrieve_javascript():
                        if hasattr(script, "location"):
                            js[script.location].add(script)
                        else:
                            js[js_location.head].add(script)
        output.update(include_widgets)
        output["tg_css"] = css
        #output.update([("tg_js_%s" % str(l), js[l]) for l in js_location])
        for l in iter(js_location):
            output["tg_js_%s" % str(l)] = js[l]

        tg_flash = _get_flash() 
        if not tg_flash == None:
            output["tg_flash"] = tg_flash 
        output = view.render(output, template=template, format=format,
                    mapping=mapping, content_type=content_type,
                    fragment=fragment)
    else:
        if content_type:
            cherrypy.response.headers["Content-Type"] = content_type

    # fix the Safari XMLHttpRequest encoding problem
    try:
        contentType = cherrypy.response.headers["Content-Type"]
        ua = cherrypy.request.headers["User-Agent"]
    except KeyError:
        return output
    if not contentType.startswith("text/"):
        return output
    ua = view.UserAgent(ua)
    enc = tg_util.get_template_encoding_default()
    if ua.browser == "safari":
        if isinstance(output, str):
            output = output.decode(enc)
        output = unicodechars.sub(
            lambda m: "&#x%x;" % ord(m.group(1)), output).encode("ascii")
    if isinstance(output, unicode):
        output = output.encode(enc)
    return output
Пример #19
0
def _process_output(output, template, format, content_type, fragment=False,
                    **options):
    """Produce final output form from data returned from a controller method.

    See the expose() arguments for more info since they are the same.

    """
    if isinstance(output, dict):
        # import this here to prevent circular import in widgets.forms
        from turbogears.widgets import js_location

        css = tg_util.setlike()
        js = dict(izip(js_location, iter(tg_util.setlike, None)))
        include_widgets = {}
        include_widgets_lst = config.get('tg.include_widgets', [])

        if config.get('tg.mochikit_all', False):
            include_widgets_lst.insert(0, 'turbogears.mochikit')

        for name in include_widgets_lst:
            widget = tg_util.load_class(name)
            if widget is None:
                log.debug("Could not load widget %s", name)
                continue
            if isclass(widget):
                widget = widget()
            if hasattr(widget, 'retrieve_resources') and hasattr(widget, 'inject'):
                # it's a ToscaWidget, we register it for injection
                widget.inject()
            # XXX: widgets with same base name will override each other
            include_widgets['tg_%s' % name.rsplit('.', 1)[-1]] = widget
        output.update(include_widgets)

        # collect JS/CSS resources from widgets in the output dict or
        # tg.include_widgets
        for value in output.itervalues():
            if hasattr(value, 'retrieve_resources'):
                # it's a ToscaWidget, will be injected by the ToscaWidget middleware
                continue
            else:
                try:
                    css_resources = value.retrieve_css()
                except (AttributeError, TypeError):
                    css_resources = []
                try:
                    js_resources = value.retrieve_javascript()
                except (AttributeError, TypeError):
                    js_resources = []
            css.add_all(css_resources)
            for script in js_resources:
                location = getattr(script, 'location', js_location.head)
                js[location].add(script)
        css.sort(key=lambda obj:  getattr(obj, 'order', 0))
        output['tg_css'] = css
        for location in iter(js_location):
            js[location].sort(key=lambda obj:  getattr(obj, 'order', 0))
            output['tg_js_%s' % location] = js[location]

        tg_flash = _get_flash()
        if tg_flash:
            output['tg_flash'] = tg_flash

        headers = {'Content-Type': content_type}
        output = view.render(output, template=template, format=format,
                             headers=headers, fragment=fragment, **options)
        content_type = headers['Content-Type']

    if content_type:
        response.headers['Content-Type'] = content_type
    else:
        content_type = response.headers.get('Content-Type', 'text/plain')

    if content_type.startswith('text/'):
        if isinstance(output, unicode):
            output = output.encode(tg_util.get_template_encoding_default())

    return output
Пример #20
0
def _process_output(output,
                    template,
                    format,
                    content_type,
                    mapping,
                    fragment=False):
    """Produce final output form from data returned from a controller method.

    See the expose() arguments for more info since they are the same.

    """
    if isinstance(output, dict):
        from turbogears.widgets import js_location

        css = tg_util.setlike()
        js = dict(izip(js_location, iter(tg_util.setlike, None)))
        include_widgets = {}
        include_widgets_lst = config.get("tg.include_widgets", [])

        if config.get("tg.mochikit_all", False):
            include_widgets_lst.insert(0, 'turbogears.mochikit')

        for i in include_widgets_lst:
            widget = tg_util.load_class(i)
            if isclass(widget):
                widget = widget()
            include_widgets["tg_%s" % i.split(".")[-1]] = widget
            for script in widget.retrieve_javascript():
                if hasattr(script, "location"):
                    js[script.location].add(script)
                else:
                    js[js_location.head].add(script)
            css.add_all(widget.retrieve_css())

        for value in output.itervalues():
            if hasattr(value, "retrieve_css"):
                retrieve = getattr(value, "retrieve_css")
                if callable(retrieve):
                    css.add_all(value.retrieve_css())
            if hasattr(value, "retrieve_javascript"):
                retrieve = getattr(value, "retrieve_javascript")
                if callable(retrieve):
                    for script in value.retrieve_javascript():
                        if hasattr(script, "location"):
                            js[script.location].add(script)
                        else:
                            js[js_location.head].add(script)
        output.update(include_widgets)
        output["tg_css"] = css
        for location in iter(js_location):
            output["tg_js_%s" % str(location)] = js[location]

        tg_flash = _get_flash()
        if tg_flash:
            output["tg_flash"] = tg_flash

        headers = {'Content-Type': content_type}
        output = view.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
    else:
        content_type = response.headers.get("Content-Type", 'text/plain')

    if content_type.startswith("text/"):
        if isinstance(output, unicode):
            output = output.encode(tg_util.get_template_encoding_default())

    return output
Пример #21
0
 def test_UnicodeValueAppearingInATemplateIsFine(self):
     ustr = u"micro-eXtreme Programming ( µ XP): Embedding XP Within Standard Projects"
     info = dict(someval=ustr)
     val = view.render(info, template="turbogears.tests.simple")
     self.failUnless(u"Paging all " + ustr in val.decode("utf-8"))