From a5cac1ec16c9d45b70436772b992756d92e40bd4 Mon Sep 17 00:00:00 2001 From: amercader Date: Wed, 27 Mar 2013 12:20:14 +0000 Subject: [PATCH] [#705] remove_url_params supports multiple params 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) --- ckan/lib/helpers.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 4c6be063221..aeb8d66d35e 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -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) @@ -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)