PythonScripts and REQUEST, RESPONSE objects
Hello zop'ers I'm doing most of my work with pythonscripts but I have some problems with it: whenever a pythonscript needs to invoke objects like REQUEST or RESPONSE, I have to put those objects at the paramter list and then, when invoking the pythonscript I have to pass those objects to it. This brings me 2 problems: the syntax is much longer, instead of having: <dtml-var MyMethod> I have to type now: <dtml-var "MyMethod(RESPONSE=RESPONSE , REQUEST=REQUEST)"> the other problem is that from now on this object requires to be called from a dtml-method (because it needs these RESPONSE and REQUEST objects as parameters) is there a cleaner solution than doing this? back to my php days, I remember a similar issue with variable scopes on functions, but there were 3 workarounds to access those out-of-scope variables: 1. pass it in the argument list (just like in pythonscript) 2. declare the variables as global (global $foo, $bar) 3. access them thru a $GLOBAL['foo'] $GLOBAL['bar'] is there any similar solution at python script like example 2 and 3? or is it a must to pass those objects in the parameter list? if that's the case: how am I supposed to invoke those pythonscripts from other pythonscripts? so far I've noticed dtml methods being very limited in functionality (but at least they can reach any object) while pythonscript is very flexible in functionality but way too annoying to be used, as it sometimes depends on dtml methods to be called from, so it can be passed objects that cant reach by itself. Isnt there a more integrated and independant solution than this ugly dmtl/pythonscript method mixture ? Hope to be wrong about all this. if I am, please let me know =) Thanks. Alex
The REQUEST object can be accessed from the bound names context and container in a Python Script. For instance, if you passed a Python Script the Form variable foo, you could access it like so: print "Foo is " + context.REQUEST['foo'] print "An object of type %s named %s called a script named %s." % (context.meta_type, context.id, script.id) print "This script lives in the folder " + container.id return printed The RESPONSE object is part of the REQUEST object and could be called like this: context.REQUEST.RESPONSE.redirect('http://www.zope.org')
whenever a pythonscript needs to invoke objects like REQUEST or RESPONSE, I have to put those objects at the paramter list and then, when invoking the pythonscript I have to pass those objects to it.
This brings me 2 problems: the syntax is much longer, instead of having: <dtml-var MyMethod> I have to type now: <dtml-var "MyMethod(RESPONSE=RESPONSE , REQUEST=REQUEST)">
the other problem is that from now on this object requires to be called from a dtml-method (because it needs these RESPONSE and REQUEST objects as parameters)
is there a cleaner solution than doing this?
Yup :) Kevin Teague http://www.bud.ca
Alex Verstraeten wrote:
This brings me 2 problems: the syntax is much longer, instead of having: <dtml-var MyMethod> I have to type now: <dtml-var "MyMethod(RESPONSE=RESPONSE , REQUEST=REQUEST)">
the other problem is that from now on this object requires to be called from a dtml-method (because it needs these RESPONSE and REQUEST objects as parameters)
is there a cleaner solution than doing this?
Yup, the following two lines at the start of your script: ;-) REQUEST = context.REQUEST RESPONSE = context.REQUEST.RESPONSE It'd be nice if Python Scripts did that for you, I wonder if there's any reason they don't? cheers, Chris
On 7 Mar 2001, at 9:54, Chris Withers wrote:
Yup, the following two lines at the start of your script: ;-)
REQUEST = context.REQUEST RESPONSE = context.REQUEST.RESPONSE
It'd be nice if Python Scripts did that for you, I wonder if there's any reason they don't?
I've a better idea. I added three lines (the else part): <snip> if file: self._getOb(id).write(file) else: file ="from Products.PythonScripts.standard import html_quote\nREQUEST = context.REQUEST\nRESPONSE = \ context.REQUEST.RESPONSE\n" self._getOb(id).write(file) <snip> after line 124 in Pythonscript.py. Now each time I add a new PythonScript object, it starts with from Products.PythonScripts.standard import html_quote REQUEST = context.REQUEST RESPONSE = context.REQUEST.RESPONSE If I don't need it, I just delete it. -- Wolfgang Strobl
Wolfgang Strobl wrote:
after line 124 in Pythonscript.py. Now each time I add a new PythonScript object, it starts with
from Products.PythonScripts.standard import html_quote REQUEST = context.REQUEST RESPONSE = context.REQUEST.RESPONSE
Can you stick that in the Collector as a 'Feature w/patch'? cheers, Chris :-)
On 8 Mar 2001, 9:26 Chris Withers wrote:
after line 124 in Pythonscript.py. Now each time I add a new PythonScript object, it starts with
from Products.PythonScripts.standard import html_quote REQUEST = context.REQUEST RESPONSE = context.REQUEST.RESPONSE
Can you stick that in the Collector as a 'Feature w/patch'?
Done. In addition as an afterhought, I suggested adding two additional lines print "This is Script (Python) ",script.getId(),"("+script.title+") at ",container.absolute_url() return printed in analogy to the default content of DTML Methods. Btw, what's the correct way of rendering the default standard_html_header/footer from within a Python script? -- o ( Wolfgang.Strobl@gmd.de (+49 2241) 14-2394 /\ * GMD mbH #include _`\ `_<=== Schloss Birlinghoven, <std.disclaimer> __(_)/_(_)___.-._ 53754 Sankt Augustin, Germany ________________
participants (5)
-
Alex Verstraeten -
Chris Withers -
Kevin Teague -
Wolfgang Strobl -
Wolfgang Strobl