Skip to content

Commit

Permalink
Don't do rendering in form's __call__ method when request is a redire…
Browse files Browse the repository at this point in the history
…ction.
  • Loading branch information
Michael Howitz committed Mar 7, 2009
1 parent 9f39b99 commit 0797f50
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ CHANGES
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
Expand Down
27 changes: 25 additions & 2 deletions src/z3c/formui/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ Now our new request should know the table based form template:
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
Expand Down Expand Up @@ -689,6 +689,29 @@ request and render the form again:
</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
-------
Expand Down
11 changes: 10 additions & 1 deletion src/z3c/formui/layout.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -21,13 +21,21 @@
from z3c.template.interfaces import ILayoutTemplate


REDIRECT_STATUS_CODES = (301, 302, 303)


class FormLayoutSupport(object):
"""Layout support for forms except IAddForm."""

layout = None

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)
Expand All @@ -45,6 +53,7 @@ def __call__(self):
if self._finishedAdd:
self.request.response.redirect(self.nextURL())
return ''

if self.layout is None:
layout = zope.component.getMultiAdapter((self, self.request),
ILayoutTemplate)
Expand Down

0 comments on commit 0797f50

Please sign in to comment.