Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ``includeme`` hook for automatic configuration of translations and

configuration of additional template search paths for the ZPT renderer
through the INI file.

Also, take away code blocks from docs as they're not displayed in PyPI
or Github.
  • Loading branch information...
commit 549410328cd6bcf0bcab3800d0cca5ee13440076 1 parent ae5ecff
@dnouri dnouri authored
View
4 .gitignore
@@ -4,3 +4,7 @@
dist/
env26/
.coverage
+bin/
+include/
+lib/
+local
View
7 CHANGES.txt
@@ -1,6 +1,13 @@
Unreleased
----------
+Next release
+------------
+
+- Add ``includeme`` hook for automatic configuration of translations
+ and configuration of additional template search paths for the ZPT
+ renderer through the INI file.
+
0.2a2 (2012-01-29)
------------------
View
1  CONTRIBUTORS.txt
@@ -107,3 +107,4 @@ Contributors
------------
Chris McDonough, 2010/11/27
+Daniel Nouri, 2012/03/30
View
43 docs/index.rst
@@ -11,13 +11,38 @@ Install using setuptools, e.g. (within a virtualenv)::
$ easy_install pyramid_deform
+Configuring translations
+------------------------
+
+pyramid_deform provides an ``includeme`` hook that will set up
+translation paths so that the translations for deform and colander are
+registered. To use this in your project, add ``pyramid_deform`` to
+the ``pyramid.includes`` in your PasteDeploy configuration file. An
+example::
+
+ [myapp:main]
+ ...
+ pyramid.includes = pyramid_debugtoolbar pyramid_tm pyramid_deform
+
+Configuring template search paths
+---------------------------------
+
+pyramid_deform allows you to add template search paths in the
+configuration. An example::
+
+ [myapp:main]
+ ...
+ pyramid_deform.template_search_path = myapp:templates/deform
+
+Thus, if you put a ``form.pt`` into your application's
+``templates/deform`` directory, that will override deform's default
+``form.pt``.
+
FormView Usage
--------------
Consider this `colander
-<http://docs.pylonsproject.org/projects/colander/en/latest/>`_ schema:
-
-.. code-block:: python
+<http://docs.pylonsproject.org/projects/colander/en/latest/>`_ schema::
from colander import Schema, SchemaNode, String
from deform.widget import RichTextWidget, TextAreaWidget
@@ -36,9 +61,7 @@ Consider this `colander
)
You can then write a ``PageEditView`` using
-``pyramid_deform.FormView`` like this:
-
-.. code-block:: python
+``pyramid_deform.FormView`` like this::
from pyramid_deform import FormView
@@ -59,9 +82,7 @@ validates. E.g. it's not called when the ``title`` is left blank, as
it's a required field.
The ``PageEditView`` is registered like any other Pyramid view. Maybe
-like this:
-
-.. code-block:: python
+like this::
from myapp.resources import Page
@@ -75,9 +96,7 @@ like this:
Your template in ``myapp:templates/form.pt`` will receive ``form`` as
a variable: this is the rendered form. Your template might look
-something like this:
-
-.. code-block:: html
+something like this::
<html>
<body>
View
31 pyramid_deform/__init__.py
@@ -1,3 +1,5 @@
+from pkg_resources import resource_filename
+
import colander
import deform
import deform.form
@@ -6,7 +8,9 @@
from deform.form import Button
from pyramid.httpexceptions import HTTPFound
+from pyramid.i18n import get_localizer
from pyramid.i18n import TranslationStringFactory
+from pyramid.threadlocal import get_current_request
_ = TranslationStringFactory('pyramid_deform')
@@ -272,7 +276,6 @@ def get_summary(self, request):
})
return result
-
@colander.deferred
def deferred_csrf_value(node, kw):
return kw['request'].session.get_csrf_token()
@@ -285,7 +288,6 @@ def csrf_validate(node, value):
_('Invalid cross-site scripting token'))
return csrf_validate
-
class CSRFSchema(colander.Schema):
"""
Schema base class which generates and validates a CSRF token
@@ -317,3 +319,28 @@ def aview(request):
validator=deferred_csrf_validator,
)
+def translator(term):
+ request = get_current_request()
+ if request is not None:
+ return get_localizer(request).translate(term)
+ else:
+ return term.interpolate() if hasattr(term, 'interpolate') else term
+
+def configure_zpt_renderer(search_path=()):
+ default_paths = deform.form.Form.default_renderer.loader.search_path
+ paths = []
+ for path in search_path:
+ pkg, resource_name = path.split(':')
+ paths.append(resource_filename(pkg, resource_name))
+ deform.form.Form.default_renderer = deform.ZPTRendererFactory(
+ tuple(paths) + default_paths, translator=translator)
+
+def includeme(config):
+ settings = config.registry.settings
+ search_path = settings.get(
+ 'pyramid_deform.template_search_path', '').strip()
+
+ config.add_translation_dirs('colander:locale', 'deform:locale')
+ config.add_static_view('static-deform', 'deform:static')
+
+ configure_zpt_renderer(search_path.split())
View
55 pyramid_deform/tests.py
@@ -7,6 +7,9 @@
# increment_step
import unittest
+
+from mock import patch
+from mock import Mock
from pyramid import testing
class TestFormView(unittest.TestCase):
@@ -532,7 +535,6 @@ def __init__(self, request):
def __call__(self):
return 'viewed'
-
class DummySerializer(object):
def __init__(self, result):
self.result = result
@@ -542,3 +544,54 @@ def deserialize(self, state):
def serialize(self, state):
return self.result
+
+class TestConfigureZPTRenderer(unittest.TestCase):
+ @patch('deform.form.Form')
+ def test_translator(self, Form):
+ from pyramid_deform import translator
+ from pyramid_deform import configure_zpt_renderer
+
+ configure_zpt_renderer()
+ assert Form.default_renderer.translate is translator
+
+ @patch('deform.form.Form')
+ def test_search_path(self, Form):
+ from pyramid_deform import configure_zpt_renderer
+
+ search_path_before = ('search-path-before',)
+ Form.default_renderer.loader.search_path = search_path_before
+ configure_zpt_renderer(['deform:templates'])
+
+ search_path = Form.default_renderer.loader.search_path
+ assert len(Form.default_renderer.loader.search_path) == 2
+ assert (search_path[-1],) == search_path_before
+ assert search_path[0].endswith('deform/templates')
+
+class TestIncludeMe(unittest.TestCase):
+ @patch('pyramid_deform.configure_zpt_renderer')
+ @patch('deform.form.Form')
+ def test_default(self, Form, configure_zpt_renderer):
+ from pyramid_deform import includeme
+
+ config = Mock()
+ config.registry.settings = {}
+ includeme(config)
+
+ assert config.add_translation_dirs.call_count == 1
+ assert config.add_static_view.call_count == 1
+ configure_zpt_renderer.assert_called_with([])
+
+ @patch('pyramid_deform.configure_zpt_renderer')
+ @patch('deform.form.Form')
+ def test_template_search_path(self, Form, configure_zpt_renderer):
+ from pyramid_deform import includeme
+
+ config = Mock()
+ config.registry.settings = {
+ 'pyramid_deform.template_search_path': 'this-path',
+ }
+ includeme(config)
+
+ assert config.add_translation_dirs.call_count == 1
+ assert config.add_static_view.call_count == 1
+ configure_zpt_renderer.assert_called_with(['this-path'])
Please sign in to comment.
Something went wrong with that request. Please try again.