[Zope-dev] Why does restrictedTraverse() in Zope 2 not respect IPublishTraverse adapters?
Paul Winkler
slinkp at gmail.com
Fri May 15 15:28:08 EDT 2009
On Thu, May 14, 2009 at 10:55:40PM +0200, Laurence Rowe wrote:
> > For maximum portability across Z2 / Z3 / BFG, you could just do the same
> > thing and implement __getitem__ on any object you want to be traversable
> > by either the publisher or APIs like (un)restrictedTraverse, and forego
> > the over-complicated component-laden traversal dance. ;)
>
> Minimal example demonstrating this with a view in zope2:
>
> >>> from zope.component import getSiteManager
> >>> from Testing.makerequest import makerequest
> >>> from zope.publisher.browser import IBrowserView
> >>> from Acquisition import Explicit
> >>> from zope.component import getSiteManager
> >>> app = makerequest(app)
> >>> smgr = getSiteManager()
> >>> class Foo(Explicit):
> ... def __init__(self, context, request):
> ... self.context, self.request = context, request
> ... def __getitem__(self, key):
> ... return int(key)
> ...
> >>> smgr.registerAdapter(Foo, (None, IRequest), IBrowserView, name='foo')
> >>> app.unrestrictedTraverse('@@foo/12345')
> 12345
Thanks for reminding me of this. I keep forgetting that this works!
I only add that if you want to use __getitem__ for publishing, the
items you return should inherit from Acquisition.(Im|Ex)plicit to make
the security machinery happy.
--
Paul Winkler
http://www.slinkp.com
More information about the Zope-Dev
mailing list