Skip to content

Commit

Permalink
[#3022] Support alternative_url param on h.add/remove_url_param
Browse files Browse the repository at this point in the history
This allows custom URLs like the ones from dataset types. Private
function _url_with_params was duplicated on helpers.
  • Loading branch information
amercader committed Dec 5, 2012
1 parent 0f55922 commit 4ebfe69
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
17 changes: 8 additions & 9 deletions ckan/lib/helpers.py
Expand Up @@ -1021,16 +1021,10 @@ 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 with parameters. '''

def url_with_params(url, 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:
Expand All @@ -1039,7 +1033,7 @@ def url_with_params(url, params):
extras = {}

url = url_for(controller=controller, action=action, **extras)
return url_with_params(url, params)
return _url_with_params(url, params)

def add_url_param(alternative_url=None, controller=None, action=None,
extras=None, new_params=None):
Expand All @@ -1050,18 +1044,19 @@ def add_url_param(alternative_url=None, controller=None, action=None,
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)
if new_params:
params |= set(new_params.items())
if alternative_url:
return url_with_params(alternative_url, params)
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):
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
Expand All @@ -1079,6 +1074,10 @@ def remove_url_param(key, value=None, replace=None, controller=None,
[params.remove((k, v)) for (k, v) in params[:] if k == key]
if replace is not None:
params.append((key, replace))

if alternative_url:
return _url_with_params(alternative_url, params)

return _create_url_with_params(params=params, controller=controller,
action=action, extras=extras)

Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/snippets/facet_list.html
Expand Up @@ -33,7 +33,7 @@ <h2 class="module-heading"><i class="icon-medium icon-filter"></i> {{ h.get_face
<nav>
<ul class="unstyled nav nav-simple nav-facet">
{% for item in items %}
{% set href = h.remove_url_param(name, item.name, extras=extras) if item.active else h.add_url_param(new_params={name: item.name}, extras=extras) %}
{% set href = h.remove_url_param(name, item.name, extras=extras, alternative_url=alternative_url) if item.active else h.add_url_param(new_params={name: item.name}, extras=extras, alternative_url=alternative_url) %}
{% set label = label_function(item) if label_function else h.truncate(item.display_name, 22) %}
{% set count = count_label(item['count']) if count_label else ('(%d)' % item['count']) %}
<li class="nav-item{% if item.active %} active{% endif %}">
Expand Down

0 comments on commit 4ebfe69

Please sign in to comment.