restrictedTraverse versus unrestrictedTraverse versus simpler notations
Q1. What is the difference between restrictedTraverse and unrestrictedTraverse? When would one use the latter over the former? Q2. In product code, what is the value in using 'self.restrictedTraverse(id)' or 'self.unrestrictedTraverse(id)' versus simpler notation, such as 'self[id]' or 'getattr(self, id)' ?
On Monday 15 September 2003 01:38 pm, nwingfield@che-llp.com wrote:
Q1. What is the difference between restrictedTraverse and
unrestrictedTraverse? When would one use the latter over the former?
In short, you will almost always use the former, as it is the only one allowed in templates or scripts. unrestrictedTraverse is only usable from unrestricted code, as in a Python Product or External method.
Q2. In product code, what is the value in using
'self.restrictedTraverse(id)' or 'self.unrestrictedTraverse(id)' versus
simpler notation, such as 'self[id]' or 'getattr(self, id)' ?
I think the principle value is that unrestrictedTraverse() will actually follow a path, and it will search attributes and items both to do it. Using restrictedTraverse() would be appropriate if you wanted normal Zope security checks to apply to the traversal (there are reasons you might want that to be -- if you were ever to traverse to a URL acquired from the REQUEST, for example, then it should obviously be subject to the same security as you would expect for restricted code). Since explicitness is probably more desireable in product code, I suspect that it would generally be better to use one of the simpler notations that you mention. There may be other reasons to use it, too. Cheers, Terry -- Terry Hancock ( hancock at anansispaceworks.com ) Anansi Spaceworks http://www.anansispaceworks.com
nwingfield@che-llp.com wrote:
Q1. What is the difference between restrictedTraverse and unrestrictedTraverse? When would one use the latter over the former? Q2. In product code, what is the value in using 'self.restrictedTraverse(id)' or 'self.unrestrictedTraverse(id)' versus simpler notation, such as 'self[id]' or 'getattr(self, id)' ?
restrictedTraverse() can take a path as an argument, while self[id] only takes an id. This is especially usefull if you want to make a reference to another object. For various reasons it is better to save the full path rather than just the id. Ie. if you have a form with a multiple select box where you can choose from a list of objects found by a catalog query. Then it is useless to only save the id of the objects, as they can be positioned anywhere in the hierachy. <select name="objects"> <option="/path/to/obj1"Object 1</> <option="/position/of/obj2"Object 2</> </select> regards Max M
nwingfield@che-llp.com wrote at 2003-9-15 14:38 -0400:
... Q2. In product code, what is the value in using 'self.restrictedTraverse(id)' or 'self.unrestrictedTraverse(id)' versus simpler notation, such as 'self[id]' or 'getattr(self, id)' ?
In addition, what others already said: [un]restrictedTraverse is very similar to how ZPublisher traverses the object hierarchy along the URL. Especially, it uses "__bobo_traverse__" when it is defined. If "__bobo_traverse__" is not defined and "getattr" cannot resolve "id", it uses "getitem" (if it is available). "restrictedTraverse" is what TALES "path" expressions use. Dieter
participants (4)
-
Dieter Maurer -
Max M -
nwingfield@che-llp.com -
Terry Hancock