Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding render_options option to FormView for passing options to render method #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ Daniel Nouri, 2012/03/30
Josh Jaques, 2012/04/26
Alexander Fedorov, 2012/04/26
Lane Stevens, 2012/09/11
Atsushi Odagiri, 2012/12/05
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ method. In the case above, we customise the ID for the form using the
and more. For more details, see
http://deform.readthedocs.org/en/latest/api.html#deform.Form.

And ``render_options`` is provided too.
This options to be passed as keyword arguments to the form class' ``render`` method.

The ``PageEditView`` is registered like any other Pyramid view. Maybe
like this:

Expand Down
11 changes: 9 additions & 2 deletions pyramid_deform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class FormView(object):
#: passed to this class' ``__init__`` can be provided here.
form_options = ()

#: Two-tuple of options to pass as keyword arguments when rendering
#: the form instance of :attr:`form_class`. Any options that can be
#: passed to this form's ``render`` can be provided here.
render_options = ()

def __init__(self, request):
self.request = request

Expand Down Expand Up @@ -167,11 +172,13 @@ def show(self, form):
if :meth:`appstruct` provides one. Otherwise, it is rendered without.
Returns the rendered form as the ``form`` key in a ``dict`` structure.
"""

render_options = dict(self.render_options)
appstruct = self.appstruct()
if appstruct is None:
rendered = form.render()
rendered = form.render(**render_options)
else:
rendered = form.render(appstruct)
rendered = form.render(appstruct, **render_options)
return {
'form': rendered,
}
Expand Down
25 changes: 23 additions & 2 deletions pyramid_deform/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ def check_form(self, form):
for key, value in dict(form_options).items():
self.assertEqual(getattr(form, key), value)

def test_render_options_applied_without_appstruct(self):
request = DummyRequest()
schema = DummySchema()
inst = self._makeOne(request)
inst.render_options = (("readonly", True),)
form = DummyForm(schema)

result = inst.show(form)
self.assertEqual(result['form'], "rendered with None with options {'readonly': True}")

def test_render_options_applied_with_appstruct(self):
request = DummyRequest()
schema = DummySchema()
inst = self._makeOne(request)
inst.render_options = (("readonly", True),)
inst.appstruct = lambda: {'my': 'appstruct'}
form = DummyForm(schema)

result = inst.show(form)
self.assertEqual(result['form'], "rendered with {'my': 'appstruct'} with options {'readonly': True}")

class TestFormWizardView(unittest.TestCase):
def _makeOne(self, wizard):
from pyramid_deform import FormWizardView
Expand Down Expand Up @@ -646,9 +667,9 @@ def __init__(self, schema, buttons=None, use_ajax=False, ajax_options='',
def get_widget_resources(self):
return {'js':(), 'css':()}

def render(self, appstruct=None):
def render(self, appstruct=None, **kw):
self.appstruct = appstruct
return 'rendered with {0}'.format(appstruct)
return 'rendered with {0}'.format(appstruct) + (" with options {0}".format(kw) if kw else "")

def validate(self, controls):
return 'validated'
Expand Down