diff options
| author | Chris McDonough <chrism@agendaless.com> | 2009-10-10 01:03:53 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2009-10-10 01:03:53 +0000 |
| commit | 820508eb8200692cc8b1510caa19903d2af79c5a (patch) | |
| tree | 3f562b5a07ceba4fee0dbb60ba9144417d2de8fa /docs/tutorials | |
| parent | 9bbaa8a0be555560d8f1dad7bcf10bb64c500097 (diff) | |
| download | pyramid-820508eb8200692cc8b1510caa19903d2af79c5a.tar.gz pyramid-820508eb8200692cc8b1510caa19903d2af79c5a.tar.bz2 pyramid-820508eb8200692cc8b1510caa19903d2af79c5a.zip | |
Convert bfgwiki to use renderers.
Diffstat (limited to 'docs/tutorials')
16 files changed, 166 insertions, 239 deletions
diff --git a/docs/tutorials/bfgwiki/authorization.rst b/docs/tutorials/bfgwiki/authorization.rst index 444a0e5db..9944f69e0 100644 --- a/docs/tutorials/bfgwiki/authorization.rst +++ b/docs/tutorials/bfgwiki/authorization.rst @@ -83,19 +83,17 @@ into its template. We'll add something like this to each view body: logged_in = authenticated_userid(request) -We'll then change the return value of ``render_template_to_response`` -within each view to pass the `resulting `logged_in`` value to the +We'll then change the return value of each view that has an associated +``renderer`` to pass the `resulting `logged_in`` value to the template, e.g.: .. code-block:: python :linenos: - return render_template_to_response('templates/view.pt', - request = request, - page = context, - content = content, - logged_in = logged_in, - edit_url = edit_url) + return dict(page = context, + content = content, + logged_in = logged_in, + edit_url = edit_url) Adding the ``login.pt`` Template ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/tutorials/bfgwiki/basiclayout.rst b/docs/tutorials/bfgwiki/basiclayout.rst index d57cd7134..5df7f2487 100644 --- a/docs/tutorials/bfgwiki/basiclayout.rst +++ b/docs/tutorials/bfgwiki/basiclayout.rst @@ -30,18 +30,28 @@ XML namespace. Our sample ZCML file looks like the following: #. *Line 4*. Boilerplate, the comment explains. -#. *Lines 6-9*. Register a ``<view>`` that is bound to a class. +#. *Lines 6-10*. Register a ``<view>`` that is ``for`` a class. ``.views.my_view`` is a *function* we write (generated by the ``bfg_zodb`` template) that is given a ``context`` and a - ``request`` and returns a response. + ``request`` and which returns a dictionary. The ``renderer`` tag + indicates that the ``templates/mytemplate.pt`` template should be + used to turn the dictionary returned by the view into a response. + ``templates/mytemplate.pt`` is a *relative* path: it names the + ``mytemplate.pt`` file which lives in the ``templates`` + subdirectory of the directory in which this ``configure.zcml`` + lives in. In this case, it means it lives in the ``tutorial`` + package's ``templates`` directory as ``mytemplate.pt`` Since this ``<view>`` doesn't have a ``name`` attribute, it is the "default" view for that class. -#. *Lines 11-15*. Register a ``static`` view which answers requests +#. *Lines 12-15*. Register a ``static`` view which answers requests which start with ``/static``. This is a view that will serve up static resources for us, in this case, at - ``http://localhost:6543/static/`` and below. + ``http://localhost:6543/static/`` and below. The ``path`` element + of this tag is a relative directory name, so it finds the resources + it should serve within the ``templates/static`` directory inside + the ``tutorial`` package. Content Models with ``models.py`` --------------------------------- diff --git a/docs/tutorials/bfgwiki/definingviews.rst b/docs/tutorials/bfgwiki/definingviews.rst index 53578e90f..699a86d66 100644 --- a/docs/tutorials/bfgwiki/definingviews.rst +++ b/docs/tutorials/bfgwiki/definingviews.rst @@ -6,6 +6,19 @@ Views in BFG are typically simple Python functions that accept two parameters: :term:`context`, and :term:`request`. A view is assumed to return a :term:`response` object. +.. note:: A :mod:`repoze.bfg` view can also be defined as callable + which accepts *one* arguments: a :term:`request`. You'll see this + two-argument pattern used in other :mod:`repoze.bfg` tutorials and + applications. Either calling convention will work in any + :mod:`repoze.bfg` application. In :term:`traversal` based + applications, such as this tutorial, the context is used frequently + within the body of a view method, so it makes sense to use the + two-argument syntax in this application. However, in :term:`url + dispatch` based applications, however, the context object is rarely + used in the view body itself, so within code that uses + URL-dispatch-only, it's common to define views as callables that + accept only a request to avoid the visual "noise". + Adding View Functions ===================== @@ -63,18 +76,22 @@ HTML containing various view and add links for WikiWords based on the content of our current page object. We then generate an edit URL (because it's easier to do here than in -the template), and we call the -``repoze.bfg.chameleon_zpt.render_template_to_response`` function with -a number of arguments. The first argument is the *relative* path to a -:term:`Chameleon` ZPT template. It is relative to the directory of -the file in which we're creating the ``view_page`` function. The -``render_template_to_response`` function also accepts ``request``, -``page``, ``content``, and ``edit_url`` as keyword arguments. As a -result, the template will be able to use these names to perform -various rendering tasks. - -The result of ``render_template_to_response`` is returned to -:mod:`repoze.bfg`. Unsurprisingly, it is a response object. +the template), and we wrap up a number of arguments in a dictionary +and return it. + +The arguments we wrap into a dictionary include ``page``, ``content``, +and ``edit_url``. As a result, the *template* associated with this +view will be able to use these names to perform various rendering +tasks. The template associated with this view will be a template +which lives in ``templates/view.pt``, which we'll associate with this +view via the ``configure.zcml`` file. + +Note the contrast between this view and the ``view_wiki`` view. In +the ``view_wiki`` view, we return a *response* object. In this view, +we return a *dictionary*. It is *always* fine to return a response +object from a :mod:`repoze.bfg` view. Returning a dictionary is +allowed only when there is a ``renderer`` associated with the view in +the view configuration. The ``add_page`` view function ------------------------------ @@ -263,19 +280,36 @@ add four ``view`` declarations to ``configure.zcml``. #. Add a declaration which maps the "Wiki" class in our ``models.py`` file to the view named ``view_wiki`` in our ``views.py`` file with - no view name. This is the default view for a Wiki. - -#. Add a declaration which maps the "Page" class in our ``models.py`` - file to the view named ``view_page`` in our ``views.py`` file with - no view name. This is the default view for a Page. + no view name. This is the default view for a Wiki. It does not + use a ``renderer`` because the ``view_wiki`` view callable always + returns a *response* object rather than a dictionary. #. Add a declaration which maps the "Wiki" class in our ``models.py`` file to the view named ``add_page`` in our ``views.py`` file with - the view name ``add_page``. This is the add view for a new Page. + the view name ``add_page``. Associate this view with the + ``templates/edit.pt`` template file via the ``renderer`` attribute. + This view will use the :term:`Chameleon` ZPT renderer configured + with the ``templates/edit.pt`` template to render non-*response* + return values from the ``add_page`` view. This is the add view for + a new Page. + +#. Add a declaration which maps the "Page" class in our ``models.py`` + file to the view named ``view_page`` in our ``views.py`` file with + no view name. Associate this view with the ``templates/view.pt`` + template file via the ``renderer`` attribute. This view will use + the :term:`Chameleon` ZPT renderer configured with the + ``templates/view.pt`` template to render non-*response* return + values from the ``view_page`` view. This is the default view for a + Page. #. Add a declaration which maps the "Page" class in our ``models.py`` file to the view named ``edit_page`` in our ``views.py`` file with - the view name ``edit_page``. This is the edit view for a page. + the view name ``edit_page``. Associate this view with the + ``templates/edit.pt`` template file via the ``renderer`` attribute. + This view will use the :term:`Chameleon` ZPT renderer configured + with the ``templates/edit.pt`` template to render non-*response* + return values from the ``edit_page`` view. This is the edit view + for a page. As a result of our edits, the ``configure.zcml`` file should look something like so: @@ -349,10 +383,10 @@ our application in a browser. The views we'll try are as follows: <http://localhost:6543/add_page/SomePageName>`_ in a browser invokes the add view for a page. -- To generate an error, do `http://localhost:6543/add_page - <http://localhost:6543/add_page>`_. IndexError for - ``request.subpath[0]``. You'll see an interactive traceback - facility provided by evalerror. +- To generate an error, visit `http://localhost:6543/add_page + <http://localhost:6543/add_page>`_ which will generate an + ``IndexError`` for the expression ``request.subpath[0]``. You'll + see an interactive traceback facility provided by evalerror. diff --git a/docs/tutorials/bfgwiki/src/authorization/tutorial/login.py b/docs/tutorials/bfgwiki/src/authorization/tutorial/login.py index 70697ec2e..d9d65bdca 100644 --- a/docs/tutorials/bfgwiki/src/authorization/tutorial/login.py +++ b/docs/tutorials/bfgwiki/src/authorization/tutorial/login.py @@ -1,6 +1,5 @@ from webob.exc import HTTPFound -from repoze.bfg.chameleon_zpt import render_template_to_response from repoze.bfg.view import bfg_view from repoze.bfg.url import model_url @@ -10,7 +9,7 @@ from repoze.bfg.security import forget from tutorial.models import Wiki from tutorial.security import USERS -@bfg_view(for_=Wiki, name='login') +@bfg_view(for_=Wiki, name='login', renderer='templates/login.pt') def login(context, request): login_url = model_url(context, request, 'login') referrer = request.url @@ -29,14 +28,12 @@ def login(context, request): headers = headers) message = 'Failed login' - return render_template_to_response( - 'templates/login.pt', + return dict( message = message, url = request.application_url + '/login', came_from = came_from, login = login, password = password, - request =request, ) @bfg_view(for_=Wiki, name='logout') diff --git a/docs/tutorials/bfgwiki/src/authorization/tutorial/tests.py b/docs/tutorials/bfgwiki/src/authorization/tutorial/tests.py index 84a3a0f37..cd29d6ff4 100644 --- a/docs/tutorials/bfgwiki/src/authorization/tutorial/tests.py +++ b/docs/tutorials/bfgwiki/src/authorization/tutorial/tests.py @@ -72,18 +72,17 @@ class ViewPageTests(unittest.TestCase): context.__parent__ = wiki context.__name__ = 'thepage' request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/view.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) self.assertEqual( - renderer.content, + info['content'], '<div class="document">\n' '<p>Hello <a href="http://example.com/add_page/CruelWorld">' 'CruelWorld</a> ' '<a href="http://example.com/IDoExist/">' 'IDoExist</a>' '</p>\n</div>\n') - self.assertEqual(renderer.edit_url, + self.assertEqual(info['edit_url'], 'http://example.com/thepage/edit_page') @@ -99,13 +98,14 @@ class AddPageTests(unittest.TestCase): return add_page(context, request) def test_it_notsubmitted(self): + from repoze.bfg.url import model_url context = testing.DummyModel() request = testing.DummyRequest() request.subpath = ['AnotherPage'] - renderer = testing.registerDummyRenderer('templates/edit.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) - self.assertEqual(renderer.page.data, '') + info = self._callFUT(context, request) + self.assertEqual(info['page'].data,'') + self.assertEqual(info['save_url'], + model_url(context, request, 'add_page', 'AnotherPage')) def test_it_submitted(self): context = testing.DummyModel() @@ -130,12 +130,13 @@ class EditPageTests(unittest.TestCase): return edit_page(context, request) def test_it_notsubmitted(self): + from repoze.bfg.url import model_url context = testing.DummyModel() request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/edit.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) - self.assertEqual(renderer.page, context) + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) + self.assertEqual(info['save_url'], + model_url(context, request, 'edit_page')) def test_it_submitted(self): context = testing.DummyModel() @@ -145,6 +146,3 @@ class EditPageTests(unittest.TestCase): response = self._callFUT(context, request) self.assertEqual(response.location, 'http://example.com/') self.assertEqual(context.data, 'Hello yo!') - - - diff --git a/docs/tutorials/bfgwiki/src/authorization/tutorial/views.py b/docs/tutorials/bfgwiki/src/authorization/tutorial/views.py index beb52e1b7..26a44fcda 100644 --- a/docs/tutorials/bfgwiki/src/authorization/tutorial/views.py +++ b/docs/tutorials/bfgwiki/src/authorization/tutorial/views.py @@ -3,7 +3,6 @@ import re from webob.exc import HTTPFound from repoze.bfg.url import model_url -from repoze.bfg.chameleon_zpt import render_template_to_response from repoze.bfg.security import authenticated_userid @@ -19,7 +18,7 @@ wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)") def view_wiki(context, request): return HTTPFound(location = model_url(context, request, 'FrontPage')) -@bfg_view(for_=Page, permission='view') +@bfg_view(for_=Page, renderer='templates/view.pt', permission='view') def view_page(context, request): wiki = context.__parent__ @@ -39,14 +38,11 @@ def view_page(context, request): logged_in = authenticated_userid(request) - return render_template_to_response('templates/view.pt', - request = request, - page = context, - content = content, - logged_in = logged_in, - edit_url = edit_url) + return dict(page = context, content = content, edit_url = edit_url, + logged_in = logged_in) -@bfg_view(for_=Wiki, name='add_page', permission='edit') +@bfg_view(for_=Wiki, name='add_page', renderer='templates/edit.pt', + permission='edit') def add_page(context, request): name = request.subpath[0] if 'form.submitted' in request.params: @@ -63,13 +59,10 @@ def add_page(context, request): logged_in = authenticated_userid(request) - return render_template_to_response('templates/edit.pt', - request = request, - page = page, - logged_in = logged_in, - save_url = save_url) + return dict(page = page, save_url = save_url, logged_in = logged_in) -@bfg_view(for_=Page, name='edit_page', permission='edit') +@bfg_view(for_=Page, name='edit_page', renderer='templates/edit.pt', + permission='edit') def edit_page(context, request): if 'form.submitted' in request.params: context.data = request.params['body'] @@ -77,11 +70,7 @@ def edit_page(context, request): logged_in = authenticated_userid(request) - return render_template_to_response('templates/edit.pt', - request = request, - page = context, - logged_in = logged_in, - save_url = model_url(context, request, - 'edit_page') - ) - + return dict(page = context, + save_url = model_url(context, request, 'edit_page'), + logged_in = logged_in) + diff --git a/docs/tutorials/bfgwiki/src/basiclayout/tutorial/configure.zcml b/docs/tutorials/bfgwiki/src/basiclayout/tutorial/configure.zcml index f990298c5..e15d3a65d 100644 --- a/docs/tutorials/bfgwiki/src/basiclayout/tutorial/configure.zcml +++ b/docs/tutorials/bfgwiki/src/basiclayout/tutorial/configure.zcml @@ -6,6 +6,7 @@ <view for=".models.MyModel" view=".views.my_view" + renderer="templates/mytemplate.pt" /> <static diff --git a/docs/tutorials/bfgwiki/src/basiclayout/tutorial/tests.py b/docs/tutorials/bfgwiki/src/basiclayout/tutorial/tests.py index 408645fcb..f8c0ceb20 100644 --- a/docs/tutorials/bfgwiki/src/basiclayout/tutorial/tests.py +++ b/docs/tutorials/bfgwiki/src/basiclayout/tutorial/tests.py @@ -3,71 +3,9 @@ import unittest from repoze.bfg import testing class ViewTests(unittest.TestCase): - - """ These tests are unit tests for the view. They test the - functionality of *only* the view. They register and use dummy - implementations of repoze.bfg functionality to allow you to avoid - testing 'too much'""" - - def setUp(self): - """ cleanUp() is required to clear out the application registry - between tests (done in setUp for good measure too) - """ - testing.cleanUp() - - def tearDown(self): - """ cleanUp() is required to clear out the application registry - between tests - """ - testing.cleanUp() - def test_my_view(self): from tutorial.views import my_view context = testing.DummyModel() request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/mytemplate.pt') - response = my_view(context, request) - renderer.assert_(project='tutorial') - -class ViewIntegrationTests(unittest.TestCase): - """ These tests are integration tests for the view. These test - the functionality the view *and* its integration with the rest of - the repoze.bfg framework. They cause the entire environment to be - set up and torn down as if your application was running 'for - real'. This is a heavy-hammer way of making sure that your tests - have enough context to run properly, and it tests your view's - integration with the rest of BFG. You should not use this style - of test to perform 'true' unit testing as tests will run faster - and will be easier to write if you use the testing facilities - provided by bfg and only the registrations you need, as in the - above ViewTests. - """ - def setUp(self): - """ This sets up the application registry with the - registrations your application declares in its configure.zcml - (including dependent registrations for repoze.bfg itself). - """ - testing.cleanUp() - import tutorial - import zope.configuration.xmlconfig - zope.configuration.xmlconfig.file('configure.zcml', - package=tutorial) - - def tearDown(self): - """ Clear out the application registry """ - testing.cleanUp() - - def test_my_view(self): - from tutorial.views import my_view - context = testing.DummyModel() - request = testing.DummyRequest() - result = my_view(context, request) - self.assertEqual(result.status, '200 OK') - body = result.app_iter[0] - self.failUnless('Welcome to' in body) - self.assertEqual(len(result.headerlist), 2) - self.assertEqual(result.headerlist[0], - ('Content-Type', 'text/html; charset=UTF-8')) - self.assertEqual(result.headerlist[1], ('Content-Length', - str(len(body)))) - + info = my_view(context, request) + self.assertEqual(info['project'], 'tutorial') diff --git a/docs/tutorials/bfgwiki/src/basiclayout/tutorial/views.py b/docs/tutorials/bfgwiki/src/basiclayout/tutorial/views.py index 923c1fffa..9a2c37bc3 100644 --- a/docs/tutorials/bfgwiki/src/basiclayout/tutorial/views.py +++ b/docs/tutorials/bfgwiki/src/basiclayout/tutorial/views.py @@ -1,6 +1,2 @@ -from repoze.bfg.chameleon_zpt import render_template_to_response - def my_view(context, request): - return render_template_to_response('templates/mytemplate.pt', - request = request, - project = 'tutorial') + return {'project':'tutorial'} diff --git a/docs/tutorials/bfgwiki/src/models/tutorial/tests.py b/docs/tutorials/bfgwiki/src/models/tutorial/tests.py index f367b7da0..1b540a60c 100644 --- a/docs/tutorials/bfgwiki/src/models/tutorial/tests.py +++ b/docs/tutorials/bfgwiki/src/models/tutorial/tests.py @@ -48,28 +48,9 @@ class AppmakerTests(unittest.TestCase): self.failUnless(root['app_root'] is app_root) class ViewTests(unittest.TestCase): - - """ These tests are unit tests for the view. They test the - functionality of *only* the view. They register and use dummy - implementations of repoze.bfg functionality to allow you to avoid - testing 'too much'""" - - def setUp(self): - """ cleanUp() is required to clear out the application registry - between tests (done in setUp for good measure too) - """ - testing.cleanUp() - - def tearDown(self): - """ cleanUp() is required to clear out the application registry - between tests - """ - testing.cleanUp() - def test_my_view(self): from tutorial.views import my_view context = testing.DummyModel() request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/mytemplate.pt') - response = my_view(context, request) - renderer.assert_(project='tutorial') + info = my_view(context, request) + self.assertEqual(info['project'], 'tutorial') diff --git a/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/tests.py b/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/tests.py index 84a3a0f37..cd29d6ff4 100644 --- a/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/tests.py +++ b/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/tests.py @@ -72,18 +72,17 @@ class ViewPageTests(unittest.TestCase): context.__parent__ = wiki context.__name__ = 'thepage' request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/view.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) self.assertEqual( - renderer.content, + info['content'], '<div class="document">\n' '<p>Hello <a href="http://example.com/add_page/CruelWorld">' 'CruelWorld</a> ' '<a href="http://example.com/IDoExist/">' 'IDoExist</a>' '</p>\n</div>\n') - self.assertEqual(renderer.edit_url, + self.assertEqual(info['edit_url'], 'http://example.com/thepage/edit_page') @@ -99,13 +98,14 @@ class AddPageTests(unittest.TestCase): return add_page(context, request) def test_it_notsubmitted(self): + from repoze.bfg.url import model_url context = testing.DummyModel() request = testing.DummyRequest() request.subpath = ['AnotherPage'] - renderer = testing.registerDummyRenderer('templates/edit.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) - self.assertEqual(renderer.page.data, '') + info = self._callFUT(context, request) + self.assertEqual(info['page'].data,'') + self.assertEqual(info['save_url'], + model_url(context, request, 'add_page', 'AnotherPage')) def test_it_submitted(self): context = testing.DummyModel() @@ -130,12 +130,13 @@ class EditPageTests(unittest.TestCase): return edit_page(context, request) def test_it_notsubmitted(self): + from repoze.bfg.url import model_url context = testing.DummyModel() request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/edit.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) - self.assertEqual(renderer.page, context) + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) + self.assertEqual(info['save_url'], + model_url(context, request, 'edit_page')) def test_it_submitted(self): context = testing.DummyModel() @@ -145,6 +146,3 @@ class EditPageTests(unittest.TestCase): response = self._callFUT(context, request) self.assertEqual(response.location, 'http://example.com/') self.assertEqual(context.data, 'Hello yo!') - - - diff --git a/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/views.py b/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/views.py index 0725ffe28..a5d8f6b6a 100644 --- a/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/views.py +++ b/docs/tutorials/bfgwiki/src/viewdecorators/tutorial/views.py @@ -3,7 +3,6 @@ import re from webob.exc import HTTPFound from repoze.bfg.url import model_url -from repoze.bfg.chameleon_zpt import render_template_to_response from repoze.bfg.view import bfg_view from tutorial.models import Page @@ -16,7 +15,7 @@ wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)") def view_wiki(context, request): return HTTPFound(location = model_url(context, request, 'FrontPage')) -@bfg_view(for_=Page) +@bfg_view(for_=Page, renderer='templates/view.pt') def view_page(context, request): wiki = context.__parent__ @@ -33,13 +32,9 @@ def view_page(context, request): content = publish_parts(context.data, writer_name='html')['html_body'] content = wikiwords.sub(check, content) edit_url = model_url(context, request, 'edit_page') - return render_template_to_response('templates/view.pt', - request = request, - page = context, - content = content, - edit_url = edit_url) - -@bfg_view(for_=Wiki, name='add_page') + return dict(page = context, content = content, edit_url = edit_url) + +@bfg_view(for_=Wiki, name='add_page', renderer='templates/edit.pt') def add_page(context, request): name = request.subpath[0] if 'form.submitted' in request.params: @@ -53,22 +48,15 @@ def add_page(context, request): page = Page('') page.__name__ = name page.__parent__ = context - return render_template_to_response('templates/edit.pt', - request = request, - page = page, - save_url = save_url) - -@bfg_view(for_=Page, name='edit_page') + return dict(page = page, save_url = save_url) + +@bfg_view(for_=Page, name='edit_page', renderer='templates/edit.pt') def edit_page(context, request): if 'form.submitted' in request.params: context.data = request.params['body'] return HTTPFound(location = model_url(context, request)) - return render_template_to_response('templates/edit.pt', - request = request, - page = context, - save_url = model_url(context, request, - 'edit_page') - ) + return dict(page = context, + save_url = model_url(context, request, 'edit_page')) diff --git a/docs/tutorials/bfgwiki/src/views/tutorial/configure.zcml b/docs/tutorials/bfgwiki/src/views/tutorial/configure.zcml index 8fa1a10c9..038677bfc 100644 --- a/docs/tutorials/bfgwiki/src/views/tutorial/configure.zcml +++ b/docs/tutorials/bfgwiki/src/views/tutorial/configure.zcml @@ -17,17 +17,20 @@ for=".models.Wiki" name="add_page" view=".views.add_page" + renderer="templates/edit.pt" /> <view for=".models.Page" view=".views.view_page" + renderer="templates/view.pt" /> <view for=".models.Page" name="edit_page" view=".views.edit_page" + renderer="templates/edit.pt" /> </configure> diff --git a/docs/tutorials/bfgwiki/src/views/tutorial/tests.py b/docs/tutorials/bfgwiki/src/views/tutorial/tests.py index 84a3a0f37..888618842 100644 --- a/docs/tutorials/bfgwiki/src/views/tutorial/tests.py +++ b/docs/tutorials/bfgwiki/src/views/tutorial/tests.py @@ -72,18 +72,17 @@ class ViewPageTests(unittest.TestCase): context.__parent__ = wiki context.__name__ = 'thepage' request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/view.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) self.assertEqual( - renderer.content, + info['content'], '<div class="document">\n' '<p>Hello <a href="http://example.com/add_page/CruelWorld">' 'CruelWorld</a> ' '<a href="http://example.com/IDoExist/">' 'IDoExist</a>' '</p>\n</div>\n') - self.assertEqual(renderer.edit_url, + self.assertEqual(info['edit_url'], 'http://example.com/thepage/edit_page') @@ -99,13 +98,14 @@ class AddPageTests(unittest.TestCase): return add_page(context, request) def test_it_notsubmitted(self): + from repoze.bfg.url import model_url context = testing.DummyModel() request = testing.DummyRequest() request.subpath = ['AnotherPage'] - renderer = testing.registerDummyRenderer('templates/edit.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) - self.assertEqual(renderer.page.data, '') + info = self._callFUT(context, request) + self.assertEqual(info['page'].data,'') + self.assertEqual(info['save_url'], + model_url(context, request, 'add_page', 'AnotherPage')) def test_it_submitted(self): context = testing.DummyModel() @@ -130,12 +130,13 @@ class EditPageTests(unittest.TestCase): return edit_page(context, request) def test_it_notsubmitted(self): + from repoze.bfg.url import model_url context = testing.DummyModel() request = testing.DummyRequest() - renderer = testing.registerDummyRenderer('templates/edit.pt') - response = self._callFUT(context, request) - self.assertEqual(renderer.request, request) - self.assertEqual(renderer.page, context) + info = self._callFUT(context, request) + self.assertEqual(info['page'], context) + self.assertEqual(info['save_url'], + model_url(context, request, 'edit_page')) def test_it_submitted(self): context = testing.DummyModel() diff --git a/docs/tutorials/bfgwiki/src/views/tutorial/views.py b/docs/tutorials/bfgwiki/src/views/tutorial/views.py index e99864550..57ca24d1b 100644 --- a/docs/tutorials/bfgwiki/src/views/tutorial/views.py +++ b/docs/tutorials/bfgwiki/src/views/tutorial/views.py @@ -3,7 +3,6 @@ import re from webob.exc import HTTPFound from repoze.bfg.url import model_url -from repoze.bfg.chameleon_zpt import render_template_to_response from tutorial.models import Page @@ -29,11 +28,7 @@ def view_page(context, request): content = publish_parts(context.data, writer_name='html')['html_body'] content = wikiwords.sub(check, content) edit_url = model_url(context, request, 'edit_page') - return render_template_to_response('templates/view.pt', - request = request, - page = context, - content = content, - edit_url = edit_url) + return dict(page = context, content = content, edit_url = edit_url) def add_page(context, request): name = request.subpath[0] @@ -48,21 +43,14 @@ def add_page(context, request): page = Page('') page.__name__ = name page.__parent__ = context - return render_template_to_response('templates/edit.pt', - request = request, - page = page, - save_url = save_url) + return dict(page = page, save_url = save_url) def edit_page(context, request): if 'form.submitted' in request.params: context.data = request.params['body'] return HTTPFound(location = model_url(context, request)) - return render_template_to_response('templates/edit.pt', - request = request, - page = context, - save_url = model_url(context, request, - 'edit_page') - ) + return dict(page = context, + save_url = model_url(context, request, 'edit_page')) diff --git a/docs/tutorials/bfgwiki/viewdecorators.rst b/docs/tutorials/bfgwiki/viewdecorators.rst index c0414a36e..299e82658 100644 --- a/docs/tutorials/bfgwiki/viewdecorators.rst +++ b/docs/tutorials/bfgwiki/viewdecorators.rst @@ -31,6 +31,11 @@ The ``bfg_view`` callable accepts a number of arguments: The name of the view. +``renderer`` + + The renderer (usually a *template name*) that will be used when the + view returns a non-:term:`response` object. + There are other arguments which this callable accepts, but these are the ones we're going to use. @@ -67,7 +72,7 @@ The decorator above the ``view_page`` function will be: .. code-block:: python :linenos: - @bfg_view(for_=Page) + @bfg_view(for_=Page, renderer='templates/view.pt') This indicates that the view is "for" the Page class and has the *empty* view_name (indicating the default view). After injecting this @@ -80,9 +85,9 @@ decorator, we can now *remove* the following from our <view for=".models.Page" view=".views.view_page" + renderer="templates/view.pt" /> - Our new decorator takes its place. The ``add_page`` view function @@ -93,7 +98,7 @@ The decorator above the ``add_page`` function will be: .. code-block:: python :linenos: - @bfg_view(for_=Wiki, name='add_page') + @bfg_view(for_=Wiki, name='add_page', renderer='templates/edit.pt') This indicates that the view is "for" the Wiki class and has the ``add_page`` view_name. After injecting this decorator, we can now @@ -106,6 +111,7 @@ This indicates that the view is "for" the Wiki class and has the for=".models.Wiki" name="add_page" view=".views.add_page" + renderer="templates/edit.pt" /> Our new decorator takes its place. @@ -118,7 +124,7 @@ The decorator above the ``edit_page`` function will be: .. code-block:: python :linenos: - @bfg_view(for_=Page, name='edit_page') + @bfg_view(for_=Page, name='edit_page', renderer='templates/edit.pt') This indicates that the view is "for" the Page class and has the ``edit_page`` view_name. After injecting this decorator, we can now @@ -131,6 +137,7 @@ This indicates that the view is "for" the Page class and has the for=".models.Page" name="edit_page" view=".views.edit_page" + renderer="templates/edit.pt" /> Our new decorator takes its place. |
