summaryrefslogtreecommitdiff
path: root/docs/narr/hooks.rst
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2016-06-01 17:13:27 -0400
committerChris McDonough <chrism@plope.com>2016-06-01 17:13:27 -0400
commit3e9a737500e79a6a919ce53db9557c75d874b84c (patch)
treeef674c176ab29b9dede8a8fa70c3a18a26edde44 /docs/narr/hooks.rst
parentb5f065906f75efdcc9f80d4f0b8b4092e92b41c0 (diff)
parent382f93e2bfec5563587e306fda3fd34759314300 (diff)
downloadpyramid-3e9a737500e79a6a919ce53db9557c75d874b84c.tar.gz
pyramid-3e9a737500e79a6a919ce53db9557c75d874b84c.tar.bz2
pyramid-3e9a737500e79a6a919ce53db9557c75d874b84c.zip
Merge branch 'master' of github.com:Pylons/pyramid
Diffstat (limited to 'docs/narr/hooks.rst')
-rw-r--r--docs/narr/hooks.rst67
1 files changed, 61 insertions, 6 deletions
diff --git a/docs/narr/hooks.rst b/docs/narr/hooks.rst
index b776f99e8..49ef29d3f 100644
--- a/docs/narr/hooks.rst
+++ b/docs/narr/hooks.rst
@@ -300,13 +300,38 @@ added as a property and its result is cached per-request by setting
``reify=True``. This way, we eliminate the overhead of running the function
multiple times.
+.. testsetup:: group1
+
+ from pyramid.config import Configurator
+
+
+ def total(request, *args):
+ return sum(args)
+
+
+ def prop(request):
+ print("getting the property")
+ return "the property"
+
+
+
+ config = Configurator()
+ config.add_request_method(total)
+ config.add_request_method(prop, reify=True)
+ config.commit()
+
+ from pyramid.scripting import prepare
+ request = prepare(registry=config.registry)["request"]
+
+.. doctest:: group1
+
>>> request.total(1, 2, 3)
6
>>> request.prop
getting the property
- the property
+ 'the property'
>>> request.prop
- the property
+ 'the property'
To not cache the result of ``request.prop``, set ``property=True`` instead of
``reify=True``.
@@ -338,13 +363,42 @@ Here is an example of passing a class to ``Configurator.add_request_method``:
We attach and cache an object named ``extra`` to the ``request`` object.
+.. testsetup:: group2
+
+ from pyramid.config import Configurator
+ from pyramid.decorator import reify
+
+ class ExtraStuff(object):
+
+ def __init__(self, request):
+ self.request = request
+
+ def total(self, *args):
+ return sum(args)
+
+ # use @property if you don't want to cache the result
+ @reify
+ def prop(self):
+ print("getting the property")
+ return "the property"
+
+ config = Configurator()
+ config.add_request_method(ExtraStuff, 'extra', reify=True)
+ config.commit()
+
+ from pyramid.scripting import prepare
+ request = prepare(registry=config.registry)["request"]
+
+.. doctest:: group2
+
>>> request.extra.total(1, 2, 3)
6
>>> request.extra.prop
getting the property
- the property
+ 'the property'
>>> request.extra.prop
- the property
+ 'the property'
+
.. index::
single: response factory
@@ -1593,8 +1647,9 @@ the user-defined :term:`view callable`:
``csrf_view``
Used to check the CSRF token provided in the request. This element is a
- no-op if both the ``require_csrf`` view option and the
- ``pyramid.require_default_csrf`` setting are disabled.
+ no-op if ``require_csrf`` view option is not ``True``. Note there will
+ always be a ``require_csrf`` option if a default value was assigned via
+ :meth:`pyramid.config.Configurator.set_default_csrf_options`.
``owrapped_view``