def override_template(controller, template): """Override the template to be used. Use override_template in a controller in order to change the template that will be used to render the response dictionary dynamically. The template string passed in requires that you include the template engine name, even if you're using the default. So you have to pass in a template id string like:: "genshi:myproject.templates.index2" future versions may make the `genshi:` optional if you want to use the default engine. """ try: engines = controller.decoration.engines except: return for content_type, content_engine in engines.items(): tmpl = template.split(':', 1) tmpl.extend(content_engine[2:]) try: override_mapping = request._override_mapping except AttributeError: override_mapping = request._override_mapping = {} override_mapping.setdefault(im_func(controller), {}).update({content_type: tmpl})
def create_cache_key(func, key_dict=None, self=None): """Get a cache namespace and key used by the beaker_cache decorator. Example:: from tg import cache from tg.caching import create_cache_key namespace, key = create_cache_key(MyController.some_method) cache.get_cache(namespace).remove(key) """ kls = None imfunc = im_func(func) if imfunc: kls = im_class(func) func = imfunc cache_key = func.__name__ else: cache_key = func.__name__ if key_dict: cache_key += " " + " ".join("%s=%s" % (k, v) for k, v in key_dict.items()) if not kls and self: kls = getattr(self, '__class__', None) if kls: return '%s.%s' % (kls.__module__, kls.__name__), cache_key else: return func.__module__, cache_key
def use_custom_format(controller, custom_format): """Use use_custom_format in a controller in order to change the active @expose decorator when available.""" deco = Decoration.get_decoration(controller) # Check the custom_format passed is available for use if custom_format not in deco.custom_engines: raise ValueError("'%s' is not a valid custom_format" % custom_format) try: render_custom_format = request._render_custom_format except AttributeError: render_custom_format = request._render_custom_format = {} render_custom_format[im_func(controller)] = custom_format
def test_chameleon_genshi_base(): if ChameleonGenshiRenderer is None: raise SkipTest() def add_chameleon_renderer(app_config): app_config.register_rendering_engine(ChameleonGenshiRenderer) app_config.renderers.append('chameleon_genshi') app = setup_noDB(extra_init=add_chameleon_renderer) # Manually add the exposition again as it was already discarded # due to chameleon_genshi not being in the available renderes. milestones.renderers_ready._reset() from .controllers.root import RootController controller = im_func(RootController.chameleon_genshi_index) expose('chameleon_genshi:index.html')(controller) milestones.renderers_ready.reach() resp = app.get('/chameleon_genshi_index') assert ("<p>TurboGears 2 is rapid web application development toolkit" " designed to make your life easier.</p>") in resp
def test_default_chameleon_genshi_renderer(): if ChameleonGenshiRenderer is None: raise SkipTest() def add_chameleon_renderer(app_config): app_config.register_rendering_engine(ChameleonGenshiRenderer) app_config.renderers.append('chameleon_genshi') app = setup_noDB(add_chameleon_renderer) # Manually add the exposition again as it was already discarded # due to chameleon_genshi not being in the available renderes. milestones.renderers_ready._reset() from .controllers.root import RootController controller = im_func(RootController.chameleon_index_dotted) expose('chameleon_genshi:tests.test_stack.rendering.templates.index')(controller) milestones.renderers_ready.reach() resp = app.get('/chameleon_index_dotted') assert "Welcome" in resp, resp assert "TurboGears" in resp, resp
def test_default_chameleon_genshi_renderer(): if ChameleonGenshiRenderer is None: raise SkipTest() def add_chameleon_renderer(app_config): app_config.register_rendering_engine(ChameleonGenshiRenderer) app_config.renderers.append('chameleon_genshi') app = setup_noDB(add_chameleon_renderer) # Manually add the exposition again as it was already discarded # due to chameleon_genshi not being in the available renderes. milestones.renderers_ready._reset() from .controllers.root import RootController controller = im_func(RootController.chameleon_index_dotted) expose('chameleon_genshi:tests.test_stack.rendering.templates.index')( controller) milestones.renderers_ready.reach() resp = app.get('/chameleon_index_dotted') assert "Welcome" in resp, resp assert "TurboGears" in resp, resp
def test_chameleon_genshi_inheritance(): if ChameleonGenshiRenderer is None: raise SkipTest() def add_chameleon_renderer(app_config): app_config.register_rendering_engine(ChameleonGenshiRenderer) app_config.renderers.append("chameleon_genshi") try: import lxml except ImportError: # match templates need lxml, but since they don're really work anyway # (at least not fully compatible with Genshi), we just skip this test return app = setup_noDB(extra_init=add_chameleon_renderer) milestones.renderers_ready._reset() from .controllers.root import RootController controller = im_func(RootController.chameleon_genshi_inherits) expose("chameleon_genshi:genshi_inherits.html")(controller) milestones.renderers_ready.reach() try: resp = app.get("/chameleon_genshi_inherits") except NameError as e: # known issue with chameleon.genshi 1.0 if "match_templates" not in str(e): raise except AttributeError as e: # known issue with chameleon.genshi 1.3 if "XPathResult" not in str(e): raise else: assert "Inheritance template" in resp assert "Master template" in resp
def test_chameleon_genshi_inheritance(): if ChameleonGenshiRenderer is None: raise SkipTest() def add_chameleon_renderer(app_config): app_config.register_rendering_engine(ChameleonGenshiRenderer) app_config.renderers.append('chameleon_genshi') try: import lxml except ImportError: # match templates need lxml, but since they don're really work anyway # (at least not fully compatible with Genshi), we just skip this test return app = setup_noDB(extra_init=add_chameleon_renderer) milestones.renderers_ready._reset() from .controllers.root import RootController controller = im_func(RootController.chameleon_genshi_inherits) expose('chameleon_genshi:genshi_inherits.html')(controller) milestones.renderers_ready.reach() try: resp = app.get('/chameleon_genshi_inherits') except NameError as e: # known issue with chameleon.genshi 1.0 if 'match_templates' not in str(e): raise except AttributeError as e: # known issue with chameleon.genshi 1.3 if 'XPathResult' not in str(e): raise else: assert "Inheritance template" in resp assert "Master template" in resp