summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCasey Duncan <casey.duncan@gmail.com>2010-12-31 13:48:13 -0700
committerCasey Duncan <casey.duncan@gmail.com>2010-12-31 13:48:13 -0700
commit22642d73056f06a1834dce8970922951fa18c5a1 (patch)
treee56037a9d48e7900b5ec3396b1ae28a2723ca623
parentf4c5f1a60612749ef36aae01d9a3a559b6acdfff (diff)
parentadfcf6d579496495fb71f8c1af293a953b3a13cb (diff)
downloadpyramid-22642d73056f06a1834dce8970922951fa18c5a1.tar.gz
pyramid-22642d73056f06a1834dce8970922951fa18c5a1.tar.bz2
pyramid-22642d73056f06a1834dce8970922951fa18c5a1.zip
Merge https://github.com/Pylons/pyramid
-rw-r--r--.gitignore1
-rw-r--r--CHANGES.txt62
-rw-r--r--CONTRIBUTORS.txt8
-rw-r--r--TODO.txt11
-rw-r--r--docs/api/interfaces.rst4
-rw-r--r--docs/conf.py2
-rw-r--r--docs/designdefense.rst8
-rw-r--r--docs/index.rst1
-rw-r--r--docs/latexindex.rst3
-rw-r--r--docs/narr/assets.rst4
-rw-r--r--docs/narr/csrf.rst2
-rw-r--r--docs/narr/flash.rst14
-rw-r--r--docs/narr/introduction.rst2
-rw-r--r--docs/narr/project.rst2
-rw-r--r--docs/narr/renderers.rst1
-rw-r--r--docs/narr/resources.rst6
-rw-r--r--docs/narr/urldispatch.rst44
-rw-r--r--docs/tutorials/cmf/actions.rst28
-rw-r--r--docs/tutorials/cmf/catalog.rst73
-rw-r--r--docs/tutorials/cmf/content.rst67
-rw-r--r--docs/tutorials/cmf/index.rst38
-rw-r--r--docs/tutorials/cmf/missing.rst22
-rw-r--r--docs/tutorials/cmf/skins.rst23
-rw-r--r--docs/tutorials/cmf/workflow.rst14
-rw-r--r--pyramid/chameleon_text.py33
-rw-r--r--pyramid/config.py696
-rw-r--r--pyramid/httpexceptions.py1
-rw-r--r--pyramid/interfaces.py21
-rw-r--r--pyramid/paster.py100
-rw-r--r--pyramid/renderers.py12
-rw-r--r--pyramid/request.py2
-rw-r--r--pyramid/tests/test_chameleon_text.py25
-rw-r--r--pyramid/tests/test_chameleon_zpt.py17
-rw-r--r--pyramid/tests/test_config.py130
-rw-r--r--pyramid/tests/test_paster.py155
-rw-r--r--pyramid/tests/test_view.py11
-rw-r--r--pyramid/url.py4
-rw-r--r--pyramid/view.py12
-rw-r--r--pyramid/zcml.py5
-rw-r--r--setup.py1
40 files changed, 950 insertions, 715 deletions
diff --git a/.gitignore b/.gitignore
index ae0b17b8e..562abec68 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,7 @@
.coverage
tutorial.db
env26/
+env26-debug/
env24/
env27/
jyenv/
diff --git a/CHANGES.txt b/CHANGES.txt
index e58a1dc76..04f5a7d05 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,60 @@ Next release
Bug Fixes
---------
+- The ``proutes`` command tried too hard to resolve the view for printing,
+ resulting in exceptions when an exceptional root factory was encountered.
+ Instead of trying to resolve the view, if it cannot, it will now just print
+ ``<unknown>``.
+
+Features
+--------
+
+- ``config.add_view`` now accepts a ``decorator`` keyword argument, a callable
+ which will decorate the view callable before it is added to the registry.
+
+- ``config.add_view`` now accepts a ``view_mapper`` keyword argument, which
+ should be a class which implements the new
+ ``pyramid.interfaces.IViewMapperFactory`` interface. Use of an alternate
+ view mapper allows objects that are meant to be used as view callables to
+ have an arbitrary argument list and an arbitrary result. This feature will
+ be used by Pyramid extension developers, not by "civilians".
+
+- If a handler class provides an __action_decorator__ attribute (usually a
+ classmethod or staticmethod), use that as the decorator for each view
+ registration for that handler.
+
+Documentation
+-------------
+
+- The (weak) "Converting a CMF Application to Pyramid" tutorial has been
+ removed from the tutorials section. It was moved to the
+ ``pyramid_tutorials`` Github repository.
+
+Internals
+---------
+
+- The "view derivation" code is now factored into a set of classes rather
+ than a large number of standalone functions (a side effect of the
+ ``view_mapper`` refactoring).
+
+- The ``pyramid.renderer.RendererHelper`` class has grown a ``render_view``
+ method, which is used by the default view mapper (a side effect of the
+ ``view_mapper`` refactoring).
+
+- The object passed as ``renderer`` to the "view deriver" is now an instance
+ of ``pyramid.renderers.RendererHelper`` rather than a dictionary (a side
+ effect of ``view_mapper`` refactoring).
+
+- The class used as the "page template" in ``pyramid.chameleon_text`` was
+ removed, in preference to using a Chameleon-inbuilt version.
+
+
+1.0a8 (2010-12-27)
+==================
+
+Bug Fixes
+---------
+
- The name ``registry`` was not available in the ``paster pshell``
environment under IPython.
@@ -25,6 +79,10 @@ Features
arguments to add_route work by raising an exception during configuration if
view-related arguments exist but no ``view`` argument is passed.
+- Add ``paster proute`` command which displays a summary of the routing
+ table. See the narrative documentation section within the "URL Dispatch"
+ chapter entitled "Displaying All Application Routes".
+
Paster Templates
----------------
@@ -81,6 +139,10 @@ Documentation
- Merge "Static Assets" chapter into the "Assets" chapter.
+- Added narrative documentation section within the "URL Dispatch" chapter
+ entitled "Displaying All Application Routes" (for ``paster proutes``
+ command).
+
1.0a7 (2010-12-20)
==================
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index 40234836e..7b0364b6d 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -90,7 +90,7 @@ Licensing Exceptions
Code committed within the ``docs/`` subdirectory of the Pyramid source
control repository and "docstrings" which appear in the documentation
-generated by runnning "make" within this directory is licensed under the
+generated by running "make" within this directory is licensed under the
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States
License (http://creativecommons.org/licenses/by-nc-sa/3.0/us/).
@@ -114,3 +114,9 @@ Contributors
- Blaise Laflamme, 2010/11/09
- Chris Rossi, 2010/11/10
+
+- Casey Duncan, 2010/12/27
+
+- Rob Miller, 2010/12/28
+
+- Marius Gedminas, 2010/12/31
diff --git a/TODO.txt b/TODO.txt
index f81c78387..5acc923a1 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -8,6 +8,14 @@ Must-Have (before 1.0)
- Consider deprecations for ``model`` and ``resource`` APIs.
+- Re-make testing.setUp() and testing.tearDown() the canonical APIs for test
+ configuration.
+
+- Document ``decorator=`` and ``view_mapper`` parameters to add_view.
+
+- Allow ``decorator=`` and ``view_mapper=`` to be passed via ZCML and the
+ ``view_config`` decorator.
+
Should-Have
-----------
@@ -26,9 +34,6 @@ Should-Have
- Change "Cleaning up After a Request" in the urldispatch chapter to
use ``request.add_response_callback``.
-- ``decorator=`` parameter to view_config. This would replace the existing
- _map_view "decorator" if it existed.
-
- Provide a response_set_cookie method on the request for rendered responses
that can be used as input to response.set_cookie?
diff --git a/docs/api/interfaces.rst b/docs/api/interfaces.rst
index b3c14e5f7..3ce926230 100644
--- a/docs/api/interfaces.rst
+++ b/docs/api/interfaces.rst
@@ -35,3 +35,7 @@ Other Interfaces
.. autointerface:: ITemplateRenderer
+ .. autointerface:: IViewMapperFactory
+
+ .. autointerface:: IViewMapper
+
diff --git a/docs/conf.py b/docs/conf.py
index 7bcdf3a07..8c238cecd 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -76,7 +76,7 @@ copyright = '%s, Agendaless Consulting' % datetime.datetime.now().year
# other places throughout the built documents.
#
# The short X.Y version.
-version = '1.0a7'
+version = '1.0a8'
# The full version, including alpha/beta/rc tags.
release = version
diff --git a/docs/designdefense.rst b/docs/designdefense.rst
index 53b95b9d0..1d6941283 100644
--- a/docs/designdefense.rst
+++ b/docs/designdefense.rst
@@ -1604,10 +1604,10 @@ If you can understand this hello world program, you can use Pyramid:
app = config.make_wsgi_app()
serve(app)
-Pyramid has ~ 650 of documentation (printed), covering topics from the very
-basic to the most advanced. *Nothing* is left undocumented, quite literally.
-It also has an *awesome*, very helpful community. Visit the #repoze and/or
-#pylons IRC channels on freenode.net and see.
+Pyramid has ~ 650 pages of documentation (printed), covering topics from the
+very basic to the most advanced. *Nothing* is left undocumented, quite
+literally. It also has an *awesome*, very helpful community. Visit the
+#repoze and/or #pylons IRC channels on freenode.net and see.
Hate Zope
+++++++++
diff --git a/docs/index.rst b/docs/index.rst
index 3281de13c..ecd401ddb 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -79,7 +79,6 @@ applications to various platforms.
tutorials/wiki/index.rst
tutorials/wiki2/index.rst
tutorials/bfg/index.rst
- tutorials/cmf/index.rst
tutorials/gae/index.rst
tutorials/modwsgi/index.rst
tutorials/zeo/index.rst
diff --git a/docs/latexindex.rst b/docs/latexindex.rst
index 25e6791d0..058835937 100644
--- a/docs/latexindex.rst
+++ b/docs/latexindex.rst
@@ -125,7 +125,9 @@ ZCML Directive Reference
zcml/configure
zcml/default_permission
zcml/forbidden
+ zcml/handler
zcml/include
+ zcml/localenegotiator
zcml/notfound
zcml/remoteuserauthenticationpolicy
zcml/renderer
@@ -134,6 +136,7 @@ ZCML Directive Reference
zcml/scan
zcml/static
zcml/subscriber
+ zcml/translationdir
zcml/utility
zcml/view
diff --git a/docs/narr/assets.rst b/docs/narr/assets.rst
index f147426ce..f73ff231a 100644
--- a/docs/narr/assets.rst
+++ b/docs/narr/assets.rst
@@ -98,7 +98,7 @@ directory on a filesystem to an application user's browser. Use the
mechanism makes a directory of static files available at a name relative to
the application root URL, e.g. ``/static`` or as an external URL.
-.. note:: `~pyramid.config.Configurator.add_static_view` cannot serve a
+.. note:: :meth:`~pyramid.config.Configurator.add_static_view` cannot serve a
single file, nor can it serve a directory of static files directly
relative to the root URL of a :app:`Pyramid` application. For these
features, see :ref:`advanced_static`.
@@ -282,7 +282,7 @@ create such a circumstance, we suggest using the
in the application ``.ini`` file named ``media_location``. Then set the
value of ``media_location`` to either a prefix or a URL depending on whether
the application is being run in development or in production (use a different
-`.ini`` file for production than you do for development). This is just a
+``.ini`` file for production than you do for development). This is just a
suggestion for a pattern; any setting name other than ``media_location``
could be used.
diff --git a/docs/narr/csrf.rst b/docs/narr/csrf.rst
index 7586b0ed7..2f545fb4f 100644
--- a/docs/narr/csrf.rst
+++ b/docs/narr/csrf.rst
@@ -9,7 +9,7 @@ phenomenon whereby a user with an identity on your website might click on a
URL or button on another website which unwittingly redirects the user to your
application to perform some command that requires elevated privileges.
-You can avoid most of these attacks by making sure that a the correct *CSRF
+You can avoid most of these attacks by making sure that the correct *CSRF
token* has been set in an :app:`Pyramid` session object before performing any
actions in code which requires elevated privileges and is invoked via a form
post. To use CSRF token support, you must enable a :term:`session factory`
diff --git a/docs/narr/flash.rst b/docs/narr/flash.rst
index d41c2cdaf..037bfc416 100644
--- a/docs/narr/flash.rst
+++ b/docs/narr/flash.rst
@@ -38,7 +38,7 @@ provide is not modified in any way.
The ``queue`` argument allows you to choose a queue to which to append the
message you provide. This can be used to push different kinds of messages
-into flash storage for later display in different places on a page. You cam
+into flash storage for later display in different places on a page. You can
pass any name for your queue, but it must be a string. The default value is
the empty string, which chooses the default queue. Each queue is independent,
and can be popped by ``pop_flash`` or examined via ``peek_flash`` separately.
@@ -49,20 +49,22 @@ default flash message queue.
request.session.flash(msg, 'myappsqueue')
-The ``allow_duplicate`` argument, which defaults to ``True``. If this is
+The ``allow_duplicate`` argument defaults to ``True``. If this is
``False``, if you attempt to add a message to a queue which is already
present in the queue, it will not be added.
Using the ``session.pop_flash`` Method
--------------------------------------
-Once one or more messages has been added to a flash queue by the
+Once one or more messages have been added to a flash queue by the
``session.flash`` API, the ``session.pop_flash`` API can be used to pop that
queue and return it for use.
To pop a particular queue of messages from the flash object, use the session
object's ``pop_flash`` method.
+.. method:: pop_flash(queue='')
+
.. code-block:: python
:linenos:
@@ -85,14 +87,16 @@ been popped.
The object returned from ``pop_flash`` is a list.
-Using the ``session.pop_flash`` Method
---------------------------------------
+Using the ``session.peek_flash`` Method
+---------------------------------------
Once one or more messages has been added to a flash queue by the
``session.flash`` API, the ``session.peek_flash`` API can be used to "peek"
at that queue. Unlike ``session.pop_flash``, the queue is not popped from
flash storage.
+.. method:: peek_flash(queue='')
+
.. code-block:: python
:linenos:
diff --git a/docs/narr/introduction.rst b/docs/narr/introduction.rst
index 3ade3726c..c61ef21d4 100644
--- a/docs/narr/introduction.rst
+++ b/docs/narr/introduction.rst
@@ -63,7 +63,7 @@ A Sense of Fun
Minimalism
:app:`Pyramid` provides only the very basics: *URL to code
- mapping*, *templating*, *security*, and *resources*. There is not
+ mapping*, *templating*, *security*, and *assets*. There is not
much more to the framework than these pieces: you are expected to
provide the rest.
diff --git a/docs/narr/project.rst b/docs/narr/project.rst
index 36f2d6975..55a2711f3 100644
--- a/docs/narr/project.rst
+++ b/docs/narr/project.rst
@@ -256,6 +256,8 @@ create`` -generated project. Within a project generated by the
single: IPython
single: paster pshell
+.. _interactive_shell:
+
The Interactive Shell
---------------------
diff --git a/docs/narr/renderers.rst b/docs/narr/renderers.rst
index 3804fcf42..76e9562fa 100644
--- a/docs/narr/renderers.rst
+++ b/docs/narr/renderers.rst
@@ -22,6 +22,7 @@ response. For example:
from pyramid.response import Response
from pyramid.view import view_config
+ @view_config(renderer='json')
def hello_world(request):
return {'content':'Hello!'}
diff --git a/docs/narr/resources.rst b/docs/narr/resources.rst
index 3b9efe108..b892cf3cd 100644
--- a/docs/narr/resources.rst
+++ b/docs/narr/resources.rst
@@ -533,7 +533,7 @@ declares that the blog entry implements an :term:`interface`.
implements(IBlogEntry)
def __init__(self, title, body, author):
self.title = title
- self.body = body
+ self.body = body
self.author = author
self.created = datetime.datetime.now()
@@ -568,7 +568,7 @@ To do so, use the :func:`zope.interface.directlyProvides` function:
class BlogEntry(object):
def __init__(self, title, body, author):
self.title = title
- self.body = body
+ self.body = body
self.author = author
self.created = datetime.datetime.now()
@@ -596,7 +596,7 @@ the :func:`zope.interface.alsoProvides` function:
class BlogEntry(object):
def __init__(self, title, body, author):
self.title = title
- self.body = body
+ self.body = body
self.author = author
self.created = datetime.datetime.now()
diff --git a/docs/narr/urldispatch.rst b/docs/narr/urldispatch.rst
index 76eca454d..0d28a0e96 100644
--- a/docs/narr/urldispatch.rst
+++ b/docs/narr/urldispatch.rst
@@ -533,12 +533,12 @@ neither predicates nor view configuration information.
callables. Use custom predicates when no set of predefined predicates does
what you need. Custom predicates can be combined with predefined
predicates as necessary. Each custom predicate callable should accept two
- arguments: ``context`` and ``request`` and should return either ``True`` or
+ arguments: ``info`` and ``request`` and should return either ``True`` or
``False`` after doing arbitrary evaluation of the context resource and/or
the request. If all callables return ``True``, the associated route will
be considered viable for a given request. If any custom predicate returns
- ``False``, route matching continues. Note that the value ``context`` will
- always be ``None`` when passed to a custom route predicate.
+ ``False``, route matching continues. See :ref:`custom_route_predicates`
+ for more information.
**View-Related Arguments**
@@ -1231,6 +1231,44 @@ which you started the application from. For example:
See :ref:`environment_chapter` for more information about how, and where to
set these values.
+.. index::
+ pair: routes; printing
+ single: paster proutes
+
+Displaying All Application Routes
+---------------------------------
+
+You can use the ``paster proutes`` command in a terminal window to print a
+summary of routes related to your application. Much like the ``paster
+pshell`` command (see :ref:`interactive shell`), the ``paster proutes``
+command accepts two arguments. The first argument to ``proutes`` is the path
+to your application's ``.ini`` file. The second is the ``app`` section name
+inside the ``.ini`` file which points to your application.
+
+For example:
+
+.. code-block:: text
+ :linenos:
+
+ [chrism@thinko MyProject]$ ../bin/paster proutes development.ini MyProject
+ Name Pattern View
+ ---- ------- ----
+ home / <function my_view>
+ home2 / <function my_view>
+ another /another None
+ static/ static/*subpath <static_view object>
+ catchall /*subpath <function static_view>
+
+``paster proutes`` generates a table. The table has three columns: a Name
+name column, a Pattern column, and a View column. The items listed in the
+Name column are route names, the items listen in the Pattern column are route
+patterns, and the items listed in the View column are representations of the
+view callable that will be invoked when a request matches the associated
+route pattern. The view column may show ``None`` if no associated view
+callable could be found. If no routes are configured within your
+application, nothing will be printed to the console when ``paster proutes``
+is executed.
+
References
----------
diff --git a/docs/tutorials/cmf/actions.rst b/docs/tutorials/cmf/actions.rst
deleted file mode 100644
index a6e33fa59..000000000
--- a/docs/tutorials/cmf/actions.rst
+++ /dev/null
@@ -1,28 +0,0 @@
-.. _actions_chapter:
-
-=======
-Actions
-=======
-
-In CMF, the "actions tool" along with "action providers" create an extensible
-mechanism to show links in the CMF management UI that invoke a particular
-behavior or which show a particular template.
-
-:app:`Pyramid` itself has no such concept, and no package provides a direct
-replacement. Actions are such a generic concept that it's simple to
-reimplement action-like navigation in a different way within any given
-application. For example, a module-scope global dictionary which has keys
-that are action names, and values which are tuples of (permission, link).
-Take that concept and expand on it, and you'll have some passable actions
-tool replacement within a single application.
-
-The `pyramid_viewgroup <https://github.com/Pylons/pyramid_viewgroup/>`_
-package provides some functionality for creating "view groups". Each view in
-a viewgroup can provide some snippet of HTML (e.g. a single "tab"), and
-individual views (tabs) within the group which cannot be displayed to the
-user due to the user's lack of permissions will be omitted from the rendered
-output.
-
-The :term:`repoze.lemonade` package provides "list item" support that
-may be used to construct action lists.
-
diff --git a/docs/tutorials/cmf/catalog.rst b/docs/tutorials/cmf/catalog.rst
deleted file mode 100644
index d5e9534ae..000000000
--- a/docs/tutorials/cmf/catalog.rst
+++ /dev/null
@@ -1,73 +0,0 @@
-.. _catalog_chapter:
-
-=======
-Catalog
-=======
-
-The main feature of the CMF catalog is that it filters search results
-from the Zope 2 "catalog" based on the requesting user's ability to
-view a particular cataloged object.
-
-:app:`Pyramid` itself has no cataloging facility, but an addon
-package named :term:`repoze.catalog` offers similar functionality.
-
-Creating an Allowed Index
--------------------------
-
-In CMF, a catalog index named ``getAllowedRolesAndUsers`` along with
-application indexing code allows for filtered search results. It's
-reasonably easy to reproduce this pattern using some custom code.
-
-Creating The ``allowed`` Index
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Here's some code which creates an ``allowed`` index for use in a
-``repoze.catalog`` catalog::
-
- from pyramid.security import principals_allowed_by_permission
- from repoze.catalog.indexes.keyword import CatalogKeywordIndex
- from repoze.catalog.catalog import Catalog
-
- class Allowed:
- def __init__(self, permission):
- self.permission = permission
-
- def __call__(self, context, default):
- principals = principals_allowed_by_permission(context,
- self.permission)
- return principals
-
- def make_allowed_index(permission='View'):
- index = CatalogKeywordIndex(Allowed(permission))
- return index
-
- index = make_allowed_index()
- catalog = Catalog()
- catalog['allowed'] = index
-
-When you index an item, the allowed index will be populated with all
-the principal ids which have the 'View' permission.
-
-Using the ``allowed`` Index
-~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Here's how you might use the ``allowed`` index within a query::
-
- from pyramid.security import effective_principals
- principals = effective_principals(request)
- catalog.searchResults(allowed={'operator':'or', 'query':principals})
-
-The above query will return all document ids that the current user has
-the 'View' permission against. Add other indexes to the query to get
-a useful result.
-
-See the `repoze.catalog package
-<http://svn.repoze.org/repoze.catalog/trunk>`_ for more information.
-
-
-
-
-
-
-
-
diff --git a/docs/tutorials/cmf/content.rst b/docs/tutorials/cmf/content.rst
deleted file mode 100644
index 85e5b5fbc..000000000
--- a/docs/tutorials/cmf/content.rst
+++ /dev/null
@@ -1,67 +0,0 @@
-.. _content_types_chapter:
-
-=============
-Content Types
-=============
-
-In CMF, a content type is defined as a bag of settings (the type
-information, controlled within the "types tool"), as well as factory
-code which generates an instance of that content. It is possible to
-construct and enumerate content types using APIs defined on the types
-tool.
-
-:app:`Pyramid` itself has no such concept, but an addon package named
-:term:`repoze.lemonade` has a barebones replacement.
-
-Factory Type Information
-------------------------
-
-A factory type information object in CMF allows you to associate a
-title, a description, an internationalization domain, an icon, an
-initial view name, a factory, and a number of security settings with a
-type name. Each type information object knows how to manufacture
-content objects that match its type.
-
-:app:`Pyramid` certainly enforces none of these concepts in any
-particular way, but :term:`repoze.lemonade` does.
-
-``repoze.lemonade`` Content
-+++++++++++++++++++++++++++
-
-:term:`repoze.lemonade` provides a reasonably handy directive and set
-of helper functions which allow you to:
-
-#. Associate a interface with a factory function, making it into a
- "content type".
-
-#. Enumerate all interfaces associated with factory functions.
-
-.. note:: Using this pattern is often plain silly, as it's usually
- just as easy to actually import a class implementation and
- create an instance directly using its constructor. But it
- can be useful in cases where you want to address some set of
- constructors uniformly without doing direct imports in the
- code which performs the construction, or if you need to make
- content construction uniform across a diverse set of model
- types, or if you need to enumerate some set of information
- about "content" types. It's left as an exercise to the
- reader to determine under which circumstances using this
- pattern is an appropriate thing to do. Hint: not very
- often, unless you're doing the indirection solely to aid
- content-agnostic unit tests or if you need to get an
- enumerated subset of content type information to aid in UI
- generation. That said, this *is* a tutorial about how to
- get CMF-like features in :app:`Pyramid`, so we'll assume
- the pattern is useful to readers.
-
-See the `repoze.lemonade package
-<http://svn.repoze.org/repoze.lemonade/trunk>`_ for more information,
-particularly its documentation for "content".
-
-
-
-
-
-
-
-
diff --git a/docs/tutorials/cmf/index.rst b/docs/tutorials/cmf/index.rst
deleted file mode 100644
index 26aa336a9..000000000
--- a/docs/tutorials/cmf/index.rst
+++ /dev/null
@@ -1,38 +0,0 @@
-Converting an Existing Zope/CMF Application to :app:`Pyramid`
-================================================================
-
-The Zope `Content Management Framework
-<http://www.zope.org/Products/CMF/>`_ (aka CMF) is a layer on top of
-:term:`Zope` 2 that provides facilities for creating content-driven
-websites. It's reasonably easy to convert a modern Zope/CMF
-application to :app:`Pyramid`.
-
-The main difference between CMF and :app:`Pyramid` is that :app:`Pyramid`
-does not advertise itself as a system into which you can plug arbitrary
-"packages" that extend a system-supplied management user interface. You
-*could* build a CMF-like layer on top of :app:`Pyramid` but none currently
-exists. For those sorts of high-extensibility, highly-regularized-UI
-systems, CMF is still the better choice.
-
-:app:`Pyramid` (and other more lightweight systems) is often a
-better choice when you're building the a user interface from scratch,
-which often happens when the paradigms of some CMF-provided user
-interface don't match the requirements of an application very closely.
-Even so, a good number of developers tend to use CMF even when they do
-start an application for which they need to build a UI from scratch,
-because CMF happens to provide other helpful services, such as types,
-skins, and workflow; this tutorial is for those sorts of developers
-and projects.
-
-.. toctree::
- :maxdepth: 2
-
- content.rst
- catalog.rst
- skins.rst
- actions.rst
- workflow.rst
- missing.rst
-
-
-
diff --git a/docs/tutorials/cmf/missing.rst b/docs/tutorials/cmf/missing.rst
deleted file mode 100644
index 964e0ab04..000000000
--- a/docs/tutorials/cmf/missing.rst
+++ /dev/null
@@ -1,22 +0,0 @@
-Missing Comparisons
-===================
-
-We currently don't have any comparative Pyramid-vs-CMF information
-about the following concepts within this tutorial:
-
-- Templates
-
-- Forms
-
-- Membership
-
-- Discussions
-
-- Syndication
-
-- Dublincore
-
-Please ask on the `Pylons-devel maillist
-<http://groups.google.com/group/pylons-devel>`_ or on the `#pylons IRC
-channel <http://irc.freenode.net#pylons>`_ about these topics.
-
diff --git a/docs/tutorials/cmf/skins.rst b/docs/tutorials/cmf/skins.rst
deleted file mode 100644
index 676a076b3..000000000
--- a/docs/tutorials/cmf/skins.rst
+++ /dev/null
@@ -1,23 +0,0 @@
-.. _skins_chapter:
-
-=====
-Skins
-=====
-
-In CMF, a "skin layer" is defined as a collection of templates and
-code (Python scripts, DTML methods, etc) that can be activated and
-deactivated within a particular setup. A collection of active "skin
-layers" grouped in a particular order forms a "skin". "Add-on" CMF
-products often provide skin layers that are activated within a
-particular skin to provide the site with additional features.
-
-To override static resources using a "search path" much like a set of
-skin layers, :app:`Pyramid` provides the concept of
-:term:`resource` overrides. See :ref:`overriding_resources_section`
-for more information about resource overrides.
-
-While there is no analogue to a skin layer search path for locating
-Python code (as opposed to resources), :term:`view` code combined with
-differing :term:`predicate` arguments can provide a good deal of
-the same sort of behavior.
-
diff --git a/docs/tutorials/cmf/workflow.rst b/docs/tutorials/cmf/workflow.rst
deleted file mode 100644
index cc70d771a..000000000
--- a/docs/tutorials/cmf/workflow.rst
+++ /dev/null
@@ -1,14 +0,0 @@
-.. _workflow_chapter:
-
-========
-Workflow
-========
-
-In CMF, the "workflow tool" allows developers to design state machines
-which imply transition between content states.
-
-:app:`Pyramid` itself has no such concept, but the
-:term:`repoze.workflow` package provides a simple state machine
-implementation that can act as a barebones workflow tool. See its
-documentation for more information.
-
diff --git a/pyramid/chameleon_text.py b/pyramid/chameleon_text.py
index 32896b8e9..b687ecda9 100644
--- a/pyramid/chameleon_text.py
+++ b/pyramid/chameleon_text.py
@@ -4,39 +4,22 @@ from zope.deprecation import deprecated
from zope.interface import implements
try:
- from chameleon.core.template import TemplateFile
- TemplateFile # prevent pyflakes complaining about a redefinition below
+ from chameleon.zpt.template import PageTextTemplateFile
+ # prevent pyflakes complaining about a redefinition below
+ PageTextTemplateFile
except ImportError: # pragma: no cover
exc_class, exc, tb = sys.exc_info()
# Chameleon doesn't work on non-CPython platforms
- class TemplateFile(object):
+ class PageTextTemplateFile(object):
def __init__(self, *arg, **kw):
raise ImportError, exc, tb
-try:
- from chameleon.zpt.language import Parser
- Parser # prevent pyflakes complaining about a redefinition below
-except ImportError: # pragma: no cover
- # Chameleon doesn't work on non-CPython platforms
- class Parser(object):
- pass
-
from pyramid.interfaces import ITemplateRenderer
from pyramid.decorator import reify
from pyramid import renderers
from pyramid.path import caller_package
-class TextTemplateFile(TemplateFile):
- default_parser = Parser()
-
- def __init__(self, filename, parser=None, format='text', doctype=None,
- **kwargs):
- if parser is None:
- parser = self.default_parser
- super(TextTemplateFile, self).__init__(filename, parser, format,
- doctype, **kwargs)
-
def renderer_factory(info):
return renderers.template_renderer_factory(info, TextTemplateRenderer)
@@ -51,10 +34,10 @@ class TextTemplateRenderer(object):
if sys.platform.startswith('java'): # pragma: no cover
raise RuntimeError(
'Chameleon templates are not compatible with Jython')
- return TextTemplateFile(self.path,
- auto_reload=self.lookup.auto_reload,
- debug=self.lookup.debug,
- translate=self.lookup.translate)
+ return PageTextTemplateFile(self.path,
+ auto_reload=self.lookup.auto_reload,
+ debug=self.lookup.debug,
+ translate=self.lookup.translate)
def implementation(self):
return self.template
diff --git a/pyramid/config.py b/pyramid/config.py
index f6b4a2112..ee34adae1 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -45,6 +45,7 @@ from pyramid.interfaces import ITranslationDirectories
from pyramid.interfaces import ITraverser
from pyramid.interfaces import IView
from pyramid.interfaces import IViewClassifier
+from pyramid.interfaces import IViewMapperFactory
try:
from pyramid import chameleon_text
@@ -338,30 +339,34 @@ class Configurator(object):
def _split_spec(self, path_or_spec):
return resolve_asset_spec(path_or_spec, self.package_name)
+ # b/w compat
def _derive_view(self, view, permission=None, predicates=(),
attr=None, renderer=None, wrapper_viewname=None,
viewname=None, accept=None, order=MAX_ORDER,
phash=DEFAULT_PHASH):
- if renderer is None: # use default renderer if one exists
- default_renderer_factory = self.registry.queryUtility(
- IRendererFactory)
- if default_renderer_factory is not None:
- renderer = {'name':None, 'package':self.package}
view = self.maybe_dotted(view)
- authn_policy = self.registry.queryUtility(IAuthenticationPolicy)
- authz_policy = self.registry.queryUtility(IAuthorizationPolicy)
- settings = self.registry.settings
- logger = self.registry.queryUtility(IDebugLogger)
- mapped_view = _map_view(view, self.registry, attr, renderer)
- owrapped_view = _owrap_view(mapped_view, viewname, wrapper_viewname)
- secured_view = _secure_view(owrapped_view, permission,
- authn_policy, authz_policy)
- debug_view = _authdebug_view(secured_view, permission,
- authn_policy, authz_policy, settings,
- logger)
- predicated_view = _predicate_wrap(debug_view, predicates)
- derived_view = _attr_wrap(predicated_view, accept, order, phash)
- return derived_view
+ if isinstance(renderer, basestring):
+ renderer = RendererHelper(name=renderer, package=self.package,
+ registry = self.registry)
+ if renderer is None:
+ # use default renderer if one exists
+ if self.registry.queryUtility(IRendererFactory) is not None:
+ renderer = RendererHelper(name=None,
+ package=self.package,
+ registry=self.registry)
+ deriver = ViewDeriver(
+ registry=self.registry,
+ permission=permission,
+ predicates=predicates,
+ attr=attr,
+ renderer=renderer,
+ wrapper_viewname=wrapper_viewname,
+ viewname=viewname,
+ accept=accept,
+ order=order,
+ phash=phash,
+ package=self.package)
+ return deriver(view)
def _override(self, package, path, override_package, override_prefix,
PackageOverrides=PackageOverrides):
@@ -757,9 +762,6 @@ class Configurator(object):
a :term:`response` object. If a ``renderer`` argument is not
supplied, the user-supplied view must itself return a
:term:`response` object. """
-
- if renderer is not None and not isinstance(renderer, dict):
- renderer = {'name':renderer, 'package':self.package}
return self._derive_view(view, attr=attr, renderer=renderer)
@action_method
@@ -941,6 +943,20 @@ class Configurator(object):
pattern = route.pattern
+ action_decorator = getattr(handler, '__action_decorator__', None)
+ if action_decorator is not None:
+ if hasattr(action_decorator, 'im_self'):
+ # instance methods have an im_self == None
+ # classmethods have an im_self == cls
+ # staticmethods have no im_self
+ # instances have no im_self
+ if action_decorator.im_self is not handler:
+ raise ConfigurationError(
+ 'The "__action_decorator__" attribute of a handler '
+ 'must not be an instance method (must be a '
+ 'staticmethod, classmethod, function, or an instance '
+ 'which is a callable')
+
path_has_action = ':action' in pattern or '{action}' in pattern
if action and path_has_action:
@@ -970,7 +986,8 @@ class Configurator(object):
preds.append(ActionPredicate(action))
view_args['custom_predicates'] = preds
self.add_view(view=handler, attr=method_name,
- route_name=route_name, **view_args)
+ route_name=route_name,
+ decorator=action_decorator, **view_args)
else:
method_name = action
if method_name is None:
@@ -993,14 +1010,15 @@ class Configurator(object):
view_args = expose_config.copy()
del view_args['name']
self.add_view(view=handler, attr=meth_name,
- route_name=route_name, **view_args)
+ route_name=route_name,
+ decorator=action_decorator, **view_args)
# Now register the method itself
method = getattr(handler, method_name, None)
configs = getattr(method, '__exposed__', [{}])
for expose_config in configs:
self.add_view(view=handler, attr=action, route_name=route_name,
- **expose_config)
+ decorator=action_decorator, **expose_config)
return route
@@ -1010,7 +1028,7 @@ class Configurator(object):
request_param=None, containment=None, attr=None,
renderer=None, wrapper=None, xhr=False, accept=None,
header=None, path_info=None, custom_predicates=(),
- context=None):
+ context=None, decorator=None, view_mapper=None):
""" Add a :term:`view configuration` to the current
configuration state. Arguments to ``add_view`` are broken
down below into *predicate* arguments and *non-predicate*
@@ -1119,6 +1137,15 @@ class Configurator(object):
view is the same context and request of the inner view. If
this attribute is unspecified, no view wrapping is done.
+ decorator
+
+ A function which will be used to decorate the registered
+ :term:`view callable`. The decorator function will be
+ called with the view callable as a single argument, and it
+ must return a replacement view callable which accepts the
+ same arguments and returns the same type of values as the
+ original function.
+
Predicate Arguments
name
@@ -1255,6 +1282,18 @@ class Configurator(object):
the context and/or the request. If all callables return
``True``, the associated view callable will be considered
viable for a given request.
+
+ view_mapper
+
+ A class implementing the
+ :class:`pyramid.interfaces.IViewMapperFactory` interface, which
+ performs view argument and response mapping. By default it is
+ ``None``, which indicates that the view should use the default view
+ mapper. This plug-point is useful for Pyramid extension
+ developers, but it's not very useful for 'civilians' who are
+ just developing stock Pyramid applications. Pay no attention to
+ the man behind the curtain.
+
"""
view = self.maybe_dotted(view)
context = self.maybe_dotted(context)
@@ -1293,6 +1332,7 @@ class Configurator(object):
renderer=renderer, wrapper=wrapper, xhr=xhr, accept=accept,
header=header, path_info=path_info,
custom_predicates=custom_predicates, context=context,
+ view_mapper = view_mapper,
)
view_info = deferred_views.setdefault(route_name, [])
view_info.append(info)
@@ -1304,9 +1344,6 @@ class Configurator(object):
containment=containment, request_type=request_type,
custom=custom_predicates)
- if renderer is not None and not isinstance(renderer, dict):
- renderer = {'name':renderer, 'package':self.package}
-
if context is None:
context = for_
@@ -1316,16 +1353,35 @@ class Configurator(object):
if not IInterface.providedBy(r_context):
r_context = implementedBy(r_context)
- def register(permission=permission):
+ if isinstance(renderer, basestring):
+ renderer = RendererHelper(name=renderer, package=self.package,
+ registry = self.registry)
+
+ def register(permission=permission, renderer=renderer):
+ if renderer is None:
+ # use default renderer if one exists
+ if self.registry.queryUtility(IRendererFactory) is not None:
+ renderer = RendererHelper(name=None,
+ package=self.package,
+ registry=self.registry)
if permission is None:
# intent: will be None if no default permission is registered
permission = self.registry.queryUtility(IDefaultPermission)
# NO_PERMISSION_REQUIRED handled by _secure_view
- derived_view = self._derive_view(view, permission, predicates, attr,
- renderer, wrapper, name, accept,
- order, phash)
+ derived_view = ViewDeriver(registry=self.registry,
+ permission=permission,
+ predicates=predicates,
+ attr=attr,
+ renderer=renderer,
+ wrapper_viewname=wrapper,
+ viewname=name,
+ accept=accept,
+ order=order,
+ phash=phash,
+ decorator=decorator,
+ view_mapper=view_mapper)(view)
registered = self.registry.adapters.registered
@@ -1959,8 +2015,9 @@ class Configurator(object):
The ``wrapper`` argument should be the name of another view
which will wrap this view when rendered (see the ``add_view``
method's ``wrapper`` argument for a description)."""
- if renderer is not None and not isinstance(renderer, dict):
- renderer = {'name':renderer, 'package':self.package}
+ if isinstance(renderer, basestring):
+ renderer = RendererHelper(name=renderer, package=self.package,
+ registry = self.registry)
view = self._derive_view(view, attr=attr, renderer=renderer)
def bwcompat_view(context, request):
context = getattr(request, 'context', None)
@@ -1998,8 +2055,9 @@ class Configurator(object):
which will wrap this view when rendered (see the ``add_view``
method's ``wrapper`` argument for a description).
"""
- if renderer is not None and not isinstance(renderer, dict):
- renderer = {'name':renderer, 'package':self.package}
+ if isinstance(renderer, basestring):
+ renderer = RendererHelper(name=renderer, package=self.package,
+ registry=self.registry)
view = self._derive_view(view, attr=attr, renderer=renderer)
def bwcompat_view(context, request):
context = getattr(request, 'context', None)
@@ -2158,11 +2216,6 @@ class Configurator(object):
# same function once for each added translation directory,
# which does too much work, but has the same effect.
- def translator(msg):
- request = get_current_request()
- localizer = get_localizer(request)
- return localizer.translate(msg)
-
ctranslate = ChameleonTranslate(translator)
self.registry.registerUtility(ctranslate, IChameleonTranslate)
@@ -2633,307 +2686,330 @@ class MultiView(object):
continue
raise PredicateMismatch(self.name)
-def decorate_view(wrapped_view, original_view):
- if wrapped_view is original_view:
- return False
- wrapped_view.__module__ = original_view.__module__
- wrapped_view.__doc__ = original_view.__doc__
+def wraps_view(wrapped):
+ def inner(self, view):
+ wrapped_view = wrapped(self, view)
+ return preserve_view_attrs(view, wrapped_view)
+ return inner
+
+def preserve_view_attrs(view, wrapped_view):
+ if wrapped_view is view:
+ return view
+ wrapped_view.__module__ = view.__module__
+ wrapped_view.__doc__ = view.__doc__
try:
- wrapped_view.__name__ = original_view.__name__
+ wrapped_view.__name__ = view.__name__
except AttributeError:
- wrapped_view.__name__ = repr(original_view)
+ wrapped_view.__name__ = repr(view)
try:
- wrapped_view.__permitted__ = original_view.__permitted__
+ wrapped_view.__permitted__ = view.__permitted__
except AttributeError:
pass
try:
- wrapped_view.__call_permissive__ = original_view.__call_permissive__
+ wrapped_view.__call_permissive__ = view.__call_permissive__
except AttributeError:
pass
try:
- wrapped_view.__predicated__ = original_view.__predicated__
+ wrapped_view.__predicated__ = view.__predicated__
except AttributeError:
pass
try:
- wrapped_view.__accept__ = original_view.__accept__
+ wrapped_view.__accept__ = view.__accept__
except AttributeError:
pass
try:
- wrapped_view.__order__ = original_view.__order__
+ wrapped_view.__order__ = view.__order__
except AttributeError:
pass
- return True
-
-def requestonly(class_or_callable, attr=None):
- """ Return true of the class or callable accepts only a request argument,
- as opposed to something that accepts context, request """
- if attr is None:
- attr = '__call__'
- if inspect.isfunction(class_or_callable):
- fn = class_or_callable
- elif inspect.isclass(class_or_callable):
- try:
- fn = class_or_callable.__init__
- except AttributeError:
- return False
- else:
- try:
- fn = getattr(class_or_callable, attr)
- except AttributeError:
- return False
-
- try:
- argspec = inspect.getargspec(fn)
- except TypeError:
- return False
-
- args = argspec[0]
- defaults = argspec[3]
-
- if hasattr(fn, 'im_func'):
- # it's an instance method
- if not args:
- return False
- args = args[1:]
- if not args:
- return False
+ return wrapped_view
- if len(args) == 1:
- return True
+class ViewDeriver(object):
+ def __init__(self, **kw):
+ self.kw = kw
+ self.registry = kw['registry']
+ self.authn_policy = self.registry.queryUtility(
+ IAuthenticationPolicy)
+ self.authz_policy = self.registry.queryUtility(
+ IAuthorizationPolicy)
+ self.logger = self.registry.queryUtility(IDebugLogger)
+
+ def __call__(self, view):
+ mapper = self.kw.get('view_mapper')
+ if mapper is None:
+ mapper = DefaultViewMapper
+ view = mapper(**self.kw)(view)
+ return self.attr_wrapped_view(
+ self.predicated_view(
+ self.authdebug_view(
+ self.secured_view(
+ self.owrap_view(
+ view)))))
+
+ @wraps_view
+ def owrap_view(self, view):
+ wrapper_viewname = self.kw.get('wrapper_viewname')
+ viewname = self.kw.get('viewname')
+ if not wrapper_viewname:
+ return view
+ def _owrapped_view(context, request):
+ response = view(context, request)
+ request.wrapped_response = response
+ request.wrapped_body = response.body
+ request.wrapped_view = view
+ wrapped_response = render_view_to_response(context, request,
+ wrapper_viewname)
+ if wrapped_response is None:
+ raise ValueError(
+ 'No wrapper view named %r found when executing view '
+ 'named %r' % (wrapper_viewname, viewname))
+ return wrapped_response
+ return _owrapped_view
+
+ @wraps_view
+ def secured_view(self, view):
+ permission = self.kw.get('permission')
+ if permission == '__no_permission_required__':
+ # allow views registered within configurations that have a
+ # default permission to explicitly override the default
+ # permission, replacing it with no permission at all
+ permission = None
+
+ wrapped_view = view
+ if self.authn_policy and self.authz_policy and (permission is not None):
+ def _secured_view(context, request):
+ principals = self.authn_policy.effective_principals(request)
+ if self.authz_policy.permits(context, principals, permission):
+ return view(context, request)
+ msg = getattr(request, 'authdebug_message',
+ 'Unauthorized: %s failed permission check' % view)
+ raise Forbidden(msg)
+ _secured_view.__call_permissive__ = view
+ def _permitted(context, request):
+ principals = self.authn_policy.effective_principals(request)
+ return self.authz_policy.permits(context, principals,
+ permission)
+ _secured_view.__permitted__ = _permitted
+ wrapped_view = _secured_view
+
+ return wrapped_view
+
+ @wraps_view
+ def authdebug_view(self, view):
+ wrapped_view = view
+ settings = self.registry.settings
+ permission = self.kw.get('permission')
+ if settings and settings.get('debug_authorization', False):
+ def _authdebug_view(context, request):
+ view_name = getattr(request, 'view_name', None)
+
+ if self.authn_policy and self.authz_policy:
+ if permission is None:
+ msg = 'Allowed (no permission registered)'
+ else:
+ principals = self.authn_policy.effective_principals(
+ request)
+ msg = str(self.authz_policy.permits(context, principals,
+ permission))
+ else:
+ msg = 'Allowed (no authorization policy in use)'
+
+ view_name = getattr(request, 'view_name', None)
+ url = getattr(request, 'url', None)
+ msg = ('debug_authorization of url %s (view name %r against '
+ 'context %r): %s' % (url, view_name, context, msg))
+ self.logger and self.logger.debug(msg)
+ if request is not None:
+ request.authdebug_message = msg
+ return view(context, request)
- elif args[0] == 'request':
- if len(args) - len(defaults) == 1:
- return True
+ wrapped_view = _authdebug_view
- return False
+ return wrapped_view
-def is_response(ob):
- if ( hasattr(ob, 'app_iter') and hasattr(ob, 'headerlist') and
- hasattr(ob, 'status') ):
- return True
- return False
+ @wraps_view
+ def predicated_view(self, view):
+ predicates = self.kw.get('predicates', ())
+ if not predicates:
+ return view
+ def predicate_wrapper(context, request):
+ if all((predicate(context, request) for predicate in predicates)):
+ return view(context, request)
+ raise PredicateMismatch('predicate mismatch for view %s' % view)
+ def checker(context, request):
+ return all((predicate(context, request) for predicate in
+ predicates))
+ predicate_wrapper.__predicated__ = checker
+ return predicate_wrapper
+
+ @wraps_view
+ def attr_wrapped_view(self, view):
+ kw = self.kw
+ accept, order, phash = (kw.get('accept', None),
+ kw.get('order', MAX_ORDER),
+ kw.get('phash', DEFAULT_PHASH))
+ # this is a little silly but we don't want to decorate the original
+ # function with attributes that indicate accept, order, and phash,
+ # so we use a wrapper
+ if ( (accept is None) and (order == MAX_ORDER) and
+ (phash == DEFAULT_PHASH) ):
+ return view # defaults
+ def attr_view(context, request):
+ return view(context, request)
+ attr_view.__accept__ = accept
+ attr_view.__order__ = order
+ attr_view.__phash__ = phash
+ return attr_view
+
+class DefaultViewMapper(object):
+ implements(IViewMapperFactory)
+ def __init__(self, **kw):
+ self.renderer = kw.get('renderer')
+ self.attr = kw.get('attr')
+ self.decorator = kw.get('decorator')
+
+ def __call__(self, view):
+ decorator = self.decorator
+ if inspect.isclass(view):
+ view = preserve_view_attrs(view, self.map_class(view))
+ else:
+ view = preserve_view_attrs(view, self.map_nonclass(view))
+ if decorator is not None:
+ view = preserve_view_attrs(view, decorator(view))
+ return view
-def _map_view(view, registry, attr=None, renderer=None):
- wrapped_view = view
-
- helper = None
-
- if renderer is not None:
- helper = RendererHelper(renderer['name'],
- package=renderer['package'],
- registry=registry)
-
- if inspect.isclass(view):
- # If the object we've located is a class, turn it into a
- # function that operates like a Zope view (when it's invoked,
- # construct an instance using 'context' and 'request' as
- # position arguments, then immediately invoke the __call__
- # method of the instance with no arguments; __call__ should
- # return an IResponse).
- if requestonly(view, attr):
- # its __init__ accepts only a single request argument,
- # instead of both context and request
- def _class_requestonly_view(context, request):
- inst = view(request)
- if attr is None:
- response = inst()
- else:
- response = getattr(inst, attr)()
- if helper is not None:
- if not is_response(response):
- system = {
- 'view':inst,
- 'renderer_name':renderer['name'], # b/c
- 'renderer_info':renderer,
- 'context':context,
- 'request':request
- }
- response = helper.render_to_response(response, system,
- request=request)
- return response
- wrapped_view = _class_requestonly_view
+ def map_class(self, view):
+ ronly = self.requestonly(view)
+ if ronly:
+ mapped_view = self._map_class_requestonly(view)
else:
- # its __init__ accepts both context and request
- def _class_view(context, request):
- inst = view(context, request)
- if attr is None:
- response = inst()
- else:
- response = getattr(inst, attr)()
- if helper is not None:
- if not is_response(response):
- system = {'view':inst,
- 'renderer_name':renderer['name'], # b/c
- 'renderer_info':renderer,
- 'context':context,
- 'request':request
- }
- response = helper.render_to_response(response, system,
- request=request)
- return response
- wrapped_view = _class_view
-
- elif requestonly(view, attr):
- # its __call__ accepts only a single request argument,
- # instead of both context and request
+ mapped_view = self._map_class_native(view)
+ return mapped_view
+
+ def map_nonclass(self, view):
+ # We do more work here than appears necessary to avoid wrapping the
+ # view unless it actually requires wrapping (to avoid function call
+ # overhead).
+ mapped_view = view
+ ronly = self.requestonly(view)
+ if ronly:
+ mapped_view = self._map_nonclass_requestonly(view)
+ elif self.attr:
+ mapped_view = self._map_nonclass_attr(view)
+ elif self.renderer is not None:
+ mapped_view = self._map_nonclass_rendered(view)
+ return mapped_view
+
+ def _map_class_requestonly(self, view):
+ # its a class that has an __init__ which only accepts request
+ attr = self.attr
+ def _class_requestonly_view(context, request):
+ inst = view(request)
+ if attr is None:
+ response = inst()
+ else:
+ response = getattr(inst, attr)()
+ if self.renderer is not None and not is_response(response):
+ response = self.renderer.render_view(request, response, view,
+ context)
+ return response
+ return _class_requestonly_view
+
+ def _map_class_native(self, view):
+ # its a class that has an __init__ which accepts both context and
+ # request
+ attr = self.attr
+ def _class_view(context, request):
+ inst = view(context, request)
+ if attr is None:
+ response = inst()
+ else:
+ response = getattr(inst, attr)()
+ if self.renderer is not None and not is_response(response):
+ response = self.renderer.render_view(request, response, view,
+ context)
+ return response
+ return _class_view
+
+ def _map_nonclass_requestonly(self, view):
+ # its a function that has a __call__ which accepts only a single
+ # request argument
+ attr = self.attr
def _requestonly_view(context, request):
if attr is None:
response = view(request)
else:
response = getattr(view, attr)(request)
-
- if helper is not None:
- if not is_response(response):
- system = {
- 'view':view,
- 'renderer_name':renderer['name'],
- 'renderer_info':renderer,
- 'context':context,
- 'request':request
- }
- response = helper.render_to_response(response, system,
- request=request)
+ if self.renderer is not None and not is_response(response):
+ response = self.renderer.render_view(request, response, view,
+ context)
return response
- wrapped_view = _requestonly_view
+ return _requestonly_view
- elif attr:
+ def _map_nonclass_attr(self, view):
+ # its a function that has a __call__ which accepts both context and
+ # request, but still has an attr
+ attr = self.attr
def _attr_view(context, request):
response = getattr(view, attr)(context, request)
- if helper is not None:
- if not is_response(response):
- system = {
- 'view':view,
- 'renderer_name':renderer['name'],
- 'renderer_info':renderer,
- 'context':context,
- 'request':request
- }
- response = helper.render_to_response(response, system,
- request=request)
+ if self.renderer is not None and not is_response(response):
+ response = self.renderer.render_view(request, response, view,
+ context)
return response
- wrapped_view = _attr_view
+ return _attr_view
- elif helper is not None:
+ def _map_nonclass_rendered(self, view):
+ # it's a function that has a __call__ that accepts both context and
+ # request, but requires rendering
def _rendered_view(context, request):
response = view(context, request)
- if not is_response(response):
- system = {
- 'view':view,
- 'renderer_name':renderer['name'], # b/c
- 'renderer_info':renderer,
- 'context':context,
- 'request':request
- }
- response = helper.render_to_response(response, system,
- request=request)
+ if self.renderer is not None and not is_response(response):
+ response = self.renderer.render_view(request, response, view,
+ context)
return response
- wrapped_view = _rendered_view
-
- decorate_view(wrapped_view, view)
- return wrapped_view
+ return _rendered_view
+
+ def requestonly(self, view):
+ attr = self.attr
+ if attr is None:
+ attr = '__call__'
+ if inspect.isfunction(view):
+ fn = view
+ elif inspect.isclass(view):
+ try:
+ fn = view.__init__
+ except AttributeError:
+ return False
+ else:
+ try:
+ fn = getattr(view, attr)
+ except AttributeError:
+ return False
-def _owrap_view(view, viewname, wrapper_viewname):
- if not wrapper_viewname:
- return view
- def _owrapped_view(context, request):
- response = view(context, request)
- request.wrapped_response = response
- request.wrapped_body = response.body
- request.wrapped_view = view
- wrapped_response = render_view_to_response(context, request,
- wrapper_viewname)
- if wrapped_response is None:
- raise ValueError(
- 'No wrapper view named %r found when executing view '
- 'named %r' % (wrapper_viewname, viewname))
- return wrapped_response
- decorate_view(_owrapped_view, view)
- return _owrapped_view
-
-def _predicate_wrap(view, predicates):
- if not predicates:
- return view
- def predicate_wrapper(context, request):
- if all((predicate(context, request) for predicate in predicates)):
- return view(context, request)
- raise PredicateMismatch('predicate mismatch for view %s' % view)
- def checker(context, request):
- return all((predicate(context, request) for predicate in
- predicates))
- predicate_wrapper.__predicated__ = checker
- decorate_view(predicate_wrapper, view)
- return predicate_wrapper
-
-def _secure_view(view, permission, authn_policy, authz_policy):
- if permission == '__no_permission_required__':
- # allow views registered within configurations that have a
- # default permission to explicitly override the default
- # permission, replacing it with no permission at all
- permission = None
-
- wrapped_view = view
- if authn_policy and authz_policy and (permission is not None):
- def _secured_view(context, request):
- principals = authn_policy.effective_principals(request)
- if authz_policy.permits(context, principals, permission):
- return view(context, request)
- msg = getattr(request, 'authdebug_message',
- 'Unauthorized: %s failed permission check' % view)
- raise Forbidden(msg)
- _secured_view.__call_permissive__ = view
- def _permitted(context, request):
- principals = authn_policy.effective_principals(request)
- return authz_policy.permits(context, principals, permission)
- _secured_view.__permitted__ = _permitted
- wrapped_view = _secured_view
- decorate_view(wrapped_view, view)
+ try:
+ argspec = inspect.getargspec(fn)
+ except TypeError:
+ return False
- return wrapped_view
+ args = argspec[0]
+ defaults = argspec[3]
-def _authdebug_view(view, permission, authn_policy, authz_policy, settings,
- logger):
- wrapped_view = view
- if settings and settings.get('debug_authorization', False):
- def _authdebug_view(context, request):
- view_name = getattr(request, 'view_name', None)
+ if hasattr(fn, 'im_func'):
+ # it's an instance method
+ if not args:
+ return False
+ args = args[1:]
+ if not args:
+ return False
- if authn_policy and authz_policy:
- if permission is None:
- msg = 'Allowed (no permission registered)'
- else:
- principals = authn_policy.effective_principals(request)
- msg = str(authz_policy.permits(context, principals,
- permission))
- else:
- msg = 'Allowed (no authorization policy in use)'
-
- view_name = getattr(request, 'view_name', None)
- url = getattr(request, 'url', None)
- msg = ('debug_authorization of url %s (view name %r against '
- 'context %r): %s' % (url, view_name, context, msg))
- logger and logger.debug(msg)
- if request is not None:
- request.authdebug_message = msg
- return view(context, request)
+ if len(args) == 1:
+ return True
- wrapped_view = _authdebug_view
- decorate_view(wrapped_view, view)
+ elif args[0] == 'request':
+ if len(args) - len(defaults) == 1:
+ return True
- return wrapped_view
+ return False
-def _attr_wrap(view, accept, order, phash):
- # this is a little silly but we don't want to decorate the original
- # function with attributes that indicate accept, order, and phash,
- # so we use a wrapper
- if (accept is None) and (order == MAX_ORDER) and (phash == DEFAULT_PHASH):
- return view # defaults
- def attr_view(context, request):
- return view(context, request)
- attr_view.__accept__ = accept
- attr_view.__order__ = order
- attr_view.__phash__ = phash
- decorate_view(attr_view, view)
- return attr_view
def isexception(o):
if IInterface.providedBy(o):
@@ -2984,3 +3060,25 @@ class PyramidConfigurationMachine(ConfigurationMachine):
self._seen_files.add(spec)
return True
+def translator(msg):
+ request = get_current_request()
+ localizer = get_localizer(request)
+ return localizer.translate(msg)
+
+# b/c
+def _map_view(view, registry, attr=None, renderer=None):
+ return DefaultViewMapper(registry=registry, attr=attr,
+ renderer=renderer)(view)
+
+# b/c
+def requestonly(view, attr=None):
+ """ Return true of the class or callable accepts only a request argument,
+ as opposed to something that accepts context, request """
+ return DefaultViewMapper(attr=attr).requestonly(view)
+
+def is_response(ob):
+ if ( hasattr(ob, 'app_iter') and hasattr(ob, 'headerlist') and
+ hasattr(ob, 'status') ):
+ return True
+ return False
+
diff --git a/pyramid/httpexceptions.py b/pyramid/httpexceptions.py
index 6d05f9475..f56910b53 100644
--- a/pyramid/httpexceptions.py
+++ b/pyramid/httpexceptions.py
@@ -3,6 +3,7 @@ from webob.exc import status_map
# Parent classes
from webob.exc import HTTPException
+from webob.exc import WSGIHTTPException
from webob.exc import HTTPOk
from webob.exc import HTTPRedirection
from webob.exc import HTTPError
diff --git a/pyramid/interfaces.py b/pyramid/interfaces.py
index 32359ca94..10a324b28 100644
--- a/pyramid/interfaces.py
+++ b/pyramid/interfaces.py
@@ -120,6 +120,27 @@ class ITemplateRenderer(IRenderer):
accepts arbitrary keyword arguments and returns a string or
unicode object """
+class IViewMapper(Interface):
+ def __call__(self, object):
+ """ Provided with an arbitrary object (a function, class, or
+ instance), returns a callable with the call signature ``(context,
+ request)``. The callable returned should itself return a Response
+ object. An IViewMapper is returned by
+ :class:`pyramid.interfaces.IViewMapperFactory`."""
+
+class IViewMapperFactory(Interface):
+ def __call__(self, **kw):
+ """
+ Return an object which implements
+ :class:`pyramid.interfaces.IViewMapper`. ``kw`` will be a dictionary
+ containing view-specific arguments, such as ``permission``,
+ ``predicates``, ``attr``, ``renderer``, and other items. An
+ IViewMapperFactory is used by
+ :meth:`pyramid.config.Configurator.add_view` to provide a plugpoint
+ to extension developers who want to modify potential view callable
+ invocation signatures and response values.
+ """
+
# internal interfaces
class IRequest(Interface):
diff --git a/pyramid/paster.py b/pyramid/paster.py
index c8bc36e80..5efbae51c 100644
--- a/pyramid/paster.py
+++ b/pyramid/paster.py
@@ -63,7 +63,22 @@ def get_app(config_file, name, loadapp=loadapp):
return app
_marker = object()
-class PShellCommand(Command):
+
+class PCommand(Command):
+ get_app = staticmethod(get_app) # hook point
+ get_root = staticmethod(get_root) # hook point
+ group_name = 'pyramid'
+ interact = (interact,) # for testing
+ loadapp = (loadapp,) # for testing
+ verbose = 3
+
+ def __init__(self, *arg, **kw):
+ # needs to be in constructor to support Jython (used to be at class
+ # scope as ``usage = '\n' + __doc__``.
+ self.usage = '\n' + self.__doc__
+ Command.__init__(self, *arg, **kw)
+
+class PShellCommand(PCommand):
"""Open an interactive shell with a :app:`Pyramid` app loaded.
This command accepts two positional arguments:
@@ -88,7 +103,6 @@ class PShellCommand(Command):
min_args = 2
max_args = 2
- group_name = 'pyramid'
parser = Command.standard_parser(simulate=True)
parser.add_option('-d', '--disable-ipython',
@@ -96,18 +110,6 @@ class PShellCommand(Command):
dest='disable_ipython',
help="Don't use IPython even if it is available")
- interact = (interact,) # for testing
- loadapp = (loadapp,) # for testing
- get_app = staticmethod(get_app) # hook point
- get_root = staticmethod(get_root) # hook point
- verbose = 3
-
- def __init__(self, *arg, **kw):
- # needs to be in constructor to support Jython (used to be at class
- # scope as ``usage = '\n' + __doc__``.
- self.usage = '\n' + self.__doc__
- Command.__init__(self, *arg, **kw)
-
def command(self, IPShell=_marker):
if IPShell is _marker:
try: #pragma no cover
@@ -136,3 +138,73 @@ class PShellCommand(Command):
closer()
BFGShellCommand = PShellCommand # b/w compat forever
+
+class PRoutesCommand(PCommand):
+ """Print all URL dispatch routes used by a Pyramid application in the
+ order in which they are evaluated. Each route includes the name of the
+ route, the pattern of the route, and the view callable which will be
+ invoked when the route is matched.
+
+ This command accepts two positional arguments:
+
+ ``config_file`` -- specifies the PasteDeploy config file to use
+ for the interactive shell.
+
+ ``section_name`` -- specifies the section name in the PasteDeploy
+ config file that represents the application.
+
+ Example::
+
+ $ paster proutes myapp.ini main
+
+ .. note:: You should use a ``section_name`` that refers to the
+ actual ``app`` section in the config file that points at
+ your Pyramid app without any middleware wrapping, or this
+ command will almost certainly fail.
+ """
+ summary = "Print all URL dispatch routes related to a Pyramid application"
+ min_args = 2
+ max_args = 2
+ stdout = sys.stdout
+
+ parser = Command.standard_parser(simulate=True)
+
+ def _get_mapper(self, app):
+ from pyramid.config import Configurator
+ registry = app.registry
+ config = Configurator(registry = registry)
+ return config.get_routes_mapper()
+
+ def out(self, msg): # pragma: no cover
+ print msg
+
+ def command(self):
+ from pyramid.request import Request
+ from pyramid.interfaces import IRouteRequest
+ from pyramid.interfaces import IViewClassifier
+ from pyramid.interfaces import IView
+ from zope.interface import Interface
+ from zope.interface import providedBy
+ config_file, section_name = self.args
+ app = self.get_app(config_file, section_name, loadapp=self.loadapp[0])
+ registry = app.registry
+ mapper = self._get_mapper(app)
+ if mapper is not None:
+ routes = mapper.get_routes()
+ fmt = '%-15s %-30s %-25s'
+ if not routes:
+ return
+ self.out(fmt % ('Name', 'Pattern', 'View'))
+ self.out(
+ fmt % ('-'*len('Name'), '-'*len('Pattern'), '-'*len('View')))
+ for route in routes:
+ request_iface = registry.queryUtility(IRouteRequest,
+ name=route.name)
+ view_callable = None
+ if (request_iface is None) or (route.factory is not None):
+ self.out(fmt % (route.name, route.pattern, '<unknown>'))
+ else:
+ view_callable = registry.adapters.lookup(
+ (IViewClassifier, request_iface, Interface),
+ IView, name='', default=None)
+ self.out(fmt % (route.name, route.pattern, view_callable))
diff --git a/pyramid/renderers.py b/pyramid/renderers.py
index c7fe86452..2e0514b01 100644
--- a/pyramid/renderers.py
+++ b/pyramid/renderers.py
@@ -282,6 +282,18 @@ class RendererHelper(object):
def get_renderer(self):
return self.renderer
+ def render_view(self, request, response, view, context):
+ system = {
+ 'view':view,
+ 'renderer_name':self.name, # b/c
+ 'renderer_info':{'name':self.name, 'package':self.package},
+ 'context':context,
+ 'request':request
+ }
+ return self.render_to_response(response, system,
+ request=request)
+
+
def render(self, value, system_values, request=None):
renderer = self.renderer
if system_values is None:
diff --git a/pyramid/request.py b/pyramid/request.py
index 74418f1bb..475df744a 100644
--- a/pyramid/request.py
+++ b/pyramid/request.py
@@ -24,7 +24,7 @@ class Request(WebobRequest):
argument.
The documentation below (save for the ``add_response_callback`` and
- ''add_finished_callback`` methods, which are defined in this subclass
+ ``add_finished_callback`` methods, which are defined in this subclass
itself, and the attributes ``context``, ``registry``, ``root``,
``subpath``, ``traversed``, ``view_name``, ``virtual_root`` , and
``virtual_root_path``, each of which is added to the request by the
diff --git a/pyramid/tests/test_chameleon_text.py b/pyramid/tests/test_chameleon_text.py
index 79bc7984c..789c78bbe 100644
--- a/pyramid/tests/test_chameleon_text.py
+++ b/pyramid/tests/test_chameleon_text.py
@@ -1,23 +1,16 @@
import unittest
-from pyramid.testing import cleanUp
from pyramid.testing import skip_on
from pyramid import testing
class Base:
def setUp(self):
- cleanUp()
- import os
- try:
- # avoid spew from chameleon logger?
- os.unlink(self._getTemplatePath('minimal.txt.py'))
- except:
- pass
+ self.config = testing.setUp()
from zope.deprecation import __show__
__show__.off()
def tearDown(self):
- cleanUp()
+ testing.tearDown()
from zope.deprecation import __show__
__show__.on()
@@ -27,22 +20,10 @@ class Base:
return os.path.join(here, 'fixtures', name)
def _registerUtility(self, utility, iface, name=''):
- from pyramid.threadlocal import get_current_registry
- reg = get_current_registry()
+ reg = self.config.registry
reg.registerUtility(utility, iface, name=name)
- return reg
-
class TextTemplateRendererTests(Base, unittest.TestCase):
- def setUp(self):
- from pyramid.registry import Registry
- registry = Registry()
- self.config = testing.setUp(registry=registry)
- self.config.begin()
-
- def tearDown(self):
- self.config.end()
-
def _getTargetClass(self):
from pyramid.chameleon_text import TextTemplateRenderer
return TextTemplateRenderer
diff --git a/pyramid/tests/test_chameleon_zpt.py b/pyramid/tests/test_chameleon_zpt.py
index 4601c2f12..4fceb809c 100644
--- a/pyramid/tests/test_chameleon_zpt.py
+++ b/pyramid/tests/test_chameleon_zpt.py
@@ -1,17 +1,16 @@
import unittest
-from pyramid.testing import cleanUp
from pyramid.testing import skip_on
from pyramid import testing
class Base(object):
def setUp(self):
- cleanUp()
+ self.config = testing.setUp()
from zope.deprecation import __show__
__show__.off()
def tearDown(self):
- cleanUp()
+ testing.tearDown()
from zope.deprecation import __show__
__show__.on()
@@ -21,21 +20,11 @@ class Base(object):
return os.path.join(here, 'fixtures', name)
def _registerUtility(self, utility, iface, name=''):
- from pyramid.threadlocal import get_current_registry
- reg = get_current_registry()
+ reg = self.config.registry
reg.registerUtility(utility, iface, name=name)
return reg
class ZPTTemplateRendererTests(Base, unittest.TestCase):
- def setUp(self):
- from pyramid.registry import Registry
- registry = Registry()
- self.config = testing.setUp(registry=registry)
- self.config.begin()
-
- def tearDown(self):
- self.config.end()
-
def _getTargetClass(self):
from pyramid.chameleon_zpt import ZPTTemplateRenderer
return ZPTTemplateRenderer
diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py
index 84e8289be..b2fa0e329 100644
--- a/pyramid/tests/test_config.py
+++ b/pyramid/tests/test_config.py
@@ -739,6 +739,22 @@ class ConfiguratorTests(unittest.TestCase):
result = wrapper(None, None)
self.assertEqual(result, 'OK')
+ def test_add_view_with_decorator(self):
+ def view(request):
+ """ ABC """
+ return 'OK'
+ def view_wrapper(fn):
+ def inner(context, request):
+ return fn(context, request)
+ return inner
+ config = self._makeOne(autocommit=True)
+ config.add_view(view=view, decorator=view_wrapper)
+ wrapper = self._getViewCallable(config)
+ self.failIf(wrapper is view)
+ self.assertEqual(wrapper.__doc__, view.__doc__)
+ result = wrapper(None, None)
+ self.assertEqual(result, 'OK')
+
def test_add_view_as_instance(self):
class AView:
def __call__(self, context, request):
@@ -1410,6 +1426,29 @@ class ConfiguratorTests(unittest.TestCase):
self.assertEqual(result.name, fixture)
self.assertEqual(result.settings, settings)
+ def test_add_view_with_default_renderer(self):
+ import pyramid.tests
+ from pyramid.interfaces import ISettings
+ class view(object):
+ def __init__(self, context, request):
+ self.request = request
+ self.context = context
+
+ def __call__(self):
+ return {'a':'1'}
+ config = self._makeOne(autocommit=True)
+ class moo(object):
+ def __init__(self, *arg, **kw):
+ pass
+ def __call__(self, *arg, **kw):
+ return 'moo'
+ config.add_renderer(None, moo)
+ config.add_view(view=view)
+ wrapper = self._getViewCallable(config)
+ request = self._makeRequest(config)
+ result = wrapper(None, request)
+ self.assertEqual(result.body, 'moo')
+
def test_add_view_with_template_renderer_no_callable(self):
import pyramid.tests
from pyramid.interfaces import ISettings
@@ -1964,6 +2003,33 @@ class ConfiguratorTests(unittest.TestCase):
self.assertEqual(view['attr'], 'action')
self.assertEqual(view['view'], MyView)
+ def test_add_handler_with_action_decorator(self):
+ config = self._makeOne(autocommit=True)
+ views = []
+ def dummy_add_view(**kw):
+ views.append(kw)
+ config.add_view = dummy_add_view
+ class MyHandler(object):
+ @classmethod
+ def __action_decorator__(cls, fn): # pragma: no cover
+ return fn
+ def action(self): # pragma: no cover
+ return 'response'
+ config.add_handler('name', '/{action}', MyHandler)
+ self.assertEqual(len(views), 1)
+ self.assertEqual(views[0]['decorator'], MyHandler.__action_decorator__)
+
+ def test_add_handler_with_action_decorator_fail_on_instancemethod(self):
+ config = self._makeOne(autocommit=True)
+ class MyHandler(object):
+ def __action_decorator__(self, fn): # pragma: no cover
+ return fn
+ def action(self): # pragma: no cover
+ return 'response'
+ from pyramid.exceptions import ConfigurationError
+ self.assertRaises(ConfigurationError, config.add_handler,
+ 'name', '/{action}', MyHandler)
+
def test_add_handler_doesnt_mutate_expose_dict(self):
config = self._makeOne(autocommit=True)
views = []
@@ -2338,7 +2404,7 @@ class ConfiguratorTests(unittest.TestCase):
config.add_route('name', '/pattern', view_attr='abc')
except ConfigurationError:
pass
- else:
+ else: # pragma: no cover
raise AssertionError
def test_add_route_no_view_with_view_context(self):
@@ -2348,7 +2414,7 @@ class ConfiguratorTests(unittest.TestCase):
config.add_route('name', '/pattern', view_context=DummyContext)
except ConfigurationError:
pass
- else:
+ else: # pragma: no cover
raise AssertionError
def test_add_route_no_view_with_view_permission(self):
@@ -2358,7 +2424,7 @@ class ConfiguratorTests(unittest.TestCase):
config.add_route('name', '/pattern', view_permission='edit')
except ConfigurationError:
pass
- else:
+ else: # pragma: no cover
raise AssertionError
def test_add_route_no_view_with_view_renderer(self):
@@ -2368,7 +2434,7 @@ class ConfiguratorTests(unittest.TestCase):
config.add_route('name', '/pattern', view_renderer='json')
except ConfigurationError:
pass
- else:
+ else: # pragma: no cover
raise AssertionError
def test__override_not_yet_registered(self):
@@ -3504,16 +3570,17 @@ class Test__map_view(unittest.TestCase):
def _registerRenderer(self, typ='.txt'):
from pyramid.interfaces import IRendererFactory
from pyramid.interfaces import ITemplateRenderer
+ from pyramid.renderers import RendererHelper
from zope.interface import implements
- class Renderer:
+ class DummyRenderer:
implements(ITemplateRenderer)
- spec = 'abc' + typ
def __init__(self, path):
self.__class__.path = path
def __call__(self, *arg):
return 'Hello!'
- self.registry.registerUtility(Renderer, IRendererFactory, name=typ)
- return Renderer
+ self.registry.registerUtility(DummyRenderer, IRendererFactory, name=typ)
+ renderer = RendererHelper(name='abc' + typ, registry=self.registry)
+ return renderer
def _makeRequest(self):
request = DummyRequest()
@@ -3541,8 +3608,7 @@ class Test__map_view(unittest.TestCase):
def test__map_view_as_function_with_attr_and_renderer(self):
renderer = self._registerRenderer()
view = lambda *arg: 'OK'
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, attr='__name__', renderer=info)
+ result = self._callFUT(view, attr='__name__', renderer=renderer)
self.failIf(result is view)
self.assertRaises(TypeError, result, None, None)
@@ -3600,8 +3666,7 @@ class Test__map_view(unittest.TestCase):
pass
def index(self):
return {'a':'1'}
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, attr='index', renderer=info)
+ result = self._callFUT(view, attr='index', renderer=renderer)
self.failIf(result is view)
self.assertEqual(view.__module__, result.__module__)
self.assertEqual(view.__doc__, result.__doc__)
@@ -3642,8 +3707,7 @@ class Test__map_view(unittest.TestCase):
pass
def index(self):
return {'a':'1'}
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, attr='index', renderer=info)
+ result = self._callFUT(view, attr='index', renderer=renderer)
self.failIf(result is view)
self.assertEqual(view.__module__, result.__module__)
self.assertEqual(view.__doc__, result.__doc__)
@@ -3684,8 +3748,7 @@ class Test__map_view(unittest.TestCase):
pass
def index(self):
return {'a':'1'}
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, attr='index', renderer=info)
+ result = self._callFUT(view, attr='index', renderer=renderer)
self.failIf(result is view)
self.assertEqual(view.__module__, result.__module__)
self.assertEqual(view.__doc__, result.__doc__)
@@ -3726,8 +3789,7 @@ class Test__map_view(unittest.TestCase):
pass
def index(self):
return {'a':'1'}
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, attr='index', renderer=info)
+ result = self._callFUT(view, attr='index', renderer=renderer)
self.failIf(result is view)
self.assertEqual(view.__module__, result.__module__)
self.assertEqual(view.__doc__, result.__doc__)
@@ -3759,8 +3821,7 @@ class Test__map_view(unittest.TestCase):
def index(self, context, request):
return {'a':'1'}
view = View()
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, attr='index', renderer=info)
+ result = self._callFUT(view, attr='index', renderer=renderer)
self.failIf(result is view)
request = self._makeRequest()
self.assertEqual(result(None, request).body, 'Hello!')
@@ -3795,8 +3856,7 @@ class Test__map_view(unittest.TestCase):
def index(self, request):
return {'a':'1'}
view = View()
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, attr='index', renderer=info)
+ result = self._callFUT(view, attr='index', renderer=renderer)
self.failIf(result is view)
self.assertEqual(view.__module__, result.__module__)
self.assertEqual(view.__doc__, result.__doc__)
@@ -3808,8 +3868,7 @@ class Test__map_view(unittest.TestCase):
renderer = self._registerRenderer()
def view(context, request):
return {'a':'1'}
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, renderer=info)
+ result = self._callFUT(view, renderer=renderer)
self.failIf(result is view)
self.assertEqual(view.__module__, result.__module__)
self.assertEqual(view.__doc__, result.__doc__)
@@ -3820,24 +3879,25 @@ class Test__map_view(unittest.TestCase):
renderer = self._registerRenderer()
def view(context, request):
return {'a':'1'}
- info = {'name':renderer.spec, 'package':None}
- result = self._callFUT(view, renderer=info)
+ result = self._callFUT(view, renderer=renderer)
self.failIf(result is view)
self.assertEqual(view.__module__, result.__module__)
self.assertEqual(view.__doc__, result.__doc__)
request = self._makeRequest()
self.assertEqual(result(None, request).body, 'Hello!')
-class Test_decorate_view(unittest.TestCase):
- def _callFUT(self, wrapped, original):
- from pyramid.config import decorate_view
- return decorate_view(wrapped, original)
+class Test_wraps_view(unittest.TestCase):
+ def _callFUT(self, fn, view):
+ from pyramid.config import wraps_view
+ return wraps_view(fn)(None, view)
def test_it_same(self):
def view(context, request):
""" """
- result = self._callFUT(view, view)
- self.assertEqual(result, False)
+ def afunc(self, view):
+ return view
+ result = self._callFUT(afunc, view)
+ self.failUnless(result is view)
def test_it_different(self):
class DummyView1:
@@ -3866,8 +3926,10 @@ class Test_decorate_view(unittest.TestCase):
""" """
view1 = DummyView1()
view2 = DummyView2()
- result = self._callFUT(view1, view2)
- self.assertEqual(result, True)
+ def afunc(self, view):
+ return view1
+ result = self._callFUT(afunc, view2)
+ self.assertEqual(result, view1)
self.failUnless(view1.__doc__ is view2.__doc__)
self.failUnless(view1.__module__ is view2.__module__)
self.failUnless(view1.__name__ is view2.__name__)
diff --git a/pyramid/tests/test_paster.py b/pyramid/tests/test_paster.py
index e2478ac4f..35349b7c7 100644
--- a/pyramid/tests/test_paster.py
+++ b/pyramid/tests/test_paster.py
@@ -111,6 +111,144 @@ class TestPShellCommand(unittest.TestCase):
self.failUnless(interact.banner)
self.assertEqual(apps, [app])
+class TestPRoutesCommand(unittest.TestCase):
+ def _getTargetClass(self):
+ from pyramid.paster import PRoutesCommand
+ return PRoutesCommand
+
+ def _makeOne(self):
+ return self._getTargetClass()('proutes')
+
+ def test_no_routes(self):
+ command = self._makeOne()
+ mapper = DummyMapper()
+ command._get_mapper = lambda *arg: mapper
+ L = []
+ command.out = L.append
+ app = DummyApp()
+ loadapp = DummyLoadApp(app)
+ command.loadapp = (loadapp,)
+ command.args = ('/foo/bar/myapp.ini', 'myapp')
+ result = command.command()
+ self.assertEqual(result, None)
+ self.assertEqual(L, [])
+
+ def test_single_route_no_route_registered(self):
+ command = self._makeOne()
+ route = DummyRoute('a', '/a')
+ mapper = DummyMapper(route)
+ command._get_mapper = lambda *arg: mapper
+ L = []
+ command.out = L.append
+ app = DummyApp()
+ loadapp = DummyLoadApp(app)
+ command.loadapp = (loadapp,)
+ command.args = ('/foo/bar/myapp.ini', 'myapp')
+ result = command.command()
+ self.assertEqual(result, None)
+ self.assertEqual(len(L), 3)
+ self.assertEqual(L[-1].split(), ['a', '/a', '<unknown>'])
+
+ def test_single_route_no_views_registered(self):
+ from zope.interface import Interface
+ from pyramid.registry import Registry
+ from pyramid.interfaces import IRouteRequest
+ registry = Registry()
+ def view():pass
+ class IMyRoute(Interface):
+ pass
+ registry.registerUtility(IMyRoute, IRouteRequest, name='a')
+ command = self._makeOne()
+ route = DummyRoute('a', '/a')
+ mapper = DummyMapper(route)
+ command._get_mapper = lambda *arg: mapper
+ L = []
+ command.out = L.append
+ app = DummyApp()
+ app.registry = registry
+ loadapp = DummyLoadApp(app)
+ command.loadapp = (loadapp,)
+ command.args = ('/foo/bar/myapp.ini', 'myapp')
+ result = command.command()
+ self.assertEqual(result, None)
+ self.assertEqual(len(L), 3)
+ self.assertEqual(L[-1].split()[:3], ['a', '/a', 'None'])
+
+ def test_single_route_one_view_registered(self):
+ from zope.interface import Interface
+ from pyramid.registry import Registry
+ from pyramid.interfaces import IRouteRequest
+ from pyramid.interfaces import IViewClassifier
+ from pyramid.interfaces import IView
+ registry = Registry()
+ def view():pass
+ class IMyRoute(Interface):
+ pass
+ registry.registerAdapter(view,
+ (IViewClassifier, IMyRoute, Interface),
+ IView, '')
+ registry.registerUtility(IMyRoute, IRouteRequest, name='a')
+ command = self._makeOne()
+ route = DummyRoute('a', '/a')
+ mapper = DummyMapper(route)
+ command._get_mapper = lambda *arg: mapper
+ L = []
+ command.out = L.append
+ app = DummyApp()
+ app.registry = registry
+ loadapp = DummyLoadApp(app)
+ command.loadapp = (loadapp,)
+ command.args = ('/foo/bar/myapp.ini', 'myapp')
+ result = command.command()
+ self.assertEqual(result, None)
+ self.assertEqual(len(L), 3)
+ self.assertEqual(L[-1].split()[:4], ['a', '/a', '<function', 'view'])
+
+ def test_single_route_one_view_registered_with_factory(self):
+ from zope.interface import Interface
+ from pyramid.registry import Registry
+ from pyramid.interfaces import IRouteRequest
+ from pyramid.interfaces import IViewClassifier
+ from pyramid.interfaces import IView
+ registry = Registry()
+ def view():pass
+ class IMyRoot(Interface):
+ pass
+ class IMyRoute(Interface):
+ pass
+ registry.registerAdapter(view,
+ (IViewClassifier, IMyRoute, IMyRoot),
+ IView, '')
+ registry.registerUtility(IMyRoute, IRouteRequest, name='a')
+ command = self._makeOne()
+ def factory(request): pass
+ route = DummyRoute('a', '/a', factory=factory)
+ mapper = DummyMapper(route)
+ command._get_mapper = lambda *arg: mapper
+ L = []
+ command.out = L.append
+ app = DummyApp()
+ app.registry = registry
+ loadapp = DummyLoadApp(app)
+ command.loadapp = (loadapp,)
+ command.args = ('/foo/bar/myapp.ini', 'myapp')
+ result = command.command()
+ self.assertEqual(result, None)
+ self.assertEqual(len(L), 3)
+ self.assertEqual(L[-1].split()[:3], ['a', '/a', '<unknown>'])
+
+ def test__get_mapper(self):
+ from pyramid.registry import Registry
+ from pyramid.urldispatch import RoutesMapper
+ command = self._makeOne()
+ registry = Registry()
+ class App: pass
+ app = App()
+ app.registry = registry
+ result = command._get_mapper(app)
+ self.assertEqual(result.__class__, RoutesMapper)
+
+
class TestGetApp(unittest.TestCase):
def _callFUT(self, config_file, section_name, loadapp):
from pyramid.paster import get_app
@@ -125,6 +263,8 @@ class TestGetApp(unittest.TestCase):
self.assertEqual(loadapp.section_name, 'myapp')
self.assertEqual(loadapp.relative_to, os.getcwd())
self.assertEqual(result, app)
+
+
class Dummy:
pass
@@ -149,7 +289,7 @@ class DummyIPShell(object):
dummy_root = Dummy()
class DummyRegistry(object):
- def queryUtility(self, iface, default=None):
+ def queryUtility(self, iface, default=None, name=''):
return default
dummy_registry = DummyRegistry()
@@ -188,3 +328,16 @@ class DummyThreadLocalManager:
def pop(self):
self.popped.append(True)
+class DummyMapper(object):
+ def __init__(self, *routes):
+ self.routes = routes
+
+ def get_routes(self):
+ return self.routes
+
+class DummyRoute(object):
+ def __init__(self, name, pattern, factory=None):
+ self.name = name
+ self.pattern = pattern
+ self.factory = factory
+
diff --git a/pyramid/tests/test_view.py b/pyramid/tests/test_view.py
index 79e363756..7fc066319 100644
--- a/pyramid/tests/test_view.py
+++ b/pyramid/tests/test_view.py
@@ -317,10 +317,9 @@ class TestViewConfigDecorator(unittest.TestCase):
settings = call_venusian(venusian)
self.assertEqual(len(settings), 1)
renderer = settings[0]['renderer']
- self.assertEqual(renderer,
- {'name':'fixtures/minimal.pt',
- 'package':pyramid.tests,
- })
+ self.assertEqual(renderer.name, 'fixtures/minimal.pt')
+ self.assertEqual(renderer.package, pyramid.tests)
+ self.assertEqual(renderer.registry.__class__, DummyRegistry)
def test_call_with_renderer_dict(self):
decorator = self._makeOne(renderer={'a':1})
@@ -494,9 +493,13 @@ class DummyVenusian(object):
self.attachments.append((wrapped, callback, category))
return self.info
+class DummyRegistry(object):
+ pass
+
class DummyConfig(object):
def __init__(self):
self.settings = []
+ self.registry = DummyRegistry()
def add_view(self, **kw):
self.settings.append(kw)
diff --git a/pyramid/url.py b/pyramid/url.py
index e1eaaaa1e..ac569eecb 100644
--- a/pyramid/url.py
+++ b/pyramid/url.py
@@ -53,7 +53,7 @@ def route_url(route_name, request, *elements, **kw):
``*remainder`` replacement value, it is tacked on to the URL
untouched.
- If a keyword argument ``_query`` is present, it will used to
+ If a keyword argument ``_query`` is present, it will be used to
compose a query string that will be tacked on to the end of the
URL. The value of ``_query`` must be a sequence of two-tuples
*or* a data structure with an ``.items()`` method that returns a
@@ -221,7 +221,7 @@ def resource_url(resource, request, *elements, **kw):
``elements`` are used, the generated URL will *not*
end in trailing a slash.
- If a keyword argument ``query`` is present, it will used to
+ If a keyword argument ``query`` is present, it will be used to
compose a query string that will be tacked on to the end of the
URL. The value of ``query`` must be a sequence of two-tuples *or*
a data structure with an ``.items()`` method that returns a
diff --git a/pyramid/view.py b/pyramid/view.py
index 3dc110863..776185d8b 100644
--- a/pyramid/view.py
+++ b/pyramid/view.py
@@ -17,8 +17,10 @@ from zope.interface import providedBy
from pyramid.interfaces import IRoutesMapper
from pyramid.interfaces import IView
from pyramid.interfaces import IViewClassifier
+from pyramid.interfaces import IRendererFactory
from pyramid.httpexceptions import HTTPFound
+from pyramid.renderers import RendererHelper
from pyramid.static import static_view
from pyramid.threadlocal import get_current_registry
@@ -404,6 +406,12 @@ class view_config(object):
settings = self.__dict__.copy()
def callback(context, name, ob):
+ renderer = settings.get('renderer')
+ if isinstance(renderer, basestring):
+ renderer = RendererHelper(name=renderer,
+ package=info.module,
+ registry=context.config.registry)
+ settings['renderer'] = renderer
context.config.add_view(view=ob, **settings)
info = self.venusian.attach(wrapped, callback, category='pyramid')
@@ -415,10 +423,6 @@ class view_config(object):
if settings['attr'] is None:
settings['attr'] = wrapped.__name__
- renderer_name = settings.get('renderer')
- if renderer_name is not None and not isinstance(renderer_name, dict):
- settings['renderer'] = {'name':renderer_name,
- 'package':info.module}
settings['_info'] = info.codeinfo
return wrapped
diff --git a/pyramid/zcml.py b/pyramid/zcml.py
index f668e3b4b..a2088e505 100644
--- a/pyramid/zcml.py
+++ b/pyramid/zcml.py
@@ -161,12 +161,7 @@ def view(
cacheable=True, # not used, here for b/w compat < 0.8
):
- if renderer is not None:
- package = getattr(_context, 'package', None)
- renderer = {'name':renderer, 'package':package}
-
context = context or for_
-
config = Configurator.with_context(_context)
config.add_view(
permission=permission, context=context, view=view, name=name,
diff --git a/setup.py b/setup.py
index f698f06ae..d58641487 100644
--- a/setup.py
+++ b/setup.py
@@ -88,6 +88,7 @@ setup(name='pyramid',
pylons_sqla=pyramid.paster:PylonsSQLAlchemyProjectTemplate
[paste.paster_command]
pshell=pyramid.paster:PShellCommand
+ proutes=pyramid.paster:PRoutesCommand
[console_scripts]
bfg2pyramid = pyramid.fixers.fix_bfg_imports:main
"""