Permalink
Browse files

Merge pull request #7 from JDeuce/master

Add 'get_bind_data' method to FormView
  • Loading branch information...
2 parents 5cbe270 + 25c4a8a commit 2a9ab19fd38b30486b30ab67d6f875c44f5d4f8d @mcdonc mcdonc committed Apr 26, 2012
Showing with 45 additions and 1 deletion.
  1. +2 −0 CHANGES.txt
  2. +1 −0 CONTRIBUTORS.txt
  3. +22 −0 docs/index.rst
  4. +4 −1 pyramid_deform/__init__.py
  5. +16 −0 pyramid_deform/tests.py
View
@@ -2,6 +2,8 @@ Next release
------------
- Python 3 compatibility.
+- Add 'get_bind_data' method to FormView, to allow subclasses the ability
+ to define arbitrary data for binding with the schema.
0.2a4 (2012-04-23)
------------------
View
@@ -108,3 +108,4 @@ Contributors
Chris McDonough, 2010/11/27
Daniel Nouri, 2012/03/30
+Josh Jaques, 2012/04/26
View
@@ -119,6 +119,28 @@ something like this::
</body>
</html>
+
+Deferred Colander Schemas
+-------------------------
+``pyramid_deform.FormView`` will `bind
+<http://docs.pylonsproject.org/projects/colander/en/latest/binding.html>`_ the
+schema by default to the pyramid request. You may wish to bind additional data
+to the schema, which you can do by overriding the get_bind_data method in your
+subclass, like this::
+
+ class PageEditView(FormView):
+ ...
+
+ def get_bind_data(self):
+ # ensure we get any base data defined by FormView
+ data = super(PageEditView, self).get_bind_data()
+ # add any custom data here
+ data.update({
+ 'bind_this_field': 'to this value',
+ 'and_this_field': 'to this value'
+ })
+ return data
+
Wizard
------
@@ -47,10 +47,13 @@ class FormView(object):
def __init__(self, request):
self.request = request
+ def get_bind_data(self):
+ return {'request': self.request}
+
def __call__(self):
use_ajax = getattr(self, 'use_ajax', False)
ajax_options = getattr(self, 'ajax_options', '{}')
- self.schema = self.schema.bind(request=self.request)
+ 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)
self.before(form)
View
@@ -93,6 +93,22 @@ def raiseit(*arg):
self.assertEqual(result,
{'css_links': (), 'js_links': (), 'form': 'failure'})
+ def test_get_bind_data_contains_request(self):
+ request = DummyRequest()
+ inst = self._makeOne(request)
+ data = inst.get_bind_data()
+ self.assertTrue('request' in data)
+
+ def test__call__binds_schema_with_get_bind_data(self):
+ schema = DummySchema()
+ request = DummyRequest()
+ inst = self._makeOne(request)
+ inst.schema = schema
+ inst.form_class = DummyForm
+ inst()
+ # note: DummySchema sets kw to the bind data
+ self.assertEquals(schema.kw, inst.get_bind_data())
+
class TestFormWizardView(unittest.TestCase):
def _makeOne(self, wizard):
from pyramid_deform import FormWizardView

0 comments on commit 2a9ab19

Please sign in to comment.