From 35ff8e696a2c983cee119c936ea12ecb2f7da2c3 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Tue, 29 Jul 2008 01:54:50 +0000 Subject: 0.2.5: add model_url. --- repoze/bfg/traversal.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'repoze/bfg/traversal.py') diff --git a/repoze/bfg/traversal.py b/repoze/bfg/traversal.py index d0e140375..c72c90e5e 100644 --- a/repoze/bfg/traversal.py +++ b/repoze/bfg/traversal.py @@ -1,4 +1,5 @@ import urllib +import urlparse from zope.interface import classProvides from zope.interface import implements @@ -66,9 +67,25 @@ class ModelGraphTraverser(object): return ob, name, path def find_interface(context, interface): - """ Return an object providing 'interface' anywhere in the parent - chain of 'context' or None if no object providing that interface - can be found in the parent chain """ + """ Return an object providing ``interface`` anywhere in the + parent chain of ``context`` or ``None`` if no object providing + that interface can be found in the parent chain""" for location in LocationIterator(context): if interface.providedBy(location): return location + +def model_url(model, request, *elements): + """ Return the absolute URL of the model object based on the + ``wsgi.url_scheme``, ``HTTP_HOST`` or ``SERVER_NAME`` in the + request, plus any ``SCRIPT_NAME``. Any positional passed in as + ``elements`` will be joined by slashes and appended to the + generated URL. The passed in elements are *not* URL-quoted. The + ``model`` passed in must be :term:`location`-aware.""" + rpath = [] + for location in LocationIterator(model): + if location.__name__: + rpath.append(urllib.quote(location.__name__)) + path = list(reversed(rpath)) + path.extend(elements) + path = '/'.join(path) + return urlparse.urljoin(request.application_url, path) -- cgit v1.2.3