[views] Properly build CKAN URL in goto-ckan-dataset component
authorDenis Laxalde <denis.laxalde@logilab.fr>
Thu, 18 Dec 2014 14:26:21 +0100
changeset 48 9534a4c8d423
parent 46 cb011e37fd3c
child 49 093dc7af8530
[views] Properly build CKAN URL in goto-ckan-dataset component Factor out the URL building step from ckan_post function and use it in the latter component.
utils.py
views.py
--- a/utils.py	Thu Dec 18 14:03:55 2014 +0100
+++ b/utils.py	Thu Dec 18 14:26:21 2014 +0100
@@ -27,11 +27,17 @@
     """CKAN post action error"""
 
 
-def ckan_post(config, action, data=None, files=None):
+def ckan_buildurl(config, path):
+    """Return a full URL in the CKAN instance built from path"""
     base = config['ckan-baseurl']
     if not base.endswith('/'):
         base += '/'
-    url = urljoin(base, 'api/3/action/' + action)
+    return urljoin(base, path)
+
+
+def ckan_post(config, action, data=None, files=None):
+    """Post an API request for `action` in the CKAN instance"""
+    url = ckan_buildurl(config, 'api/3/action/' + action)
     headers = {'Authorization': config['ckan-api-key']}
     if files is None:
         data = json.dumps(data or {})
--- a/views.py	Thu Dec 18 14:03:55 2014 +0100
+++ b/views.py	Thu Dec 18 14:26:21 2014 +0100
@@ -16,14 +16,12 @@
 
 """cubicweb-ckanpublish views/forms/actions/components for web ui"""
 
-from urlparse import urljoin
-
 from logilab.mtconverter import xml_escape
 
 from cubicweb.predicates import score_entity
 from cubicweb.web import component
 
-from cubes.ckanpublish.utils import ckan_post
+from cubes.ckanpublish.utils import ckan_post, ckan_buildurl
 
 
 _ = unicode
@@ -55,7 +53,6 @@
 
     def render_body(self, w):
         r = self._ckan_response
-        config = self._cw.vreg.config
-        base = config['ckan-baseurl']
-        url = urljoin(base, 'dataset/' + xml_escape(r['name']))
+        url = ckan_buildurl(self._cw.vreg.config,
+                            'dataset/' + xml_escape(r['name']))
         w(u'<a target=_blank href="%s">%s<a/>' % (url, xml_escape(url)))