Skip to content

Commit

Permalink
Reorganize several sections.
Browse files Browse the repository at this point in the history
Reorganize configuration, routing, testing, views, and misc sections.
  • Loading branch information
mikeorr committed Feb 20, 2012
1 parent fe2ef7b commit d1918e1
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 69 deletions.
64 changes: 1 addition & 63 deletions configuration.rst → configuration/django_settings.rst
@@ -1,7 +1,5 @@
.. _configuration:

Django-Style "settings.py" Configuration
----------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

If you enjoy accessing global configuration via import statemetns ala
Django's ``settings.py``, you can do something similar in Pyramid.
Expand Down Expand Up @@ -73,63 +71,3 @@ this format::
the section name in the config file that represents your app
(e.g. ``[app:myapp]``). In the above example, your application will refuse
to start without this environment variable being present.

Chaining Decorators
-------------------

Pyramid has a ``decorator=`` argument to its view configuration. It accepts
a single decorator that will wrap the *mapped* view callable represented by
the view configuration. That means that, no matter what the signature and
return value of the original view callable, the decorated view callable will
receive two arguments: ``context`` and ``request`` and will return a response
object:

.. code-block:: python
# the decorator
def decorator(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
# the view configuration
@view_config(decorator=decorator, renderer='json')
def myview(request):
return {'a':1}
But the ``decorator`` argument only takes a single decorator. What happens
if you want to use more than one decorator? You can chain them together:

.. code-block:: python
def combine(*decorators):
def floo(view_callable):
for decorator in decorators:
view_callable = decorator(view_callable)
return view_callable
return floo
def decorator1(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
def decorator2(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
def decorator3(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
alldecs = combine(decorator1, decorator2, decorator3)
two_and_three = combine(decorator2, decorator3)
one_and_three = combine(decorator1, decorator3)
@view_config(decorator=alldecs, renderer='json')
def myview(request):
return {'a':1}
7 changes: 7 additions & 0 deletions configuration/index.rst
@@ -0,0 +1,7 @@
Configuration
%%%%%%%%%%%%%

.. toctree::
:maxdepth: 2

django_settings
11 changes: 5 additions & 6 deletions index.rst
Expand Up @@ -7,24 +7,23 @@ The Pyramid Cookbook presents topical, practical usages of :mod:`Pyramid`.
:maxdepth: 2

auth/index
configuration/index
database/index
debugging
deployment/index
logging/index
porting/index
pylons/index
routing/index
templates/index
testing/index
views/index
misc/index

.. toctree::
:maxdepth: 2

files
interfaces
configuration
events
testing
traversal
mac_install
glossary
todo

Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions misc/index.rst
@@ -0,0 +1,9 @@
Miscellaneous
%%%%%%%%%%%%%

.. toctree::
:maxdepth: 2

interfaces
events
mac_install
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions routing/index.rst
@@ -0,0 +1,7 @@
Traversal and URL dispatch
%%%%%%%%%%%%%%%%%%%%%%%%%%

.. toctree::
:maxdepth: 2

combining
7 changes: 7 additions & 0 deletions testing/index.rst
@@ -0,0 +1,7 @@
Testing
%%%%%%%

.. toctree::
:maxdepth: 2

testing_post_curl
File renamed without changes.
59 changes: 59 additions & 0 deletions views/chaining_decorators.rst
@@ -0,0 +1,59 @@
Chaining Decorators
%%%%%%%%%%%%%%%%%%%

Pyramid has a ``decorator=`` argument to its view configuration. It accepts
a single decorator that will wrap the *mapped* view callable represented by
the view configuration. That means that, no matter what the signature and
return value of the original view callable, the decorated view callable will
receive two arguments: ``context`` and ``request`` and will return a response
object:

.. code-block:: python
# the decorator
def decorator(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
# the view configuration
@view_config(decorator=decorator, renderer='json')
def myview(request):
return {'a':1}
But the ``decorator`` argument only takes a single decorator. What happens
if you want to use more than one decorator? You can chain them together:

.. code-block:: python
def combine(*decorators):
def floo(view_callable):
for decorator in decorators:
view_callable = decorator(view_callable)
return view_callable
return floo
def decorator1(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
def decorator2(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
def decorator3(view_callable):
def inner(context, request):
return view_callable(context, request)
return inner
alldecs = combine(decorator1, decorator2, decorator3)
two_and_three = combine(decorator2, decorator3)
one_and_three = combine(decorator1, decorator3)
@view_config(decorator=alldecs, renderer='json')
def myview(request):
return {'a':1}
7 changes: 7 additions & 0 deletions views/index.rst
@@ -0,0 +1,7 @@
Views
%%%%%

.. toctree::
:maxdepth: 2

chaining_decorators

0 comments on commit d1918e1

Please sign in to comment.