From 65476e8d2cef6b3ce105c4786645a88151a09a6c Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 1 Nov 2009 00:48:04 +0000 Subject: - The ZCML ``route`` directive's attributes ``xhr``, ``request_method``, ``path_info``, ``request_param``, ``header`` and ``accept`` are now *route* predicates rather than *view* predicates. If one or more of these predicates is specified in the route configuration, all of the predicates must return true for the route to match a request. If one or more of the route predicates associated with a route returns ``False`` when checked during a request, the route match fails, and the next match in the routelist is tried. This differs from the previous behavior, where no route predicates existed and all predicates were considered view predicates, because in that scenario, the next route was not tried. --- repoze/bfg/urldispatch.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'repoze/bfg/urldispatch.py') diff --git a/repoze/bfg/urldispatch.py b/repoze/bfg/urldispatch.py index 4088cef12..185baa1a1 100644 --- a/repoze/bfg/urldispatch.py +++ b/repoze/bfg/urldispatch.py @@ -5,18 +5,20 @@ from zope.interface import directlyProvides from repoze.bfg.interfaces import IRouteRequest +from repoze.bfg.compat import all +from repoze.bfg.encode import url_quote from repoze.bfg.traversal import traversal_path from repoze.bfg.traversal import quote_path_segment -from repoze.bfg.encode import url_quote _marker = object() class Route(object): - def __init__(self, path, name=None, factory=None): + def __init__(self, path, name=None, factory=None, predicates=()): self.path = path self.match, self.generate = _compile_route(path) self.name = name self.factory = factory + self.predicates = predicates class RoutesRootFactory(object): def __init__(self, default_root_factory): @@ -30,8 +32,8 @@ class RoutesRootFactory(object): def get_routes(self): return self.routelist - def connect(self, path, name, factory=None): - route = Route(path, name, factory) + def connect(self, path, name, factory=None, predicates=()): + route = Route(path, name, factory, predicates) self.routelist.append(route) self.routes[name] = route return route @@ -69,6 +71,9 @@ class RoutesRootFactory(object): for route in self.routelist: match = route.match(path) if match is not None: + preds = route.predicates + if preds and not all((p(None, request) for p in preds)): + continue environ['wsgiorg.routing_args'] = ((), match) environ['bfg.routes.route'] = route environ['bfg.routes.matchdict'] = match -- cgit v1.2.3