# __init__.py
> from pyramid.config import Configurator
> *from pyramid.response import Response*
>
> *class Resource(dict):*
> * pass*
> *
> *
> *def get_root(request):*
> * return
> Resource({'a':Resource({'b':Resource({'c':Resource({'c':Resource()})})})})
> *
> *
> *
> *def hello_world_of_resources(context,request):*
> * output="Here's a resource and its children: %s" % content*
> * return Response(output)*
>
> def main(global_config, **settings):
> """ This function returns a Pyramid WSGI application.
> """
> config = Configurator(settings=settings,root_factory=get_root)
> config.add_static_view('static', 'static', cache_max_age=3600)
> * config.add_view(hello_world_of_resources,context=Resource)*
> config.scan()
> return config.make_wsgi_app()
>
>
> #views.py
> from pyramid.view import view_config
> *from pyramid.response import Response*
>
> *@view_config()*
> *def my_login(request):*
> * return Response('login')*
> *
> *
> *@view_config()*
> *def my_logout(request):*
> * return Response('logout')*
>
> I have no clue how to link the resource with the views. Did I miss
> something import in the docs or are there any very basic/bare minimum
> traversal only tutorial?
> Thanks in advance.
>
> Traversal and view lookup is a two step process. And both steps are
completely different.
Traversal starts with the root (get_root() function) and calls
__getitem__(url_part) for each resource returned.
It's part of the dict class but if you move it to the Resource it could
look like this:
*
*
class Resource(dict):
def __getitem__(self, name):
resource = self.get(name)
if resource:
return resource
raise KeyError, name
Resources are traversed until pyramid gets a KeyError or all url parts have
been found.
So the URL "a/b/c/c" exists in your example and traversal returns the
fourth Resource in the line.
But the URL "a/b/c/d" exists in your example and traversal returns the
third Resource in the line.
Both ways you end up with the "context".
http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/resources.html#location-aware
Now the second step finding the view for the context. By default views
don't know and don't
care about traversal. It's somehow a break of concept, views are stored as
flat list and not in a
tree structure. However the key is add_view() or @view_config(); there are
many options:
http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/viewconfig.html
e.g.
@view_config(name="login",context=Resource)
def my_login(request):
return Response('login')
==> /a/login or /a/b/login
@view_config(name="logout",context=Resource,containment=Resource)
def my_logout(request):
return Response('logout')
==> /a/b/login but not /a/login
Arndt.
--
----------------------------------------------------------------------------------------------
Arndt Droullier, Nive GmbH, Köln, Germany
Nive Web Application Publishing: < New Prototype > www.nive.co
Nive Open Source Cms: cms.nive.co
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/groups/opt_out.