diff options
| author | Chris McDonough <chrism@plope.com> | 2011-01-03 02:37:45 -0500 |
|---|---|---|
| committer | Chris McDonough <chrism@plope.com> | 2011-01-03 02:37:45 -0500 |
| commit | 83373586ea80ea5057da5fc4ede43278af47a318 (patch) | |
| tree | bb29ad6d816873da3ae9f8a9d78453d8929d0ed2 | |
| parent | 070a798934bd39223fad37471e8e2168d78909a2 (diff) | |
| parent | b23e6e4a939ddaf764137828539f63df6077f4c7 (diff) | |
| download | pyramid-83373586ea80ea5057da5fc4ede43278af47a318.tar.gz pyramid-83373586ea80ea5057da5fc4ede43278af47a318.tar.bz2 pyramid-83373586ea80ea5057da5fc4ede43278af47a318.zip | |
Merge branch 'master' of https://github.com/ergo/pyramid into ergo-master
| -rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
| -rw-r--r-- | pyramid/url.py | 32 |
2 files changed, 34 insertions, 0 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 7b0364b6d..ec9042f08 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -120,3 +120,5 @@ Contributors - Rob Miller, 2010/12/28 - Marius Gedminas, 2010/12/31 + +- Marcin Lulek, 2011/01/02
\ No newline at end of file diff --git a/pyramid/url.py b/pyramid/url.py index c11e39143..cdeb0d6c8 100644 --- a/pyramid/url.py +++ b/pyramid/url.py @@ -366,6 +366,38 @@ def static_url(path, request, **kw): return info.generate(path, request, **kw) +def current_route_url(request, *elements, **query): + """Generates a fully qualified URL for a named :app:`Pyramid` + :term:`route configuration` based on current route. + + This function is to supplement :func:`pyramid.url.route_url`, it's purpose + is to allow for easy overriding parameters of current route. + + Example:: + If our url route is /foo/{page} - and current url is /foo/1 : + current_route_url(request, page='2') + Will return the string ``/foo/2``. + + Alternatively we may have routes /foo/{action} and /foo/{action}/{page}, + on url /foo/view we may want to have a template macro/def that allows us + to output next/prev buttons that contain page numbers. The ability to + override route name helps with this task. + + Example:: + where ``foo_pages`` is route name for ``/foo/{action}/{page}`` + current_url(request, _route_name='foo_pages', page=paginator.page+1) + Will return the string like: ``/foo/view/5`` + """ + if '_route_name' in query: + route_name = query['_route_name'] + else: + route_name = getattr(request, 'matched_route', None) + route_name = getattr(route_name, 'name') + matchdict = {} + matchdict.update(getattr(request, 'matchdict', {}) or {}) + matchdict.update(query) + return route_url(route_name, request, *elements, **matchdict) + @lru_cache(1000) def _join_elements(elements): return '/'.join([quote_path_segment(s) for s in elements]) |
