pyramid config.include(...) not working as I expect

37 views
Skip to first unread message

Thomas G. Willis

unread,
Mar 5, 2012, 8:44:15 AM3/5/12
to pylons-...@googlegroups.com
I've been reading up on extending pyramid applications and trying this out. but it's not working as I expect.

Anyone see anything immediately wrong with this?
 

import unittest
from webtest import TestApp
from pyramid.configuration import Configurator
from pyramid.response import Response


def default_view(request):
    return Response("")


def config_user_app(config, with_route=False):
    if with_route:
        route_name = "default"
    else:
        route_name = None

    if route_name:
        config.add_route(route_name, "/")

    config.add_view(default_view, name="login", route_name=route_name)
    config.add_view(default_view, name="logout", route_name=route_name)


class TestApplicationComposition(unittest.TestCase):
    def testUserApp(self):
        """works"""
        cfg = Configurator()
        cfg.include(config_user_app)
        app = TestApp(cfg.make_wsgi_app())
        app.get("/login")
        app.get("/logout")

    def testIncluded(self):
        """
        fails with 404
        """
        route_prefix = "user"
        cfg = Configurator()
        cfg.include(config_user_app, route_prefix=route_prefix)
        app = TestApp(cfg.make_wsgi_app())
        app.get("/user/login")
        app.get("/user/logout")

John Anderson

unread,
Mar 5, 2012, 9:17:36 AM3/5/12
to pylons-...@googlegroups.com

Thomas G. Willis

unread,
Mar 5, 2012, 9:49:42 AM3/5/12
to pylons-...@googlegroups.com
Thanks Sontek,

After learning more URL dispatch.  Basically if I want the view name to be applicable I need to combine URL dispatch with traversal, or have an add_route for every add_view. So I got this down to an example I am happy with.

Thanks again for the help.

import unittest
from webtest import TestApp
from pyramid.configuration import Configurator
from pyramid.response import Response


def default_view(context, request):
    return Response("")


def config_user_app(config):
    class RootFactory(object):
        __parent__ = None
        __name__ = None

        def __init__(self, request):
            self._request = request

        def __getitem__(self, key):
            raise KeyError(key)

    config.add_route(name="root", path="/*traverse", factory=RootFactory)
    config.add_view(default_view,
                    name="login",
                    context=RootFactory,
                    route_name="root")
    config.add_view(default_view,
                    name="logout",
                    context=RootFactory,
                    route_name="root")


class TestApplicationComposition(unittest.TestCase):
    def testUserApp(self):
        cfg = Configurator()
        cfg.include(config_user_app)
        app = TestApp(cfg.make_wsgi_app())
        app.get("/login")
        app.get("/logout")

    def testIncluded(self):
        route_prefix = "/users"
        cfg = Configurator()
        cfg.include(config_user_app, route_prefix=route_prefix)
        app = TestApp(cfg.make_wsgi_app())
        app.get("/users/login")
        app.get("/users/logout")
Reply all
Reply to author
Forward
0 new messages