From 9ee742466213e38981e5d50e3a329132ecb6c567 Mon Sep 17 00:00:00 2001 From: Toby Date: Thu, 21 Jun 2012 17:07:54 +0100 Subject: [PATCH 1/4] [#2579] fixes to group read group read needs to pass the id of the group --- ckan/controllers/group.py | 5 +++-- ckan/lib/helpers.py | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index bf724a7d5cf..595be4867cc 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -149,14 +149,15 @@ def search_url(params): def drill_down_url(**by): return h.drill_down_url(alternative_url=None, controller='group', action='read', - id=c.group_dict.get('name'), **by) + extras=dict(id=c.group_dict.get('name')), + **by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_field(key, value=value, replace=replace, controller='group', action='read', - id=c.group_dict.get('name')) + extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 0b8f3e641da..9d461dd5a41 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -871,7 +871,7 @@ def follow_count(obj_type, obj_id): context = {'model' : model, 'session':model.Session, 'user':c.user} return logic.get_action(action)(context, {'id': obj_id}) -def _create_url_with_params(params=None, controller=None, action=None): +def _create_url_with_params(params=None, controller=None, action=None, extras=None): ''' internal function for building urls ''' def _encode_params(params): return [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) \ @@ -879,25 +879,27 @@ def _encode_params(params): def url_with_params(url, params): params = _encode_params(params) - return url + u'?' + urlencode(params) + return url + u'?' + urllib.urlencode(params) if not controller: controller = c.controller if not action: action = c.action - # can I just set the params here? - url = url_for(controller=controller, action=action) + if not extras: + extras = {} + + url = url_for(controller=controller, action=action, **extras) return url_with_params(url, params) -def drill_down_url(alternative_url=None, controller=None, action=None, **by): +def drill_down_url(alternative_url=None, controller=None, action=None, extras=None, **by): params_nopage = [(k, v) for k,v in request.params.items() if k != 'page'] params = set(params_nopage) params |= set(by.items()) if alternative_url: return url_with_params(alternative_url, params) - return _create_url_with_params(params=params, controller=controller, action=action) + return _create_url_with_params(params=params, controller=controller, action=action, extras=extras) -def remove_field(key, value=None, replace=None, controller=None, action=None): +def remove_field(key, value=None, replace=None, controller=None, action=None, extras=None): """ Remove a key from the current search parameters. A specific key/value pair can be removed by passing a second value argument @@ -912,7 +914,7 @@ def remove_field(key, value=None, replace=None, controller=None, action=None): [params.remove((k, v)) for (k, v) in params[:] if k==key] if replace is not None: params.append((key, replace)) - return _create_url_with_params(params=params, controller=controller, action=action) + return _create_url_with_params(params=params, controller=controller, action=action, extras=extras) # these are the functions that will end up in `h` template helpers # if config option restrict_template_vars is true From b8f2a7088d2787e2fa2da6605d5adac7e751fcfc Mon Sep 17 00:00:00 2001 From: Toby Date: Thu, 21 Jun 2012 17:25:16 +0100 Subject: [PATCH 2/4] [#2579] some refactoring comments etc --- ckan/lib/helpers.py | 49 ++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 9d461dd5a41..4d1621aa03a 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -871,14 +871,13 @@ def follow_count(obj_type, obj_id): context = {'model' : model, 'session':model.Session, 'user':c.user} return logic.get_action(action)(context, {'id': obj_id}) -def _create_url_with_params(params=None, controller=None, action=None, extras=None): - ''' internal function for building urls ''' - def _encode_params(params): - return [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) \ - for k, v in params] +def _create_url_with_params(params=None, controller=None, action=None, + extras=None): + ''' internal function for building urls with parameters. ''' def url_with_params(url, params): - params = _encode_params(params) + params = [(k, v.encode('utf-8') if isinstance(v, basestring) else \ + str(v)) for k, v in params] return url + u'?' + urllib.urlencode(params) if not controller: @@ -891,21 +890,34 @@ def url_with_params(url, params): url = url_for(controller=controller, action=action, **extras) return url_with_params(url, params) -def drill_down_url(alternative_url=None, controller=None, action=None, extras=None, **by): +def drill_down_url(alternative_url=None, controller=None, action=None, + extras=None, **fields): + ''' + Adds extra parameters to existing ones + + controller action & extras (dict) are used to create the base url + via url_for(controller=controller, action=action, **extras) + controller & action default to the current ones + ''' params_nopage = [(k, v) for k,v in request.params.items() if k != 'page'] params = set(params_nopage) - params |= set(by.items()) + params |= set(fields.items()) if alternative_url: return url_with_params(alternative_url, params) - return _create_url_with_params(params=params, controller=controller, action=action, extras=extras) - -def remove_field(key, value=None, replace=None, controller=None, action=None, extras=None): - """ - Remove a key from the current search 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. - """ + return _create_url_with_params(params=params, controller=controller, + action=action, extras=extras) + +def remove_field(key, value=None, replace=None, controller=None, action=None, + extras=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. + + controller action & extras (dict) are used to create the base url + via url_for(controller=controller, action=action, **extras) + controller & action default to the current ones + ''' params_nopage = [(k, v) for k,v in request.params.items() if k != 'page'] params = list(params_nopage) if value: @@ -914,7 +926,8 @@ def remove_field(key, value=None, replace=None, controller=None, action=None, ex [params.remove((k, v)) for (k, v) in params[:] if k==key] if replace is not None: params.append((key, replace)) - return _create_url_with_params(params=params, controller=controller, action=action, extras=extras) + return _create_url_with_params(params=params, controller=controller, + action=action, extras=extras) # these are the functions that will end up in `h` template helpers # if config option restrict_template_vars is true From 20eb92dc64a3e890d3e8fb0b58a605de17010cac Mon Sep 17 00:00:00 2001 From: Toby Date: Thu, 21 Jun 2012 17:30:10 +0100 Subject: [PATCH 3/4] [#2579] rename new helper functions --- ckan/controllers/group.py | 4 ++-- ckan/controllers/package.py | 4 ++-- ckan/lib/helpers.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index 595be4867cc..f7dd86d063b 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -147,7 +147,7 @@ def search_url(params): return url + u'?' + urlencode(params) def drill_down_url(**by): - return h.drill_down_url(alternative_url=None, + return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), **by) @@ -155,7 +155,7 @@ def drill_down_url(**by): c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): - return h.remove_field(key, value=value, replace=replace, + return h.remove_url_param(key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 5c06e783aae..3ee8eda313b 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -119,13 +119,13 @@ def search(self): params_nopage = [(k, v) for k,v in request.params.items() if k != 'page'] def drill_down_url(alternative_url=None, **by): - return h.drill_down_url(alternative_url=alternative_url, + return h.add_url_param(alternative_url=alternative_url, controller='package', action='search', **by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): - return h.remove_field(key, value=value, replace=replace, + return h.remove_url_param(key, value=value, replace=replace, controller='package', action='search') c.remove_field = remove_field diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 4d1621aa03a..7a09535095f 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -890,7 +890,7 @@ def url_with_params(url, params): url = url_for(controller=controller, action=action, **extras) return url_with_params(url, params) -def drill_down_url(alternative_url=None, controller=None, action=None, +def add_url_param(alternative_url=None, controller=None, action=None, extras=None, **fields): ''' Adds extra parameters to existing ones @@ -907,7 +907,7 @@ def drill_down_url(alternative_url=None, controller=None, action=None, return _create_url_with_params(params=params, controller=controller, action=action, extras=extras) -def remove_field(key, value=None, replace=None, controller=None, action=None, +def remove_url_param(key, value=None, replace=None, controller=None, action=None, extras=None): ''' Remove a key from the current parameters. A specific key/value pair can be removed by passing a second value argument otherwise all @@ -984,8 +984,8 @@ def remove_field(key, value=None, replace=None, controller=None, action=None, 'unselected_facet_items', 'follow_button', 'follow_count', - 'remove_field', - 'drill_down_url', + 'remove_url_param', + 'add_url_param', # imported into ckan.lib.helpers 'literal', 'link_to', From 858835c82b56ab55f14e70d0d47010c7eb06a0fa Mon Sep 17 00:00:00 2001 From: Toby Date: Thu, 21 Jun 2012 17:50:07 +0100 Subject: [PATCH 4/4] [#2579] some refactoring --- ckan/controllers/group.py | 6 +++--- ckan/controllers/package.py | 3 ++- ckan/lib/helpers.py | 9 +++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/ckan/controllers/group.py b/ckan/controllers/group.py index f7dd86d063b..d1e2b95cc3d 100644 --- a/ckan/controllers/group.py +++ b/ckan/controllers/group.py @@ -148,9 +148,9 @@ def search_url(params): def drill_down_url(**by): return h.add_url_param(alternative_url=None, - controller='group', action='read', - extras=dict(id=c.group_dict.get('name')), - **by) + controller='group', action='read', + extras=dict(id=c.group_dict.get('name')), + new_params=by) c.drill_down_url = drill_down_url diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index 3ee8eda313b..dffb2bece31 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -120,7 +120,8 @@ def search(self): def drill_down_url(alternative_url=None, **by): return h.add_url_param(alternative_url=alternative_url, - controller='package', action='search', **by) + controller='package', action='search', + new_params=by) c.drill_down_url = drill_down_url diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py index 7a09535095f..9ab19ac1e72 100644 --- a/ckan/lib/helpers.py +++ b/ckan/lib/helpers.py @@ -891,7 +891,7 @@ def url_with_params(url, params): return url_with_params(url, params) def add_url_param(alternative_url=None, controller=None, action=None, - extras=None, **fields): + extras=None, new_params=None): ''' Adds extra parameters to existing ones @@ -901,14 +901,15 @@ def add_url_param(alternative_url=None, controller=None, action=None, ''' params_nopage = [(k, v) for k,v in request.params.items() if k != 'page'] params = set(params_nopage) - params |= set(fields.items()) + if new_params: + params |= set(new_params.items()) if alternative_url: return url_with_params(alternative_url, params) 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): +def remove_url_param(key, value=None, replace=None, controller=None, + action=None, extras=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