[Checkins] SVN: plone.z3cform/trunk/ Don't do the rendering if there is a redirection, use the update/render

Vincent Fretin vincent.fretin at gmail.com
Tue Nov 24 08:26:56 EST 2009


Log message for revision 105981:
  Don't do the rendering if there is a redirection, use the update/render
  pattern for that.
  

Changed:
  U   plone.z3cform/trunk/docs/HISTORY.txt
  U   plone.z3cform/trunk/plone/z3cform/interfaces.py
  U   plone.z3cform/trunk/plone/z3cform/layout.py

-=-
Modified: plone.z3cform/trunk/docs/HISTORY.txt
===================================================================
--- plone.z3cform/trunk/docs/HISTORY.txt	2009-11-24 11:21:18 UTC (rev 105980)
+++ plone.z3cform/trunk/docs/HISTORY.txt	2009-11-24 13:26:55 UTC (rev 105981)
@@ -1,6 +1,13 @@
 Changelog
 =========
 
+0.5.8 - unreleased
+------------------
+
+* Don't do the rendering if there is a redirection, use the update/render
+  pattern for that.
+  [vincentfretin]
+
 0.5.7 - 2009-11-17
 ------------------
 

Modified: plone.z3cform/trunk/plone/z3cform/interfaces.py
===================================================================
--- plone.z3cform/trunk/plone/z3cform/interfaces.py	2009-11-24 11:21:18 UTC (rev 105980)
+++ plone.z3cform/trunk/plone/z3cform/interfaces.py	2009-11-24 13:26:55 UTC (rev 105981)
@@ -4,18 +4,35 @@
 from zope.pagetemplate.interfaces import IPageTemplate
 from z3c.form.interfaces import IForm
 
+
 class IFormWrapper(Interface):
     """Marker interface for the form wrapper
     """
+    def update():
+        """We use the content provider update/render couple.
+        """
+
+    def render():
+        """We use the content provider update/render couple.
+        """
     
+    def contents():
+        """Renders the wrapped form.
+        """
+
     form = Attribute("The form class. Should be set at class level")
     
-    form_instance = schema.Object(title=u"Instance of the form being rendered",
-                                  description=u"Set by the wrapper code during __init__()",
-                                  readonly=True,
-                                  schema=IForm)
+    form_instance = schema.Object(
+        title = u"Instance of the form being rendered",
+        description = u"Set by the wrapper code during __init__()",
+        readonly = True,
+        schema = IForm
+        )
                                   
-    index = schema.Object(title=u"Page template instance",
-                          description=u"If not set, a template will be found via an adapter lookup",
-                          required=False,
-                          schema=IPageTemplate)
\ No newline at end of file
+    index = schema.Object(
+        title = u"Page template instance",
+        description = (u"If not set, a template will be found "
+                       u"via an adapter lookup"),
+        required = False,
+        schema = IPageTemplate
+        )

Modified: plone.z3cform/trunk/plone/z3cform/layout.py
===================================================================
--- plone.z3cform/trunk/plone/z3cform/layout.py	2009-11-24 11:21:18 UTC (rev 105980)
+++ plone.z3cform/trunk/plone/z3cform/layout.py	2009-11-24 13:26:55 UTC (rev 105981)
@@ -17,6 +17,7 @@
 from plone.z3cform import interfaces
 from plone.z3cform import z2
 
+
 class FormWrapper(BrowserView):
     """Use this as a base class for your Five view and override the
     'form' attribute with your z3c.form form class.  Your form will
@@ -26,21 +27,47 @@
     Use the 'wrap' function in this module if you don't like defining
     classes.
     """
-    
     zope.interface.implements(interfaces.IFormWrapper)
     
     form = None # override this with a form class.
-    
     index = None # override with a page template, or rely on an adapter
     request_layer = z3c.form.interfaces.IFormLayer
-    
+
     def __init__(self, context, request):
         super(FormWrapper, self).__init__(context, request)
         if self.form is not None:
-            self.form_instance = self.form(aq_inner(self.context), self.request)
+            self.form_instance = self.form(
+                aq_inner(self.context), self.request)
             self.form_instance.__name__ = self.__name__
 
+    def update(self):
+        """On update, we switch on the zope3 request, needed to work with
+        our z3c form. We update here the wrapped form.
+
+        Override this method if you have more than one form.
+        """
+        z2.switch_on(self, request_layer=self.request_layer)
+        self.form_instance.update()
+
     def __call__(self):
+        """We use the update/render pattern. If a redirect happens in the
+        meantime, we simply skip the rendering.
+        """
+        self.update()
+        if self.request.response.getStatus() in (302, 303):
+            return
+        return self.render()
+
+    def contents(self):
+        """This method renders the wrapped form using its render method.
+        Note that this method does *not* check the redirection. The form
+        will be rendered as if nothing happened.
+
+        Override this method if you have more than one form.
+        """
+        return self.form_instance.render()
+
+    def render(self):
         """This method renders the outer skeleton template, which in
         turn calls the 'contents' method below.
 
@@ -50,30 +77,14 @@
         (self, request) to IPageTemplate and use that instead.
         """
         if self.index is None:
-            template = zope.component.getMultiAdapter((self, self.request), IPageTemplate)
+            template = zope.component.getMultiAdapter(
+                (self, self.request), IPageTemplate)
             if HAS_BOUND_PAGETEMPLATE:
                 return BoundPageTemplate(template, self)()
             else:
                 return template.__of__(self)(self)
         return self.index()
 
-    def contents(self):
-        """This is the method that'll call your form.  You don't
-        usually override this.
-        """
-        # A call to 'switch_on' is required before we can render
-        # z3c.forms within Zope 2.
-        z2.switch_on(self, request_layer=self.request_layer)
-        return self.render_form()
-
-    def render_form(self):
-        """This method returns the rendered z3c.form form.
-
-        Override this method if you need to pass a different context
-        to your form, or if you need to render a number of forms.
-        """
-        return self.form_instance()
-
     def label(self):
         """Override this method to use a different way of acquiring a
         label or title for your page.  Overriding this with a simple
@@ -81,9 +92,11 @@
         """
         return self.form_instance.label
 
+
 def wrap_form(form, __wrapper_class=FormWrapper, **kwargs):
     class MyFormWrapper(__wrapper_class):
         pass
+    assert z3c.form.interfaces.IForm.implementedBy(form)
     MyFormWrapper.form = form
     for name, value in kwargs.items():
         setattr(MyFormWrapper, name, value)



More information about the checkins mailing list