[Zope-dev] Why does restrictedTraverse() in Zope 2 not respect IPublishTraverse adapters?

Jan Hackel plonecode at hackel.name
Sun May 17 16:32:38 EDT 2009


Some days ago I ran into the same problem, and have been pointed to this 
thread. Maybe you are interested in my solution. It's ugly, but I needed it 
for a test-case, where I wanted to access 
"@@plone_context_state/is_view_template":

    >>> from ZPublisher.HTTPRequest import HTTPRequest
    >>> from ZPublisher.HTTPResponse import HTTPResponse
    >>> from Products.PloneTestCase import PloneTestCase
    >>> import base64
    >>> user_id = PloneTestCase.default_user
    >>> password = PloneTestCase.default_password
    >>> encoded = base64.encodestring( '%s:%s' % ( user_id, password ) )
    >>> auth_header = 'basic %s' % encoded
    >>> resp = HTTPResponse()
    >>> env={'SERVER_URL':'http://nohost/plone',
    ...          'URL':'http://nohost/plone',
    ...          'HTTP_AUTHORIZATION': auth_header,
    ...          'REQUEST_METHOD': 'GET',
    ...          'steps': [],
    ...          '_hacked_path': 0,
    ...          '_test_counter': 0,
    ... }
    >>> request = HTTPRequest(stdin=None, environ=env, response=resp)
    >>> request['PARENTS'] = [self.getPortal()]
    >>> contextState = request.traverse("weblog/issue193/"
    ...                  "@@plone_context_state")
    >>> request['ACTUAL_URL'] = 'http://nohost/weblog/issue193'
    >>> contextState.is_view_template()
    True
    >>> request.close()    

Grüße
Jan Hackel

Martin Aspeli wrote:
> There's currently a funny inconsistency in Zope's Traversable class. If
> you have a URL like http://localhost:8080/path/to/@@aview/foo, and
> @@aview implements IPublishTraverse (and, I presume, if there's a custom
> IPublishTraverse adapter for any other path component), URL traversal
> will work fine, but calling to.restrictedTraverse('@@aview/foo') or some
> variant thereof will fail, because (un)restrictedTraverse() does not
> respect custom IPublishTraverse adapters.





More information about the Zope-Dev mailing list