From 6e0f02a1e2d6fdece760f5ce1e61850b9514897d Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Wed, 27 May 2015 10:29:21 -0500 Subject: add an example decorator showing a response being used unconditionally --- docs/narr/viewconfig.rst | 15 +++++++++++++++ pyramid/config/views.py | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/docs/narr/viewconfig.rst b/docs/narr/viewconfig.rst index d5203c6ba..fc5ae6dc6 100644 --- a/docs/narr/viewconfig.rst +++ b/docs/narr/viewconfig.rst @@ -234,6 +234,21 @@ Non-Predicate Arguments def myview(request): ... + All view callables in the decorator chain must return a response object + implementing :class:`pyramid.interfaces.IResponse` or raise an exception: + + .. code-block:: python + + def log_timer(wrapped): + def wrapper(context, request): + start = time.time() + response = wrapped(context, request) + duration = time.time() - start + response.headers['X-View-Time'] = '%.3f' % (duration,) + log.info('view took %.3f seconds', duration) + return response + return wrapper + ``mapper`` A Python object or :term:`dotted Python name` which refers to a :term:`view mapper`, or ``None``. By default it is ``None``, which indicates that the diff --git a/pyramid/config/views.py b/pyramid/config/views.py index a522880c4..34fc49c00 100644 --- a/pyramid/config/views.py +++ b/pyramid/config/views.py @@ -848,6 +848,22 @@ class ViewsConfiguratorMixin(object): think about preserving function attributes such as ``__name__`` and ``__module__`` within decorator logic). + All view callables in the decorator chain must return a response + object implementing :class:`pyramid.interfaces.IResponse` or raise + an exception: + + .. code-block:: python + + def log_timer(wrapped): + def wrapper(context, request): + start = time.time() + response = wrapped(context, request) + duration = time.time() - start + response.headers['X-View-Time'] = '%.3f' % (duration,) + log.info('view took %.3f seconds', duration) + return response + return wrapper + .. versionchanged:: 1.4a4 Passing an iterable. -- cgit v1.2.3