Skip to content

Instantly share code, notes, and snippets.

@knzm
Created August 18, 2012 18:49
Show Gist options
  • Select an option

  • Save knzm/3388985 to your computer and use it in GitHub Desktop.

Select an option

Save knzm/3388985 to your computer and use it in GitHub Desktop.
from pyramid.config import Configurator
# from pyramid.view import view_config
from .conditional_renderer import view_config
"""
@view_config(route_name="foo", renderer="app1:foo.mako")
def foo(request):
if "success" in request.GET:
return {"message": "success"}
else:
return {"message": "failure"}
"""
class Cont:
class success: pass
class failure: pass
@view_config(route_name="foo",
renderer={Cont.success: "app1:success.mako",
Cont.failure: "app1:failure.mako"})
def foo(request):
if "success" in request.GET:
return (Cont.success, {"message": "success"})
else:
return (Cont.failure, {"message": "failure"})
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.include('.conditional_renderer')
config.add_route('foo', '/')
config.scan()
return config.make_wsgi_app()
from pyramid.view import view_config as orig_view_config
from pyramid.renderers import RendererHelper
from pyramid.compat import string_types
__all__ = ['view_config']
class view_config(orig_view_config):
def __call__(self, wrapped):
if isinstance(self.renderer, string_types):
return super(view_config, self).__call__(wrapped)
settings = self.__dict__.copy()
def callback(context, name, ob):
config = context.config.with_package(info.module)
settings["renderer"] = ConditionalRendererHelper(
self.renderer, config.package, config.registry)
config.add_view(view=ob, **settings)
info = self.venusian.attach(wrapped, callback, category='pyramid')
if info.scope == 'class':
# if the decorator was attached to a method in a class, or
# otherwise executed at class scope, we need to set an
# 'attr' into the settings if one isn't already in there
if settings.get('attr') is None:
settings['attr'] = wrapped.__name__
return wrapped
class ConditionalRendererHelper(RendererHelper):
def __init__(self, renderers, package=None, registry=None):
super(ConditionalRendererHelper, self).__init__(
"conditional", package=package, registry=registry)
renderers_ = {}
for key, name in renderers.iteritems():
renderer = RendererHelper(
name=name,
package=package,
registry=registry)
renderers_[key] = renderer
self.renderers = renderers_
def conditional_renderer(info):
def _render(value, system_value):
cond_key, value = value
return info.renderers[cond_key].render(value, system_value)
return _render
def includeme(config):
config.add_renderer('conditional', conditional_renderer)
import unittest
from pyramid import testing
class ViewTests(unittest.TestCase):
def setUp(self):
from . import main
app = main({})
from webtest import TestApp
self.testapp = TestApp(app)
def test_root_success(self):
res = self.testapp.get('/', params="success=1", status=200)
res.mustcontain("success")
def test_root_fail(self):
res = self.testapp.get('/', params="failure=1", status=200)
res.mustcontain("failure")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment