Repository: ckan/ckan

Issue:
API response help link is nonsense when root_path is set
### CKAN Version if known (or site URL)

2.8.2/master(2.9.0a0)

### Please describe the expected behaviour

help URL in API returns has root_path inside URL
http://domain.tld/data/api/3/help_show?name=package_list

### Please describe the actual behaviour

help URL has root_path prepended
/datahttp://domain.tld/api/3/help_show?name=package_list

### What steps can be taken to reproduce the issue? 

Install either 2.8.2 or master
configure with root_path = /{{LANG}}/data

$ paster serve /etc/ckan/default/development.ini &
$ curl http://localhost:5000/api/action/package_list


Discussion:
Hi @cdn, can you please provide more detailed step to reproduce the issue?
> Hi @cdn, can you please provide more detailed step to reproduce the issue?

Where would you like more detail?

Resolving patch:
diff --git a/ckan/lib/helpers.py b/ckan/lib/helpers.py
--- a/ckan/lib/helpers.py
+++ b/ckan/lib/helpers.py
@@ -505,7 +505,7 @@ def _local_url(url_to_amend, **kw):
             default_locale = True
 
     root = ''
-    if kw.get('qualified', False):
+    if kw.get('qualified', False) or kw.get('_external', False):
         # if qualified is given we want the full url ie http://...
         protocol, host = get_site_protocol_and_host()
         root = _routes_default_url_for('/',


Test patch:
diff --git a/ckan/tests/lib/test_helpers.py b/ckan/tests/lib/test_helpers.py
--- a/ckan/tests/lib/test_helpers.py
+++ b/ckan/tests/lib/test_helpers.py
@@ -226,18 +226,39 @@ def test_url_for_flask_route_new_syntax_external(self):
         generated_url = h.url_for('api.get_api', ver=3, _external=True)
         eq_(generated_url, url)
 
+    @helpers.change_config('ckan.site_url', 'http://example.com')
+    @helpers.change_config('ckan.root_path', '/{{LANG}}/data')
+    def test_url_for_flask_route_new_syntax_external_with_root_path(self):
+        url = 'http://example.com/data/api/3'
+        generated_url = h.url_for('api.get_api', ver=3, _external=True)
+        eq_(generated_url, url)
+
     @helpers.change_config('ckan.site_url', 'http://example.com')
     def test_url_for_flask_route_old_syntax_external(self):
         url = 'http://example.com/api/3'
         generated_url = h.url_for(controller='api', action='get_api', ver=3, _external=True)
         eq_(generated_url, url)
 
+    @helpers.change_config('ckan.site_url', 'http://example.com')
+    @helpers.change_config('ckan.root_path', '/{{LANG}}/data')
+    def test_url_for_flask_route_old_syntax_external_with_root_path(self):
+        url = 'http://example.com/data/api/3'
+        generated_url = h.url_for(controller='api', action='get_api', ver=3, _external=True)
+        eq_(generated_url, url)
+
     @helpers.change_config('ckan.site_url', 'http://example.com')
     def test_url_for_flask_route_old_syntax_qualified(self):
         url = 'http://example.com/api/3'
         generated_url = h.url_for(controller='api', action='get_api', ver=3, qualified=True)
         eq_(generated_url, url)
 
+    @helpers.change_config('ckan.site_url', 'http://example.com')
+    @helpers.change_config('ckan.root_path', '/{{LANG}}/data')
+    def test_url_for_flask_route_old_syntax_qualified_with_root_path(self):
+        url = 'http://example.com/data/api/3'
+        generated_url = h.url_for(controller='api', action='get_api', ver=3, qualified=True)
+        eq_(generated_url, url)
+
     @helpers.change_config('ckan.site_url', 'http://example.com')
     def test_url_for_flask_route_new_syntax_site_url(self):
         url = '/api/3'