From ddf77198c9d710a76b8659b5c584d437ccc00aca Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Wed, 5 Dec 2012 06:25:03 +0900 Subject: [PATCH 1/2] adding render_options option to FormView --- docs/index.rst | 3 +++ pyramid_deform/__init__.py | 11 +++++++++-- pyramid_deform/tests.py | 25 +++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 3be4ec8..82db516 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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: diff --git a/pyramid_deform/__init__.py b/pyramid_deform/__init__.py index 675ea02..52142f1 100644 --- a/pyramid_deform/__init__.py +++ b/pyramid_deform/__init__.py @@ -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 @@ -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, } diff --git a/pyramid_deform/tests.py b/pyramid_deform/tests.py index a1a79c9..88c244f 100644 --- a/pyramid_deform/tests.py +++ b/pyramid_deform/tests.py @@ -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 @@ -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' From 70f85265ed5c9e980e05381d684e6b5d4d97eca0 Mon Sep 17 00:00:00 2001 From: Atsushi Odagiri Date: Wed, 5 Dec 2012 06:38:43 +0900 Subject: [PATCH 2/2] sign to CONTRIBUTORS --- CONTRIBUTORS.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 4b885a1..3a131a1 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -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