Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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