[ZDP] BackTalk to Document Zope Developer's Guide (2.4
edition)/Object Publishing
webmaster at zope.org
webmaster at zope.org
Tue Feb 17 11:14:28 EST 2004
A comment to the paragraph below was recently added via http://zope.org/Documentation/Books/ZDG/current/ObjectPublishing.stx#4-12
---------------
Here's an example of how to use '__bobo_traverse__'::
def __bobo_traverse__(self, request, key):
# if there is a special cookie set, return special
# subobjects, otherwise return normal subobjects
if request.cookies.has_key('special'):
# return a subobject from the special dict
return self.special_subobjects.get(key, None)
# otherwise return a subobject from the normal dict
return self.normal_subobjects.get(key, None)
% warpeace - Feb. 17, 2004 11:14 am:
The above example is incomplete. This would work fine when called from a normal web request i.e. from
BaseRequest.Traverse. However, when an object containing this __bobo_traverse__ is traversed through
ObjectItem.unrestrictedTraverse or ObjectItem.restrictedTraverse, then the request object passed to
__bobo_traverse__ contains a single key i.e 'TraversalRequestNameStack'. In this case the attibute access
'request.cookies' would raise an AttributeError.
As an example of this consider that you call your object from the ZMI i.e. say
http://localhost:8080/my_bobo_object/manage_workspace.
Now, in manage_workspace, my_bobo_object is again traversed using 'unrestrictedTraverse' which would cause a
very unwieldy 'TypeError'of 'You are not authorized to view this object' and this can be very difficult to
track down.
One very rudimentary solution to above __bobo_traverse__ would be:
def __bobo_traverse__(self, request, key):
if hasattr(request, cookies):
blah...
else:
# probably from unrestrictedTraverse
return getattr(self, key)
Of course, it would be helpful if some one could point out a more generic and an elegant solution.
% warpeace - Feb. 17, 2004 11:14 am:
The above example is incomplete. This would work fine when called from a normal web request i.e. from
BaseRequest.Traverse. However, when an object containing this __bobo_traverse__ is traversed through
ObjectItem.unrestrictedTraverse or ObjectItem.restrictedTraverse, then the request object passed to
__bobo_traverse__ contains a single key i.e 'TraversalRequestNameStack'. In this case the attibute access
'request.cookies' would raise an AttributeError.
As an example of this consider that you call your object from the ZMI i.e. say
http://localhost:8080/my_bobo_object/manage_workspace.
Now, in manage_workspace, my_bobo_object is again traversed using 'unrestrictedTraverse' which would cause a
very unwieldy 'TypeError'of 'You are not authorized to view this object' and this can be very difficult to
track down.
One very rudimentary solution to above __bobo_traverse__ would be:
def __bobo_traverse__(self, request, key):
if hasattr(request, cookies):
blah...
else:
# probably from unrestrictedTraverse
return getattr(self, key)
Of course, it would be helpful if some one could point out a more generic and an elegant solution.
More information about the ZDP
mailing list