Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b472767

Browse files
authored
Merge pull request #6414 from ckan/remove_url_param-on-current-page
h.remove_url_param fail with minimal set of params
2 parents aa30976 + 5c0bd2d commit b472767

5 files changed

Lines changed: 46 additions & 23 deletions

File tree

changes/6414.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h.remove_url_param fail with minimal set of params

ckan/lib/helpers.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,16 +1947,25 @@ def follow_count(obj_type, obj_id):
19471947
return logic.get_action(action)(context, {'id': obj_id})
19481948

19491949

1950-
def _create_url_with_params(params=None, controller=None, action=None,
1951-
extras=None):
1952-
''' internal function for building urls with parameters. '''
1950+
def _create_url_with_params(
1951+
params=None, controller=None, action=None, extras=None
1952+
):
1953+
"""internal function for building urls with parameters."""
1954+
if extras is None:
1955+
if not controller and not action:
1956+
# it's an url for the current page. Let's keep all interlal params,
1957+
# like <package_type>
1958+
extras = dict(request.view_args)
1959+
else:
1960+
extras = {}
1961+
1962+
blueprint, view = p.toolkit.get_endpoint()
19531963
if not controller:
1954-
controller = getattr(c, 'controller', False) or request.blueprint
1964+
controller = getattr(g, "controller", blueprint)
19551965
if not action:
1956-
action = getattr(c, 'action', False) or p.toolkit.get_endpoint()[1]
1957-
if not extras:
1958-
extras = {}
1959-
endpoint = controller + '.' + action
1966+
action = getattr(g, "action", view)
1967+
1968+
endpoint = controller + "." + action
19601969
url = url_for(endpoint, **extras)
19611970
return _url_with_params(url, params)
19621971

ckan/plugins/toolkit.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -522,21 +522,16 @@ def _requires_ckan_version(cls, min_version, max_version=None):
522522

523523
@classmethod
524524
def _get_endpoint(cls):
525-
"""Returns tuple in format: (controller|blueprint, action|view).
526-
"""
527-
import ckan.common as common
528-
try:
529-
# CKAN >= 2.8
530-
endpoint = tuple(common.request.endpoint.split('.'))
531-
except AttributeError:
532-
try:
533-
return common.c.controller, common.c.action
534-
except AttributeError:
535-
return (None, None)
525+
"""Returns tuple in format: (blueprint, view)."""
526+
from ckan.common import request
527+
528+
if not request:
529+
return None, None
530+
531+
blueprint, *rest = request.endpoint.split(".", 1)
536532
# service routes, like `static`
537-
if len(endpoint) == 1:
538-
return endpoint + ('index', )
539-
return endpoint
533+
view = rest[0] if rest else "index"
534+
return blueprint, view
540535

541536
def __getattr__(self, name):
542537
''' return the function/object requested '''

ckan/tests/lib/test_helpers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,15 @@ def test_escaping(self):
743743
assert str(html).startswith(u'<option value="&#34;&gt;" >')
744744

745745

746-
class TestAddUrlParam(object):
746+
class TestRemoveUrlParam:
747+
def test_current_url(self, test_request_context):
748+
base = "/organization/name"
749+
with test_request_context(base + "?q=search"):
750+
assert h.remove_url_param("q") == base
751+
assert h.remove_url_param("q", replace="test") == base + "?q=test"
752+
747753

754+
class TestAddUrlParam(object):
748755
@pytest.mark.parametrize(u'url,params,expected', [
749756
(u'/dataset', {u'a': u'2'}, u'/dataset/?a=2'),
750757
(u'/dataset?a=1', {u'a': u'2'}, u'/dataset/?a=1&a=2'),

ckan/tests/plugins/test_toolkit.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,14 @@ def test_tk_helper_as_item_missing_helper():
107107
"""Directly attempt access as item"""
108108
with pytest.raises(tk.HelperError):
109109
tk.h[u"nothere"]()
110+
111+
112+
def test_get_endpoint_without_context():
113+
"""Do not fail in CLI and tests."""
114+
assert tk.get_endpoint() == (None, None)
115+
116+
117+
@pytest.mark.usefixtures("with_request_context")
118+
def test_get_endpoint_witho_context():
119+
"""with_request_context fixture mocks request to the homepage."""
120+
assert tk.get_endpoint() == ("home", "index")

0 commit comments

Comments
 (0)