From dc405b2e9bac8e43350442a7824901f5ced8e3d5 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 24 Jun 2009 04:04:06 +0000 Subject: - Added the ``repoze.bfg.url.route_url`` API. This is meant to "front" for the Routes ``url_for`` API. See the URL Dispatch narrative chapter and the "repoze.bfg.url" module API documentation for more information. --- repoze/bfg/url.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'repoze/bfg/url.py') diff --git a/repoze/bfg/url.py b/repoze/bfg/url.py index 528d83ae9..d3fc55203 100644 --- a/repoze/bfg/url.py +++ b/repoze/bfg/url.py @@ -8,6 +8,40 @@ from repoze.bfg.interfaces import IContextURL from repoze.bfg.traversal import TraversalContextURL from repoze.bfg.traversal import quote_path_segment +from routes import url_for +from routes.util import GenerationException + +def route_url(*arg, **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 + + 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 + + All keys given to ``route_url`` are sent to the BFG Routes "mapper" + instance for generation except for:: + + anchor specifies the anchor name to be appened to the path + host overrides the default host if provided + protocol overrides the default (current) protocol if provided + qualified return a fully qualified URL (default True) + + """ + if not 'qualified' in kw: + kw['qualified'] = True + try: + return url_for(*arg, **kw) + except GenerationException, why: + raise ValueError(str(why)) + def model_url(model, request, *elements, **kw): """ Generate a string representing the absolute URL of the model (or -- cgit v1.2.3