summaryrefslogtreecommitdiff
path: root/repoze/bfg/traversal.py
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2008-11-12 20:15:24 +0000
committerChris McDonough <chrism@agendaless.com>2008-11-12 20:15:24 +0000
commiteb6afe916a2c7456d4398e2f0df04ebacccd7f30 (patch)
tree5b8fb2a8a119c3209f69c417a07e5a7e6c9dbd3a /repoze/bfg/traversal.py
parent720c8893749556208296af0ee5adcfe9eb373ec7 (diff)
downloadpyramid-eb6afe916a2c7456d4398e2f0df04ebacccd7f30.tar.gz
pyramid-eb6afe916a2c7456d4398e2f0df04ebacccd7f30.tar.bz2
pyramid-eb6afe916a2c7456d4398e2f0df04ebacccd7f30.zip
- ``repoze.bfg.traversal.model_url`` now always appends a slash to
all generated URLs unless further elements are passed in as the third and following arguments. Rationale: views often use ``model_url`` without the third-and-following arguments in order to generate a URL for a model in order to point at the default view of a model. The URL that points to the default view of the *root* model is technically ``http://mysite/`` as opposed to ``http://mysite`` (browsers happen to ask for '/' implicitly in the GET request). Because URLs are never automatically generated for anything *except* models by ``model_url``, and because the root model is not really special, we continue this pattern. The impact of this change is minimal (at most you will have too many slashes in your URL, which BFG deals with gracefully anyway). Prep for 0.4.8.
Diffstat (limited to 'repoze/bfg/traversal.py')
-rw-r--r--repoze/bfg/traversal.py36
1 files changed, 16 insertions, 20 deletions
diff --git a/repoze/bfg/traversal.py b/repoze/bfg/traversal.py
index 29677fe99..8c2df6a41 100644
--- a/repoze/bfg/traversal.py
+++ b/repoze/bfg/traversal.py
@@ -103,30 +103,25 @@ def find_interface(model, interface):
if interface.providedBy(location):
return location
-def _pjoin(path):
- path = list(path)
- path.insert(0, '')
- result = '/'.join(path)
- if not result:
- result = '/'
- return result
-
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 arguments
- 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."""
+ request, plus any ``SCRIPT_NAME``. The model URL will end with a
+ trailing slash. Any positional arguments passed in as
+ ``elements`` will be joined by slashes and appended to the model
+ URL. The passed in elements are *not* URL-quoted. The ``model``
+ passed in must be :term:`location`-aware."""
rpath = []
for location in lineage(model):
if location.__name__:
rpath.append(urllib.quote(location.__name__))
- path = list(reversed(rpath))
- path.extend(elements)
- path = _pjoin(path)
- return urlparse.urljoin(request.application_url, path)
+ prefix = '/'.join(reversed(rpath))
+ suffix = '/'.join(elements)
+ path = '/'.join([prefix, suffix]) # always have trailing slash
+ app_url = request.application_url
+ if not app_url.endswith('/'):
+ app_url = app_url+'/'
+ return urlparse.urljoin(app_url, path)
def model_path(model, *elements):
""" Return a string representing the absolute path of the model
@@ -138,8 +133,9 @@ def model_path(model, *elements):
for location in lineage(model):
if location.__name__:
rpath.append(location.__name__)
- path = list(reversed(rpath))
- path.extend(elements)
- path = _pjoin(path)
+ path = '/' + '/'.join(reversed(rpath))
+ if elements: # never have a trailing slash
+ suffix = '/'.join(elements)
+ path = '/'.join([path, suffix])
return path