From 5a11c03834cf4aedf2c21c050d2dea0b2d229076 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 24 Jun 2009 06:10:14 +0000 Subject: Change the implementation and the signature for ``route_url``. --- repoze/bfg/url.py | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'repoze/bfg/url.py') diff --git a/repoze/bfg/url.py b/repoze/bfg/url.py index d3fc55203..f9927c431 100644 --- a/repoze/bfg/url.py +++ b/repoze/bfg/url.py @@ -3,28 +3,32 @@ import urllib from zope.component import queryMultiAdapter +from zope.component import getUtility from repoze.bfg.interfaces import IContextURL +from repoze.bfg.interfaces import IRoutesMapper from repoze.bfg.traversal import TraversalContextURL from repoze.bfg.traversal import quote_path_segment -from routes import url_for +from routes import URLGenerator from routes.util import GenerationException -def route_url(*arg, **kw): +def route_url(request, route_name, **kw): """Generates a fully qualified URL for a named BFG route. - Use the route's ``name`` as the first positional argument. Use - keyword arguments to supply values which match any dynamic path - elements in the route definition. Raises a ValueError exception - if the URL cannot be generated when the + Use the request object as the first positional argument. Use the + route's ``name`` as the second positional argument. Use keyword + arguments to supply values which match any dynamic path elements + in the route definition. Raises a ValueError exception if the URL + cannot be generated when the - For example, if you've defined a route named"foobar" with the path + For example, if you've defined a route named "foobar" with the path ``:foo/:bar/*traverse``:: - route_url(foo='1') => - route_url(foo='1', bar='2') => - route_url(foo='1', bar='2',traverse='a/b) => http://e.com/1/2/a/b + route_url(request, 'foobar', foo='1') => + route_url(request, 'foobar', foo='1', bar='2') => + route_url('foobar', foo='1', bar='2', + 'traverse='a/b) => http://e.com/1/2/a/b All keys given to ``route_url`` are sent to the BFG Routes "mapper" instance for generation except for:: @@ -38,7 +42,9 @@ def route_url(*arg, **kw): if not 'qualified' in kw: kw['qualified'] = True try: - return url_for(*arg, **kw) + mapper = getUtility(IRoutesMapper) + generator = URLGenerator(mapper, request.environ) + return generator(route_name, **kw) except GenerationException, why: raise ValueError(str(why)) -- cgit v1.2.3