Skip to content

Commit

Permalink
[#705] remove_url_params supports multiple params
Browse files Browse the repository at this point in the history
Sometimes is useful to remove more than one parameter from the URL to
clear a filter. This change does not modify any logic, just adds support
for the first parameter being a list instead of a string.

The value and replace parameters only will apply to the first element of
the list (or the provided param if key is a string)
  • Loading branch information
amercader committed Mar 27, 2013
1 parent 8dd20f3 commit a5cac1e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions ckan/lib/helpers.py
Expand Up @@ -1129,13 +1129,16 @@ def add_url_param(alternative_url=None, controller=None, action=None,
return _create_url_with_params(params=params, controller=controller,
action=action, extras=extras)


def remove_url_param(key, value=None, replace=None, controller=None,
action=None, extras=None, alternative_url=None):
''' Remove a key from the current parameters. A specific key/value
pair can be removed by passing a second value argument otherwise all
pairs matching the key will be removed. If replace is given then a
new param key=replace will be added.
''' Remove one or multiple keys from the current parameters.
The first parameter can be either a string with the name of the key to
remove or a list of keys to remove.
A specific key/value pair can be removed by passing a second value
argument otherwise all pairs matching the key will be removed. If replace
is given then a new param key=replace will be added.
Note that the value and replace parameters only apply to the first key
provided (or the only one provided if key is a string).
controller action & extras (dict) are used to create the base url
via url_for(controller=controller, action=action, **extras)
Expand All @@ -1144,14 +1147,20 @@ def remove_url_param(key, value=None, replace=None, controller=None,
This can be overriden providing an alternative_url, which will be used
instead.
'''
if isinstance(key, basestring):
keys = [key]
else:
keys = key

params_nopage = [(k, v) for k, v in request.params.items() if k != 'page']
params = list(params_nopage)
if value:
params.remove((key, value))
params.remove((keys[0], value))
else:
[params.remove((k, v)) for (k, v) in params[:] if k == key]
for key in keys:
[params.remove((k, v)) for (k, v) in params[:] if k == key]
if replace is not None:
params.append((key, replace))
params.append((keys[0], replace))

if alternative_url:
return _url_with_params(alternative_url, params)
Expand Down

0 comments on commit a5cac1e

Please sign in to comment.