I’m new to pyramid as well as the traversal concept. I have a basic object model working with traversal however the objects are location un-aware, so reading up on pyramid documentation found on this page http://docs.pylonsproject.org/projects/pyramid/en/1.0-branch/narr/resources.html#location-aware it talks about using the pyramid_traversalwrapper.
Following the instruction I added the following lines to my main:
config = Configurator(root_factory=Root)
config.registry.registerAdapter(ModelGraphTraverser, (Interface,),
ITraverser)
The rest of my traversal tree objects look like this:
class Root(object):
def __init__(self, request):
pass
def __getitem__(self, key):
if key == 'users':
return UserList()
raise KeyError
class UserList():
def __getitem__(self, key):
users = {'a':None,'b':None, 'c':None}
if key in users:
return User()
raise KeyError
class User(object):
def __init__(self):
pass
def __getitem__(self, key):
if (key == 'channels'):
return Channels()
def GET(self):
return Response("got user")
When I ran the code it does seem to use the ModelGraphTraverser when resolving urls, however what do I have to do to make my model location-aware, as in knowing the __parent__ and __name__ and such? Do I have to add wrapper classes? If so how do I go about doing it? pyramid_traversalwrapper is supposed to make managing __parent__ and __name__ effortless, but I have no idea how to take advantage of it.
pyramid_traversalwrapperautomatically sets the__name__and__parent__attributes of the objects while traversing (you would have to do this by hand otherwise).This means that you can use these attributes of the objects in your views. Taking your example :