Skip to content

Commit

Permalink
Add new prepare_form method for FormView
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjb committed Jan 30, 2013
1 parent 71b75e8 commit 8004253
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
31 changes: 21 additions & 10 deletions pyramid_deform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,26 @@ def get_bind_data(self):
"""
return {'request': self.request}

def prepare_form(self):
"""
Prepares the form object according to the provided options.
This method, in addition to instantiating an instance of the
given :attr:``form_class``, will process the form by calling
:meth:`before`. Returns an instance of the :attr:`form_class`.
"""
use_ajax = getattr(self, 'use_ajax', False)
ajax_options = getattr(self, 'ajax_options', '{}')
self.schema = self.schema.bind(**self.get_bind_data())
form = self.form_class(self.schema, buttons=self.buttons,
use_ajax=use_ajax, ajax_options=ajax_options,
**dict(self.form_options))
self.before(form)
return form

def __call__(self):
"""
Prepares and render the form according to provided options.
Prepares and renders the form according to provided options.
Upon receiving a ``POST`` request, this method will validate
the request against the form instance. After validation,
Expand All @@ -90,13 +107,7 @@ def __call__(self):
Returns a ``dict`` structure suitable for provision tog the given
view. By default, this is the page template specified
"""
use_ajax = getattr(self, 'use_ajax', False)
ajax_options = getattr(self, 'ajax_options', '{}')
self.schema = self.schema.bind(**self.get_bind_data())
form = self.form_class(self.schema, buttons=self.buttons,
use_ajax=use_ajax, ajax_options=ajax_options,
**dict(self.form_options))
self.before(form)
form = self.prepare_form()
reqts = form.get_widget_resources()
result = None

Expand Down Expand Up @@ -130,8 +141,8 @@ def before(self, form):
By default, this method does nothing. Override this method
in your dervived class to modify the ``form``. Your function
will be executed immediately after instansiating the form
instance in :meth:`__call__` (thus before obtaining widget resources,
considering buttons, or rendering).
instance in :meth:`prepare_form` (thus before obtaining widget
resources, considering buttons, or rendering during :meth:`__call__`).
"""
pass

Expand Down
17 changes: 17 additions & 0 deletions pyramid_deform/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ def check_form(self, form):
for key, value in dict(form_options).items():
self.assertEqual(getattr(form, key), value)

def test_prepare_form(self):
schema = DummySchema()
request = DummyRequest()
inst = self._makeOne(request)
inst.schema = schema
inst.form_class = DummyForm

def manipulate_form(self, form):
form.custom_attr = 'custom-attr'

inst.before = types.MethodType(manipulate_form, inst)
form = inst.prepare_form()

#Form should be correct type and customised according to options
self.assertTrue(isinstance(form, DummyForm))
self.assertEqual(form.custom_attr, 'custom-attr')

class TestFormWizardView(unittest.TestCase):
def _makeOne(self, wizard):
from pyramid_deform import FormWizardView
Expand Down

0 comments on commit 8004253

Please sign in to comment.