Here's an external method that ought to work. Well, it works for me: # acquire the object of name "attr" by context alone def aquire_by_context(self, attr): ob = self while hasattr(ob, 'aq_base'): aq_b=getattr(ob, 'aq_base', ob) if hasattr(aq_b, attr): attr_obj = getattr(aq_b, attr) break; ob = ob.aq_parent else: # not found raise KeyError, attr return self.restrictedTraverse(attr_obj.getPhysicalPath()) This should be secure, because I'm using restrictedTraverse. Use in DTML like this: <dtml-var standard_html_header> <br> <h2>Context</h2> <dtml-var "acquire_by_context(this(), 'O')"> <br> <dtml-var standard_html_footer> Assuming that you called the external method "acquire_by_context". Here, I'm assuming that you have objects like this: some_folder A C B O contains the string "some_folder->B->O" O contains the string "some_folder->O" aq dtml method containing the dtml above Typing into your browser the URL .../some_folder/A/aq will get you some_folder->O Typing into your browser the URL .../some_folder/B/A/aq will get you some_folder->B->O To verify this against normal acquisition, use this DTML: <dtml-var standard_html_header> <br> <h2>Context</h2> <dtml-var "a_context(this(), 'O')"> <br> <h2>Normal</h2> <dtml-var O> <dtml-var standard_html_footer> -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
Steve Alexander wrote:
Here's an external method that ought to work. Well, it works for me:
# acquire the object of name "attr" by context alone def aquire_by_context(self, attr): ob = self while hasattr(ob, 'aq_base'): aq_b=getattr(ob, 'aq_base', ob) if hasattr(aq_b, attr): attr_obj = getattr(aq_b, attr) break; ob = ob.aq_parent else: # not found raise KeyError, attr return self.restrictedTraverse(attr_obj.getPhysicalPath())
I'd left some cruft in there from debugging. Of course, you can simplify it to: # acquire the object of name "attr" by context alone def aquire_by_context(self, attr): ob = self while hasattr(ob, 'aq_base'): if hasattr(ob.aq_base, attr): return self.restrictedTraverse( getattr(ob.aq_base, attr).getPhysicalPath()) ob = ob.aq_parent # not found raise KeyError, attr -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
Steve Alexander wrote:
Of course, you can simplify it to:
# acquire the object of name "attr" by context alone def aquire_by_context(self, attr): ob = self while hasattr(ob, 'aq_base'): if hasattr(ob.aq_base, attr): return self.restrictedTraverse( getattr(ob.aq_base, attr).getPhysicalPath()) ob = ob.aq_parent
# not found raise KeyError, attr
I should point out that this is just for finding an object by context alone. The context-only acquisition meme doesn't get passed on to anything that the returned object might in turn call. This makes this external method less useful than having a fancy acquisition context that checks for security by containment whilst getting attributes by context. However, such a fancy acquisition context would be much more complex to design. -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
participants (1)
-
Steve Alexander