[Checkins] SVN: z3c.formui/branches/1.4/ Don't do rendering in form's __call__ method when request is a redirection.

Michael Howitz mh at gocept.com
Sat Mar 7 06:34:53 EST 2009


Log message for revision 97617:
  Don't do rendering in form's __call__ method when request is a redirection.
  

Changed:
  U   z3c.formui/branches/1.4/CHANGES.txt
  U   z3c.formui/branches/1.4/src/z3c/formui/README.txt
  U   z3c.formui/branches/1.4/src/z3c/formui/layout.py

-=-
Modified: z3c.formui/branches/1.4/CHANGES.txt
===================================================================
--- z3c.formui/branches/1.4/CHANGES.txt	2009-03-07 10:56:09 UTC (rev 97616)
+++ z3c.formui/branches/1.4/CHANGES.txt	2009-03-07 11:34:53 UTC (rev 97617)
@@ -5,6 +5,9 @@
 1.4.3 (unreleased)
 ------------------
 
+- Don't do rendering in form's __call__ method when request is a
+  redirection.
+
 - Added missing test dependency to ``z3c.ptcompat [test]``.
 
 - Fixed tests, so they work together with the current version of

Modified: z3c.formui/branches/1.4/src/z3c/formui/README.txt
===================================================================
--- z3c.formui/branches/1.4/src/z3c/formui/README.txt	2009-03-07 10:56:09 UTC (rev 97616)
+++ z3c.formui/branches/1.4/src/z3c/formui/README.txt	2009-03-07 11:34:53 UTC (rev 97617)
@@ -316,8 +316,8 @@
 Form Macros
 -----------
 
-Load the confguration, which will make sure that all macros get registered
-correctly.
+Load the configuration, which will make sure that all macros get registered
+correctly:
 
   >>> from zope.configuration import xmlconfig
   >>> import zope.component
@@ -689,7 +689,30 @@
     </div>
   </div>
 
+Redirection
+-----------
 
+ The form doesn't bother rendering itself and its layout when
+ request is a redirection as the rendering doesn't make any sense with
+ browser requests in that case. Let's create a view that does a
+ redirection in its update method:
+
+   >>> class RedirectingView(PersonEditForm):
+   ...     def update(self):
+   ...         super(RedirectingView, self).update()
+   ...         self.request.response.redirect('http://www.google.com/')
+
+   It will return an empty string when called as a browser page.
+
+   >>> redirectView = RedirectingView(person, divRequest)
+   >>> redirectView() == ''
+   True
+
+   However, the ``render`` method will render form's template as usual:
+
+   >>> print redirectView.render()
+   <div class="viewspace">...
+
 Cleanup
 -------
 

Modified: z3c.formui/branches/1.4/src/z3c/formui/layout.py
===================================================================
--- z3c.formui/branches/1.4/src/z3c/formui/layout.py	2009-03-07 10:56:09 UTC (rev 97616)
+++ z3c.formui/branches/1.4/src/z3c/formui/layout.py	2009-03-07 11:34:53 UTC (rev 97617)
@@ -1,6 +1,6 @@
 ##############################################################################
 #
-# Copyright (c) 2007 Zope Foundation and Contributors.
+# Copyright (c) 2007-2009 Zope Foundation and Contributors.
 # All Rights Reserved.
 #
 # This software is subject to the provisions of the Zope Public License,
@@ -21,6 +21,9 @@
 from z3c.template.interfaces import ILayoutTemplate
 
 
+REDIRECT_STATUS_CODES = (301, 302, 303)
+
+
 class FormLayoutSupport(object):
     """Layout support for forms except IAddForm."""
 
@@ -28,6 +31,11 @@
 
     def __call__(self):
         self.update()
+
+        if self.request.response.getStatus() in REDIRECT_STATUS_CODES:
+            # don't bother rendering when redirecting
+            return ''
+
         if self.layout is None:
             layout = zope.component.getMultiAdapter((self, self.request),
                 ILayoutTemplate)
@@ -45,6 +53,7 @@
         if self._finishedAdd:
             self.request.response.redirect(self.nextURL())
             return ''
+
         if self.layout is None:
             layout = zope.component.getMultiAdapter((self, self.request),
                 ILayoutTemplate)



More information about the Checkins mailing list