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")


On Monday, March 5, 2012 9:17:36 AM UTC-5, Sontek wrote:
>
>
>
> On Mon, Mar 5, 2012 at 7:44 AM, Thomas G. Willis <[email protected]>wrote:
>
>> I've been reading up on extending pyramid applications and trying this 
>> out. but it's not working as I expect.
>>
>> http://docs.pylonsproject.org/projects/pyramid/en/1.2-branch/narr/extending.html
>>
>> 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")
>>
>
> You need to do an add_route with your add_view for login/logout for this 
> to work.
>
> For a breakdown of each you can check out:
>
> http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/urldispatch.html
>  
>
> http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.add_route
>
> http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.add_view
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/pylons-discuss/-/i4CaE78jSbkJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en.

Reply via email to