Skip to content

Commit

Permalink
Merge pull request #4843 from ckan/4842-migrate-util-controller
Browse files Browse the repository at this point in the history
[#4842] Migrate util controller
  • Loading branch information
smotornyuk committed Jul 9, 2019
2 parents f376863 + c31c68d commit 78bcc94
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 47 deletions.
5 changes: 0 additions & 5 deletions ckan/config/routing.py
Expand Up @@ -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')

Expand Down
37 changes: 0 additions & 37 deletions ckan/controllers/util.py

This file was deleted.

2 changes: 1 addition & 1 deletion ckan/templates/snippets/language_selector.html
@@ -1,5 +1,5 @@
{% set current_lang = request.environ.CKAN_LANG %}
<form class="form-inline form-select lang-select" action="{% url_for controller='util', action='redirect' %}" data-module="select-switch" method="POST">
<form class="form-inline form-select lang-select" action="{% url_for 'util.internal_redirect' %}" data-module="select-switch" method="POST">
<label for="field-lang-select">{{ _('Language') }}</label>
<select id="field-lang-select" name="url" data-module="autocomplete" data-module-dropdown-class="lang-dropdown" data-module-container-class="lang-container">
{% for locale in h.get_available_locales() %}
Expand Down
8 changes: 4 additions & 4 deletions ckan/tests/controllers/test_util.py
Expand Up @@ -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,
)
Expand All @@ -21,23 +21,23 @@ 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,
)

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,
)

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,
)
38 changes: 38 additions & 0 deletions 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)

0 comments on commit 78bcc94

Please sign in to comment.