[Zope] Getting REQUEST in product method by default

Brian Lloyd brian@zope.com
Fri, 1 Feb 2002 09:35:39 -0500


> I am writing a Zope product that wraps a non-Zope Python class.  I
> would like to mirror some of the methods of this class in the Zope
> wrapper class, but have them only accept the REQUEST and RESPONSE
> variables.  For example,
> 
> class MyClass:
>     """ Normal class """
>     def getData(self, user, field):
>        ...
> 
> class ZMyClass(MyClass, Folder):
>     """ Zope Folder subclass """
>     def getData(self, REQUEST, RESPONSE=None):
>        ... validate REQUEST variables ...
>        return MyClass.getData(REQUEST['user'], REQUEST['field'])
> 
> I want to do this because I thought that I could then do a simple
> 
> <dtml-var getData>
> 
> to get the result of the ZMyClass.getData method, but this doesn't
> seem to work.  I don't know how the innards of Zope work, but I
> thought that methods called in this way were always given the REQUEST
> varible.  I keep getting REQUEST=None.  Is it possible to do this?

REQUEST / RESPONSE (or any method arguments) are only passed 
'automatically' when a method is directly traversed to 
through the Web (or some other protocol).

Note that as long as your ZMyClass supports Acquisition (which
it would if it is a subclass of Folder), you can always get the 
request via acquisition. So your class would look like:

class ZMyClass(MyClass, Folder):
    """ Zope Folder subclass """
    def getData(self):
       REQUEST = self.REQUEST
       RESPONSE = REQUEST.RESPONSE
       ... validate REQUEST variables ...
       return MyClass.getData(REQUEST['user'], REQUEST['field'])

This way you can use <dtml-var getData> without passing 
any arguments.

Hope this helps!



Brian Lloyd        brian@zope.com
Software Engineer  540.361.1716       
Zope Corporation   http://www.zope.com