[Twisted-Python] [PATCH] Getting request in woven.form callback

jml at ids.org.au jml at ids.org.au
Wed May 28 13:46:34 EDT 2003


Hello,

I've been wanting to get at request in the woven.form callback for a while 
now. After trying several different kludges in my code, I tried patching form 
and t.p.formmethod.

Example:
   def login(request, username=None, password=None):
       # do stuff

   sig = MethodSignature(
            String('username', '', 'Username', 'Your username'),
            Password('password', '', 'Password', 'Your password'))
   loginMethod = FormMethod(sig, login, True)

This works really well for me.

cheers,
jml

Here's the patch:

Index: python/formmethod.py
===================================================================
RCS file: /cvs/Twisted/twisted/python/formmethod.py,v
retrieving revision 1.21
diff -u -r1.21 formmethod.py
--- python/formmethod.py	22 May 2003 21:25:48 -0000	1.21
+++ python/formmethod.py	28 May 2003 06:20:29 -0000
@@ -260,9 +260,10 @@
 class FormMethod:
     """A callable object with a signature."""
 
-    def __init__(self, signature, callable):
+    def __init__(self, signature, callable, takesRequest=False):
         self.signature = signature
         self.callable = callable
+        self.takesRequest = takesRequest
 
     def getArgs(self):
         return tuple(self.signature.methodSignature)
Index: web/woven/form.py
===================================================================
RCS file: /cvs/Twisted/twisted/web/woven/form.py,v
retrieving revision 1.37
diff -u -r1.37 form.py
--- web/woven/form.py	15 May 2003 06:31:43 -0000	1.37
+++ web/woven/form.py	28 May 2003 06:20:31 -0000
@@ -332,7 +332,10 @@
             return self.errback(self.errorModelFactory(request.args, outDict, errDict)).render(request)
         else:
             try:
-                outObj = self.formMethod.call(**outDict)
+                if self.formMethod.takesRequest:
+                    outObj = self.formMethod.call(request=request, **outDict)
+                else:
+                    outObj = self.formMethod.call(**outDict)
             except formmethod.FormException, e:
                 err = request.errorInfo = self.errorModelFactory(request.args, outDict, e)
                 return self.errback(err).render(request)



----- End forwarded message -----
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030529/92bb4d24/attachment.pgp 


More information about the Twisted-Python mailing list