[Zope-dev] Why does restrictedTraverse() in Zope 2 not respect IPublishTraverse adapters?
Martin Aspeli
optilude+lists at gmail.com
Wed May 20 06:35:05 EDT 2009
Jan Hackel wrote:
> 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()
For this, you should possibly use zope.testbrowser (and
Products.Five.testbrowser).
The reason you needed this particular hack was that the
is_view_template() method looks at the url quite specifically. For most
cases, context.restrictedTraverse('@@plone_context_state') would've worked.
Martin
--
Author of `Professional Plone Development`, a book for developers who
want to work with Plone. See http://martinaspeli.net/plone-book
More information about the Zope-Dev
mailing list