Esempio n. 1
0
    def testInjectResourcesUnicode(self):
        """
        strictly for coverage, resource injection is supposed to set
        the unicode body if the result of the resource injection is
        unicode, so trying to figure out how to fake that
        """
        fake_app = Response(unicode_body=six.u("\xea"),
                            charset="utf8",
                            content_type="text/html")

        from tw2.core.middleware import TwMiddleware
        from tw2.core import resources

        def mock_inject(*a, **kw):
            return fake_app.unicode_body

        real = resources.inject_resources
        resources.inject_resources = mock_inject
        try:
            mw = TwMiddleware(fake_app)
            self.assert_(mw.config.inject_resources == True)
            res = Request.blank("/").get_response(mw)
            self.assert_(res.status_int == 200)
            self.assert_(res.unicode_body == fake_app.unicode_body,
                         res.unicode_body)
        finally:
            resources.inject_resources = real
Esempio n. 2
0
    def testTGStyleController(self):
        """ Test turbogears style dispatch """
        from tw2.core.middleware import ControllersApp, TwMiddleware
        from tw2.core.compat import TGStyleController
        controller_response = Response("CONTROLLER")

        class WidgetMock(TGStyleController):
            id = 'fake'

            class Controller(object):
                def foobar(self, request):
                    return controller_response

        mock = WidgetMock()
        mw = TwMiddleware(None, controller_prefix="goo")
        testapi.request(1, mw)
        ca = ControllersApp()

        ca.register(mock)
        res = ca(
            Request.blank("/%s/%s/foobar" %
                          (mw.config.controller_prefix, mock.id)))
        self.assert_(res.status_int == 200, res.status_int)
        self.assert_(res.body == controller_response.body, res.body)

        res = ca(Request.blank("/%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
        res = ca(Request.blank("%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
Esempio n. 3
0
    def testControllerAppWithId(self):
        """
        controllerapp should dispatch to an object having id, and a
        request method taking a webob request based on path_info of
        request.
        """
        from tw2.core.middleware import ControllersApp, TwMiddleware
        controller_response = Response("CONTROLLER")

        class WidgetMock(object):
            def __init__(self):
                self.id = "fake"

            def request(self, request):
                return controller_response

        mock = WidgetMock()
        mw = TwMiddleware(None, controller_prefix="goo")
        testapi.request(1, mw)
        ca = ControllersApp()

        ca.register(mock)
        res = ca(
            Request.blank("/%s/%s" % (mw.config.controller_prefix, mock.id)))
        self.assert_(res.status_int == 200, res.status_int)
        self.assert_(res.body == controller_response.body, res.body)

        res = ca(Request.blank("/%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
        res = ca(Request.blank("%s/404" % mw.config.controller_prefix))
        self.assert_(res.status_int == 404, res.status_int)
Esempio n. 4
0
    def add_tosca2_middleware(self, app):
        """Configure the ToscaWidgets2 middleware.

        If you would like to override the way the TW2 middleware works,
        you might do change your app_cfg.py to add something like::

            from tg.configuration import AppConfig
            from tw2.core.middleware import TwMiddleware

            class MyAppConfig(AppConfig):

                def add_tosca2_middleware(self, app):

                    app = TwMiddleware(app,
                        default_engine=self.default_renderer,
                        translator=ugettext,
                        auto_reload_templates = False
                        )

                    return app
            base_config = MyAppConfig()



        The above example would always set the template auto reloading off. (This is normally an
        option that is set within your application's ini file.)
        """
        from tw2.core.middleware import Config, TwMiddleware
        default_tw2_config = dict( default_engine=self.default_renderer,
                                   translator=ugettext,
                                   auto_reload_templates=asbool(self.get('templating.mako.reloadfromdisk', 'false'))
                                   )
        default_tw2_config.update(self.custom_tw2_config)
        app = TwMiddleware(app, **default_tw2_config)
        return app
Esempio n. 5
0
 def testWithoutApp(self):
     """
     should return a 404
     """
     from tw2.core.middleware import TwMiddleware
     mw = TwMiddleware(None)
     res = Request.blank("/").get_response(mw)
     self.assert_(res.status_int == 404)
Esempio n. 6
0
    def testGetLinkID(self):
        from tw2.core.middleware import TwMiddleware
        mw = TwMiddleware(None)
        testapi.request(1, mw)

        class CWidget(wd.Widget):
            id = "foo"

            @classmethod
            def request(cls, req):
                pass

        self.assert_(CWidget.get_link())
Esempio n. 7
0
    def testServeController(self):
        """
        should dispatch to fake controllers by default
        """
        fake_controller_app = Response("FAKE")

        def fake_controllers(request):
            return fake_controller_app

        from tw2.core.middleware import TwMiddleware
        mw = TwMiddleware(None, controllers=fake_controllers)
        self.assert_(mw.config.serve_controllers)
        self.assert_(mw.config.controller_prefix)
        res = Request.blank(\
            "%s/fake" % mw.config.controller_prefix).get_response(mw)
        self.assert_(res.status_int == 200)
        self.assert_(fake_controller_app.body == res.body)
Esempio n. 8
0
    def add_tosca2_middleware(self, app):
        """Configure the ToscaWidgets2 middleware.

        If you would like to override the way the TW2 middleware works,
        you might do change your app_cfg.py to add something like::

            from tg.configuration import AppConfig
            from tw2.core.middleware import TwMiddleware

            class MyAppConfig(AppConfig):

                def add_tosca2_middleware(self, app):

                    app = TwMiddleware(app,
                        default_engine=self.default_renderer,
                        translator=ugettext,
                        auto_reload_templates = False
                        )

                    return app
            base_config = MyAppConfig()



        The above example would always set the template auto reloading off. (This is normally an
        option that is set within your application's ini file.)
        """
        from tw2.core.middleware import Config, TwMiddleware

        tw2_engines = [self.default_renderer] + Config.preferred_rendering_engines
        default_tw2_config = dict( default_engine=self.default_renderer,
                                   preferred_rendering_engines=tw2_engines,
                                   translator=ugettext,
                                   auto_reload_templates=self.auto_reload_templates,
                                   controller_prefix='/tw2/controllers/',
                                   res_prefix='/tw2/resources/',
                                   debug=config['debug'],
                                   rendering_extension_lookup={
                                        'mako': ['mak', 'mako'],
                                        'genshi': ['genshi', 'html'],
                                        'jinja':['jinja', 'jinja2'],
                                        'kajiki':['kajiki', 'xml']
                                   })
        default_tw2_config.update(self.custom_tw2_config)
        app = TwMiddleware(app, **default_tw2_config)
        return app
    def _add_middleware(self, conf, app):
        """Configure the ToscaWidgets2 middleware"""
        if not conf['tw2.enabled']:
            return app

        from tw2.core.middleware import Config, TwMiddleware
        from tg.i18n import ugettext, get_lang

        available_renderers = set(conf.get('renderers', []))
        shared_engines = list(available_renderers
                              & set(Config.preferred_rendering_engines))
        if not shared_engines:
            raise TGConfigError(
                'None of the configured rendering engines %s is supported '
                'by ToscaWidgets2, unable to configure ToscaWidgets.' %
                available_renderers)

        default_renderer = conf.get('default_renderer', None)
        if default_renderer in shared_engines:
            tw2_engines = [default_renderer] + shared_engines
            tw2_default_engine = default_renderer
        else:
            # If preferred rendering engine is not available in TW2, fallback to another one
            tw2_engines = shared_engines
            tw2_default_engine = shared_engines[0]

        default_tw2_config = dict(default_engine=tw2_default_engine,
                                  preferred_rendering_engines=tw2_engines,
                                  translator=ugettext,
                                  get_lang=lambda: get_lang(all=False),
                                  auto_reload_templates=conf.get(
                                      'auto_reload_templates', False),
                                  controller_prefix='/tw2/controllers/',
                                  res_prefix='/tw2/resources/',
                                  debug=conf['debug'],
                                  rendering_extension_lookup={
                                      'mako': ['mak', 'mako'],
                                      'genshi': ['genshi', 'html'],
                                      'jinja': ['jinja', 'jinja2'],
                                      'kajiki': ['kajiki', 'xhtml', 'xml']
                                  })

        default_tw2_config.update(conf.get('custom_tw2_config', {}))
        app = TwMiddleware(app, **default_tw2_config)
        return app
Esempio n. 10
0
    def testControllerAppWithoutId(self):
        """
        controllerapp should dispatch to an object having id, and a
        request method taking a webob request based on path_info of
        request.
        """
        import tw2.core
        from tw2.core.middleware import register_controller, TwMiddleware
        controller_response = Response("CONTROLLER")

        class WidgetMock(tw2.core.Widget):
            def request(self, request):
                return controller_response

        mw = TwMiddleware(None)
        testapi.request(1, mw)
        register_controller(WidgetMock, 'foobar')
        self.assert_(WidgetMock.controller_path() == 'foobar')
Esempio n. 11
0
    def add_tosca2_middleware(self, app):
        if self.tw2_initialized:
            return app

        from tg import config
        from tw2.core.middleware import Config, TwMiddleware
        default_tw2_config = dict( default_engine=self.default_renderer,
                                   translator=ugettext,
                                   auto_reload_templates=asbool(self.get('templating.mako.reloadfromdisk', 'false'))
                                   )
        res_prefix = config.get('fedoratagger.resource_path_prefix')
        if res_prefix:
            default_tw2_config['res_prefix'] = res_prefix
        if getattr(self, 'custom_tw2_config', None):
            default_tw2_config.update(self.custom_tw2_config)
        app = TwMiddleware(app, **default_tw2_config)
        self.tw2_initialized = True
        return app
Esempio n. 12
0

class RootController(TGController):
    @expose('new.xhtml')
    def new(self, **kw):
        tmpl_context.widget = new_city_form
        return dict(value=kw)

    @expose('new.xhtml')
    def old(self, **kw):
        tmpl_context.widget = new_movie_form
        return dict(value=kw)

    @expose()
    def index(self):
        return 'Hello World'

    @expose('templates.hello.xhtml')
    def hello(self, person=None):
        return dict(person=person)


config = AppConfig(minimal=True, root_controller=RootController())
config.renderers = ['kajiki']
application = config.make_wsgi_app()
from tw2.core.middleware import TwMiddleware
application = TwMiddleware(application)

from wsgiref.simple_server import make_server
httpd = make_server("", 8080, application)
httpd.serve_forever()