From a15e35f5f0978f3d524aea5eafc648fc220311d6 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Wed, 16 Jul 2008 15:32:03 +0000 Subject: Added 'repoze.bfg.push:pushpage' decorator o Creates BFG views from callables which take (context, request) and return a mapping of top-level names. --- CHANGES.txt | 8 +++++++- repoze/bfg/push.py | 25 +++++++++++++++++++++++++ repoze/bfg/tests/pp.pt | 3 +++ repoze/bfg/tests/test_push.py | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 repoze/bfg/push.py create mode 100644 repoze/bfg/tests/pp.pt create mode 100644 repoze/bfg/tests/test_push.py diff --git a/CHANGES.txt b/CHANGES.txt index cf765f3a0..7e4a48ccf 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,10 @@ +After 0.1 + + - Added 'repoze.bfg.push:pushpage' decorator, which creates BFG views + from callables which take (context, request) and return a mapping of + top-level names. + 0.1 - Initial release. + - Initial release. diff --git a/repoze/bfg/push.py b/repoze/bfg/push.py new file mode 100644 index 000000000..205c7f26e --- /dev/null +++ b/repoze/bfg/push.py @@ -0,0 +1,25 @@ +import os.path +from repoze.bfg.template import render_template + +class pushpage(object): + """ Decorator for functions which return template namespaces. + + E.g.: + + @pushpage('www/my_template.pt') + def my_view(context, request): + return {'a': 1, 'b': ()} + """ + def __init__(self, template): + self.template = template + + def __call__(self, wrapped): + prefix = os.path.dirname(wrapped.func_globals['__file__']) + path = os.path.join(prefix, self.template) + + def _curried(context, request): + kw = wrapped(context, request) + return render_template(path, **kw) + _curried.__name__ = wrapped.__name__ + + return _curried diff --git a/repoze/bfg/tests/pp.pt b/repoze/bfg/tests/pp.pt new file mode 100644 index 000000000..9df7d22da --- /dev/null +++ b/repoze/bfg/tests/pp.pt @@ -0,0 +1,3 @@ +

WRAPPED

diff --git a/repoze/bfg/tests/test_push.py b/repoze/bfg/tests/test_push.py new file mode 100644 index 000000000..907503983 --- /dev/null +++ b/repoze/bfg/tests/test_push.py @@ -0,0 +1,37 @@ +import unittest + +from zope.component.testing import PlacelessSetup + +class Test_pushpage(unittest.TestCase, PlacelessSetup): + def setUp(self): + PlacelessSetup.setUp(self) + + def tearDown(self): + PlacelessSetup.tearDown(self) + + def _zcmlConfigure(self): + import repoze.bfg + import zope.configuration.xmlconfig + zope.configuration.xmlconfig.file('configure.zcml', package=repoze.bfg) + + def _getTargetClass(self): + from repoze.bfg.push import pushpage + return pushpage + + def _makeOne(self, template): + return self._getTargetClass()(template) + + def test_decorated_has_same_name_as_wrapped(self): + pp = self._makeOne('pp.pt') + wrapped = pp(to_wrap) + self.assertEqual(wrapped.__name__, 'to_wrap') + + def test___call___passes_names_from_wrapped(self): + self._zcmlConfigure() + pp = self._makeOne('pp.pt') + wrapped = pp(to_wrap) + response = wrapped(object(), object()) + self.assertEqual(response.body, '

WRAPPED

') + +def to_wrap(context, request): + return {'wrapped': 'WRAPPED'} -- cgit v1.2.3