From 6225a24982dfaeffbc53f85d214159c08a0dbfc2 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 19 Dec 2009 18:40:19 +0000 Subject: - Add a ``custom_predicates`` argument to the ``Configurator`` ``add_view`` method, the ``bfg_view`` decorator and the attribute list of the ZCML ``view`` directive. If ``custom_predicates`` is specified, it must be a sequence of predicate callables (a predicate callable accepts two arguments: ``context`` and ``request`` and returns ``True`` or ``False``). The associated view callable will only be invoked if all custom predicates return ``True``. Use one or more custom predicates when no existing predefined predicate is useful. Predefined and custom predicates can be mixed freely. - Add a ``custom_predicates`` argument to the ``Configurator`` ``add_route`` and the attribute list of the ZCML ``route`` directive. If ``custom_predicates`` is specified, it must be a sequence of predicate callables (a predicate callable accepts two arguments: ``context`` and ``request`` and returns ``True`` or ``False``). The associated route will match will only be invoked if all custom predicates return ``True``, else route matching continues. Use one or more custom predicates when no existing predefined predicate is useful. Predefined and custom predicates can be mixed freely. --- docs/designdefense.rst | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'docs') diff --git a/docs/designdefense.rst b/docs/designdefense.rst index 7260ec1ce..ea5a072f8 100644 --- a/docs/designdefense.rst +++ b/docs/designdefense.rst @@ -404,11 +404,27 @@ request method was ``POST`` and that the remote user agent passed For more information about predicates, see :ref:`view_predicates_in_1dot1` and :ref:`route_predicates_in_1dot1`. -Many "prebaked" predicates exist. Use of only "prebaked" predicates, -however, doesn't entirely meet Ian's criterion. He would like to be -able to match a request using a lambda or another function which -interrogates the request imperatively. This may become a feature in a -later release. +Many "prebaked" predicates exist. However, use of only "prebaked" +predicates, however, doesn't entirely meet Ian's criterion. He would +like to be able to match a request using a lambda or another function +which interrogates the request imperatively. In 1.2, we acommodate +this by allowing people to define "custom" view predicates: + +.. code-block:: python + :linenos: + + from repoze.bfg.view import bfg_view + from webob import Response + + def subpath(context, request): + return request.subpath and request.subpath[0] == 'abc' + + @bfg_view(custom_predicates=(subpath,))) + def aview(request): + return Response('OK') + +The above view will only match when the first element of the request's +:term:`subpath` is ``abc``. .. _zcml_encouragement: -- cgit v1.2.3