Hooking into traversal
I want to write an object that can be traversed by ZPublisher. The goal is so I can write URLs like thingyForwarder/45/display, to get an object corresponding to thingy #45, and call its display() method. What's the Zope Zen way of doing this? Implement an __of__ method for acquisition? Implement a __bobo_traverse__ method? Make thingyForwarder a Folderish object and implement all the folder methods? -- A.M. Kuchling http://starship.python.net/crew/amk/ "Wet", said he. Classics was his subject, and he sometimes affected a classical simplicity in social conversation. -- Robertson Davies, _Tempest-Tost_
I want to write an object that can be traversed by ZPublisher. The goal is so I can write URLs like thingyForwarder/45/display, to get an object corresponding to thingy #45, and call its display() method.
What's the Zope Zen way of doing this? Implement an __of__ method for acquisition? Implement a __bobo_traverse__ method? Make thingyForwarder a Folderish object and implement all the folder methods?
I think __bobo_traverse__ will do what you want. You can look at the local file system code in LocalFS.py as an example. -jfarr ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hi! I'm a signature virus. Copy me into your .sig to join the fun! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Andrew M. Kuchling" wrote:
I want to write an object that can be traversed by ZPublisher. The goal is so I can write URLs like thingyForwarder/45/display, to get an object corresponding to thingy #45, and call its display() method.
What's the Zope Zen way of doing this? Implement an __of__ method for acquisition? Implement a __bobo_traverse__ method? Make thingyForwarder a Folderish object and implement all the folder methods?
Take yer pick. ZPublisher will try the following methods to traverse an object, in this order: __bobo_traverse__(request, name) Request is, suprise, the REQUEST object, and 'name' is the path element ZPublisher is trying to traverse to. getattr ZPublisher will try and get the subobject by 'name' with getattr. If this fails with an Attribute error then... getitem ZPublisher will try to get the subobject by 'name' with getitem. If this fails, you get a 404. -Michel
ZPublisher will try the following methods to traverse an object, in this order:
[etc] One subtlety to be aware of is that __getitem__ seems to be called *after* acquisition is given a crack for reasons that elude me. Since I've never been able to get an explanation of how to use __getattr__ without blowing up the acquisition machinery, this makes __bobo_traverse__ the only foolproof way that I know of for hooking traversal. Someone please correct me if I am wrong. -jfarr ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hi! I'm a signature virus. Copy me into your .sig to join the fun! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
participants (3)
-
Andrew M. Kuchling -
Jonothan Farr -
Michel Pelletier