Help! How do I get my namespace in Python??
I have a Python method in a product, which in turn calls a DTML Method. def test(self, REQUEST=None): return self.myDTMLMethod(REQUEST) But the DTML Method doesn't have access to the namespace! I see I can pass in a mapping to the DTML Method, but how do I get the namespace in Python? Thanks! Andrew
Andrew Wilcox wrote:
I have a Python method in a product, which in turn calls a DTML Method.
def test(self, REQUEST=None): return self.myDTMLMethod(REQUEST)
But the DTML Method doesn't have access to the namespace!
What namespace?
I see I can pass in a mapping to the DTML Method, but how do I get the namespace in Python?
You probably want the instance's namespace plus the request namespace. If this is the case, you want: def test(self, REQUEST=None): return self.myDTMLMethod(self, REQUEST) DocumentTemplates (of which DTMLMethods are special cases) take two positional arguments: - An object who's attributes are added to the namespace, - An object who's keys are added to the namespace. Jim -- Jim Fulton mailto:jim@digicool.com Technical Director (888) 344-4332 Python Powered! Digital Creations http://www.digicool.com http://www.python.org Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email address may not be added to any commercial mail list with out my permission. Violation of my privacy with advertising or SPAM will result in a suit for a MINIMUM of $500 damages/incident, $1500 for repeats.
But the DTML Method doesn't have access to the namespace!
What namespace?
The regular DTML namespace, _.
You probably want the instance's namespace plus the request namespace. If this is the case, you want:
def test(self, REQUEST=None): return self.myDTMLMethod(self, REQUEST)
Nope, doesn't work! For example, first try a plain DTML Document called "A": <dtml-var standard_html_header> <dtml-let food="'Popcorn'"> X says: <dtml-var X> </dtml-let> <dtml-var standard_html_footer> which calls a DTML Method called "X": My favorite food is <dtml-var food> Run A and you'll get: X says: My favorite food is Popcorn Now make a method called "test" which also calls X: def test(self, REQUEST=None): return self.X(self, REQUEST) But try to call X through test: <dtml-var standard_html_header> <dtml-let food="'Popcorn'"> test says: <dtml-var test> </dtml-let> <dtml-var standard_html_footer> and I get: Error Type: KeyError Error Value: food See, X can no longer see "food" in the namespace. Andrew
Andrew Wilcox wrote:
But the DTML Method doesn't have access to the namespace!
What namespace?
The regular DTML namespace, _.
You probably want the instance's namespace plus the request namespace. If this is the case, you want:
def test(self, REQUEST=None): return self.myDTMLMethod(self, REQUEST)
Nope, doesn't work!
For example, first try a plain DTML Document called "A":
<dtml-var standard_html_header> <dtml-let food="'Popcorn'"> X says: <dtml-var X> </dtml-let> <dtml-var standard_html_footer>
which calls a DTML Method called "X":
My favorite food is <dtml-var food>
Run A and you'll get:
X says: My favorite food is Popcorn
Now make a method called "test" which also calls X:
def test(self, REQUEST=None): return self.X(self, REQUEST)
But try to call X through test:
<dtml-var standard_html_header> <dtml-let food="'Popcorn'"> test says: <dtml-var test> </dtml-let> <dtml-var standard_html_footer>
and I get:
Error Type: KeyError Error Value: food
See, X can no longer see "food" in the namespace.
If this is what you want to do, then you need to pass the namespace to the Python function. You will have to do this explicitly. def test(self, namespace): return self.myDTMLMethod(self, namespace) if test is an ordinary Python method (defined in some base class), then you don't need to pass self, so from DTML: <dtml-var standard_html_header> <dtml-let food="'Popcorn'"> test says: <dtml-var expr="test(_)"> </dtml-let> <dtml-var standard_html_footer> If test is an external method, then, unfortunately, you need to pass the self along: <dtml-var standard_html_header> <dtml-let food="'Popcorn'"> test says: <dtml-var expr="test(this(),_)"> </dtml-let> <dtml-var standard_html_footer> Jim -- Jim Fulton mailto:jim@digicool.com Technical Director (888) 344-4332 Python Powered! Digital Creations http://www.digicool.com http://www.python.org Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email address may not be added to any commercial mail list with out my permission. Violation of my privacy with advertising or SPAM will result in a suit for a MINIMUM of $500 damages/incident, $1500 for repeats.
Jim Fulton wrote:
If this is what you want to do, then you need to pass the namespace to the Python function. You will have to do this explicitly.
def test(self, namespace): return self.myDTMLMethod(self, namespace)
if test is an ordinary Python method (defined in some base class), then you don't need to pass self, so from DTML:
<dtml-var standard_html_header> <dtml-let food="'Popcorn'"> test says: <dtml-var expr="test(_)"> </dtml-let> <dtml-var standard_html_footer>
This works! But is there a danger of bypassing security? I am working on a product, tableView, which Zope hosters may want to install for their users. If I don't pass in the namespace, the called DTML doesn't get permissions to do much of anything. If I do pass in the namespace, then it looks like it gets regular permissions. But if Zope is relying on the namespace to check permissions, then is there a danger that a user using my product could create their own munged dictionary, pass it in as the "namespace", and give themselves superuser status? For my particular product, it might be OK if the called DTML was part of the product and not modifiable by the user, but it would be good to know what's safe and what's not. Andrew
participants (2)
-
Andrew Wilcox -
Jim Fulton