[Checkins] SVN: grok/trunk/src/grok/ Make sure that a redirect in a form's update() won't ocntinue to process or render

Philipp von Weitershausen philikon at philikon.de
Fri Mar 16 14:07:02 EDT 2007


Log message for revision 73227:
  Make sure that a redirect in a form's update() won't ocntinue to process or render
  the form (just like with views).
  

Changed:
  U   grok/trunk/src/grok/components.py
  U   grok/trunk/src/grok/ftests/form/update.py

-=-
Modified: grok/trunk/src/grok/components.py
===================================================================
--- grok/trunk/src/grok/components.py	2007-03-16 18:04:59 UTC (rev 73226)
+++ grok/trunk/src/grok/components.py	2007-03-16 18:07:01 UTC (rev 73227)
@@ -156,7 +156,7 @@
     def __call__(self):
         mapply(self.update, (), self.request)
         if self.request.response.getStatus() in (302, 303):
-            # Somewhere in update(), a redirect was triggered.  Don't
+            # A redirect was triggered somewhere in update().  Don't
             # continue rendering the template or doing anything else.
             return
 
@@ -393,6 +393,11 @@
 
     def __call__(self):
         self.update()
+        if self.request.response.getStatus() in (302, 303):
+            # A redirect was triggered somewhere in update().  Don't
+            # continue rendering the template or doing anything else.
+            return
+
         self.update_form()
         return self.render() 
 

Modified: grok/trunk/src/grok/ftests/form/update.py
===================================================================
--- grok/trunk/src/grok/ftests/form/update.py	2007-03-16 18:04:59 UTC (rev 73226)
+++ grok/trunk/src/grok/ftests/form/update.py	2007-03-16 18:07:01 UTC (rev 73227)
@@ -17,6 +17,21 @@
   >>> browser.open("http://localhost/manfred")
   >>> print browser.contents
   Ellie, the Mammoth reports: The form's update() was called and my name was Manfred.
+
+A form's update() method can issue a redirect.  In that case, the form
+won't proceed to do any form processing nor rendering:
+
+  >>> browser.open("http://localhost/manfred/editredirect")
+  >>> browser.getControl(name="form.name").value = "Mallie"
+  >>> browser.getControl("Apply").click()
+  >>> print browser.url
+  http://localhost/manfred/index
+
+Because of the redirect, no changes happened to the edited object:
+
+  >>> print browser.contents
+  Ellie, the Mammoth reports: The form's update() was called and my name was Manfred.
+
 """
 import grok
 from zope import schema
@@ -36,3 +51,10 @@
     def update(self):
         self.context.report = ("The form's update() was called and my name "
                                "was %s." % self.context.name)
+
+class EditRedirect(grok.EditForm):
+
+    def update(self):
+        # redirect upon form submit so that no changes are ever saved
+        if 'form.name' in self.request:
+            self.redirect(self.url('index'))



More information about the Checkins mailing list