[Zope3-checkins] SVN: zope.formlib/trunk/ Avoid rendering when an action caused a redirect.

Christian Theune ct at gocept.com
Thu May 29 06:53:26 EDT 2008


Log message for revision 87020:
  Avoid rendering when an action caused a redirect.
  

Changed:
  U   zope.formlib/trunk/CHANGES.txt
  U   zope.formlib/trunk/src/zope/formlib/form.py
  U   zope.formlib/trunk/src/zope/formlib/form.txt

-=-
Modified: zope.formlib/trunk/CHANGES.txt
===================================================================
--- zope.formlib/trunk/CHANGES.txt	2008-05-29 10:48:43 UTC (rev 87019)
+++ zope.formlib/trunk/CHANGES.txt	2008-05-29 10:53:24 UTC (rev 87020)
@@ -18,6 +18,9 @@
 Bugs Fixed
 ----------
 
+- Actions that cause a redirect (301, 302) do not cause the `render` method to
+  be called anymore.
+
 - The zope.formlib.form.Action class didn't fully implement
   zope.formlib.interfaces.IAction.
 

Modified: zope.formlib/trunk/src/zope/formlib/form.py
===================================================================
--- zope.formlib/trunk/src/zope/formlib/form.py	2008-05-29 10:48:43 UTC (rev 87019)
+++ zope.formlib/trunk/src/zope/formlib/form.py	2008-05-29 10:53:24 UTC (rev 87020)
@@ -779,7 +779,12 @@
 
     def __call__(self):
         self.update()
-        return self.render()
+        if self.request.response.getStatus() in [301, 302]:
+            # Avoid rendering if the action caused a redirect.
+            result = self.form_result or ''
+        else:
+            result = self.render()
+        return result
 
     def error_views(self):
         for error in self.errors:

Modified: zope.formlib/trunk/src/zope/formlib/form.txt
===================================================================
--- zope.formlib/trunk/src/zope/formlib/form.txt	2008-05-29 10:48:43 UTC (rev 87019)
+++ zope.formlib/trunk/src/zope/formlib/form.txt	2008-05-29 10:53:24 UTC (rev 87020)
@@ -1835,4 +1835,38 @@
 Cleanup:
 
     >>> zope.event.subscribers.remove(eventLog)
-    
+
+Actions that cause a redirect
+-----------------------------
+
+When an action causes a redirect, the following `render` phase is omitted as
+the result will not be displayed anyway. This is both a performance
+improvement and for avoiding application bugs with one-time session
+information.
+
+    >>> class MyForm(form.Form):
+    ...     form_fields = form.FormFields(IFooBar)
+    ...     @form.action("Redirect")
+    ...     def redirect(self, action, data):
+    ...         print 'Action: redirect'
+    ...         self.request.response.redirect('foo')
+    ...     @form.action("Stay")
+    ...     def redirect(self, action, data):
+    ...         print 'Action: stay'
+    ...         pass
+    ...     def render(self):
+    ...         print 'render was called'
+    ...         return ''
+
+    >>> request = TestRequest()
+    >>> print MyForm(None, request)() # doctest: +NORMALIZE_WHITESPACE
+    render was called
+    >>> request.form['form.actions.redirect'] = u''
+    >>> print MyForm(None, request)() # doctest: +NORMALIZE_WHITESPACE
+    Action: redirect
+
+    >>> request = TestRequest()
+    >>> request.form['form.actions.stay'] = u''
+    >>> print MyForm(None, request)() # doctest: +NORMALIZE_WHITESPACE
+    Action: stay
+    render was called



More information about the Zope3-Checkins mailing list