Skip to content

Commit

Permalink
Merge branch 'feature-2579-sort-functions-to-helpers' into feature-23…
Browse files Browse the repository at this point in the history
…75-demo-theme
  • Loading branch information
tobes committed Jun 21, 2012
2 parents 59f2d1a + 858835c commit 449bf03
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
11 changes: 6 additions & 5 deletions ckan/controllers/group.py
Expand Up @@ -147,16 +147,17 @@ def search_url(params):
return url + u'?' + urlencode(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)
return h.add_url_param(alternative_url=None,
controller='group', action='read',
extras=dict(id=c.group_dict.get('name')),
new_params=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',
id=c.group_dict.get('name'))
extras=dict(id=c.group_dict.get('name')))

c.remove_field = remove_field

Expand Down
7 changes: 4 additions & 3 deletions ckan/controllers/package.py
Expand Up @@ -119,13 +119,14 @@ 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,
controller='package', action='search', **by)
return h.add_url_param(alternative_url=alternative_url,
controller='package', action='search',
new_params=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
Expand Down
62 changes: 39 additions & 23 deletions ckan/lib/helpers.py
Expand Up @@ -1046,39 +1046,54 @@ 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):
''' 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)
return url + u'?' + urlencode(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:
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 add_url_param(alternative_url=None, controller=None, action=None,
extras=None, new_params=None):
'''
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())
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)

def remove_field(key, value=None, replace=None, controller=None, action=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_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
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:
Expand All @@ -1087,7 +1102,8 @@ 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)

def include_resource(resource):
r = getattr(html_resources, resource)
Expand Down Expand Up @@ -1202,8 +1218,8 @@ def popular(type_, number, min=1, title=None):
'sorted_extras',
'follow_button',
'follow_count',
'remove_field',
'drill_down_url',
'remove_url_param',
'add_url_param',
# imported into ckan.lib.helpers
'literal',
'link_to',
Expand Down

0 comments on commit 449bf03

Please sign in to comment.