Repository: ckan/ckan

Issue:
Migrate util controller
This was a left over from the core blueprints migration.


Discussion:

Resolving patch:
diff --git a/ckan/config/routing.py b/ckan/config/routing.py
--- a/ckan/config/routing.py
+++ b/ckan/config/routing.py
@@ -134,11 +134,6 @@ def make_map():
     # users
     map.redirect('/users/{url:.*}', '/user/{url}')
 
-    with SubMapper(map, controller='util') as m:
-        m.connect('/i18n/strings_{lang}.js', action='i18n_js_strings')
-        m.connect('/util/redirect', action='redirect')
-        m.connect('/testing/primer', action='primer')
-
     # robots.txt
     map.connect('/(robots.txt)', controller='template', action='view')
 
diff --git a/ckan/controllers/util.py b/ckan/controllers/util.py
deleted file mode 100644
--- a/ckan/controllers/util.py
+++ /dev/null
@@ -1,37 +0,0 @@
-# encoding: utf-8
-
-import re
-
-import ckan.lib.base as base
-import ckan.lib.i18n as i18n
-import ckan.lib.helpers as h
-from ckan.common import _
-
-
-class UtilController(base.BaseController):
-    ''' Controller for functionality that has no real home'''
-
-    def redirect(self):
-        ''' redirect to the url parameter. '''
-        url = base.request.params.get('url')
-        if not url:
-            base.abort(400, _('Missing Value') + ': url')
-
-        if h.url_is_local(url):
-            return h.redirect_to(url)
-        else:
-            base.abort(403, _('Redirecting to external site is not allowed.'))
-
-    def primer(self):
-        ''' Render all html components out onto a single page.
-        This is useful for development/styling of ckan. '''
-        return base.render('development/primer.html')
-
-    def i18_js_strings(self, lang):
-        ''' This is used to produce the translations for javascript. '''
-        i18n.set_lang(lang)
-        html = base.render('js_strings.html', cache_force=True)
-        html = re.sub(r'<[^\>]*>', '', html)
-        header = "text/javascript; charset=utf-8"
-        base.response.headers['Content-type'] = header
-        return html
diff --git a/ckan/views/util.py b/ckan/views/util.py
new file mode 100644
--- /dev/null
+++ b/ckan/views/util.py
@@ -0,0 +1,38 @@
+# encoding: utf-8
+
+import re
+
+from flask import Blueprint
+
+import ckan.lib.base as base
+import ckan.lib.helpers as h
+from ckan.common import _, request
+
+
+util = Blueprint(u'util', __name__)
+
+
+def internal_redirect():
+    u''' Redirect to the url parameter.
+    Only internal URLs are allowed'''
+
+    url = request.form.get(u'url') or request.args.get(u'url')
+    if not url:
+        base.abort(400, _(u'Missing Value') + u': url')
+
+    if h.url_is_local(url):
+        return h.redirect_to(url)
+    else:
+        base.abort(403, _(u'Redirecting to external site is not allowed.'))
+
+
+def primer():
+    u''' Render all HTML components out onto a single page.
+    This is useful for development/styling of CKAN. '''
+
+    return base.render(u'development/primer.html')
+
+
+util.add_url_rule(
+    u'/util/redirect', view_func=internal_redirect, methods=(u'GET', u'POST',))
+util.add_url_rule(u'/testing/primer', view_func=primer)


Test patch:
diff --git a/ckan/tests/controllers/test_util.py b/ckan/tests/controllers/test_util.py
--- a/ckan/tests/controllers/test_util.py
+++ b/ckan/tests/controllers/test_util.py
@@ -11,7 +11,7 @@ class TestUtil(helpers.FunctionalTestBase):
     def test_redirect_ok(self):
         app = self._get_test_app()
         response = app.get(
-            url=url_for(controller='util', action='redirect'),
+            url=url_for('util.internal_redirect'),
             params={'url': '/dataset'},
             status=302,
         )
@@ -21,7 +21,7 @@ def test_redirect_ok(self):
     def test_redirect_external(self):
         app = self._get_test_app()
         response = app.get(
-            url=url_for(controller='util', action='redirect'),
+            url=url_for('util.internal_redirect'),
             params={'url': 'http://nastysite.com'},
             status=403,
         )
@@ -29,7 +29,7 @@ def test_redirect_external(self):
     def test_redirect_no_params(self):
         app = self._get_test_app()
         response = app.get(
-            url=url_for(controller='util', action='redirect'),
+            url=url_for('util.internal_redirect'),
             params={},
             status=400,
         )
@@ -37,7 +37,7 @@ def test_redirect_no_params(self):
     def test_redirect_no_params_2(self):
         app = self._get_test_app()
         response = app.get(
-            url=url_for(controller='util', action='redirect'),
+            url=url_for('util.internal_redirect'),
             params={'url': ''},
             status=400,
         )