Skip to content

Commit

Permalink
[release-v1.6][#1791][controllers]: Fix locale change exception on /d…
Browse files Browse the repository at this point in the history
…ataset/new page by filtering out the offending __cache parameter.
  • Loading branch information
David Read committed Feb 13, 2012
1 parent abff118 commit beaeaed
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
4 changes: 3 additions & 1 deletion ckan/controllers/home.py
Expand Up @@ -15,6 +15,8 @@
from ckan.lib.hash import get_redirect
from ckan.lib.helpers import url_for

CACHE_PARAMETER = '__cache'

class HomeController(BaseController):
repo = model.repo

Expand Down Expand Up @@ -116,7 +118,7 @@ def locale(self):
return
return_to += '&' if '?' in return_to else '?'
# hack to prevent next page being cached
return_to += '__cache=%s' % int(random.random()*100000000)
return_to += '%s=%s' % (CACHE_PARAMETER, int(random.random()*100000000))
redirect_to(return_to)

def cache(self, id):
Expand Down
4 changes: 3 additions & 1 deletion ckan/controllers/package.py
Expand Up @@ -30,6 +30,7 @@
import ckan.rating
import ckan.misc
import ckan.logic.action.get
from home import CACHE_PARAMETER

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -456,7 +457,8 @@ def new(self, data=None, errors=None, error_summary=None):
if context['save'] and not data:
return self._save_new(context)

data = data or clean_dict(unflatten(tuplize_dict(parse_params(request.params))))
data = data or clean_dict(unflatten(tuplize_dict(parse_params(
request.params, ignore_keys=[CACHE_PARAMETER]))))

errors = errors or {}
error_summary = error_summary or {}
Expand Down
15 changes: 12 additions & 3 deletions ckan/logic/__init__.py
Expand Up @@ -40,12 +40,19 @@ def __init__(self, error_dict, error_summary=None, extra_msg=None):

log = logging.getLogger(__name__)

def parse_params(params):
def parse_params(params, ignore_keys=None):
'''Takes a dict and returns it with some values standardised.
This is done on a dict before calling tuplize_dict on it.
'''
parsed = {}
for key in params:
if ignore_keys and key in ignore_keys:
continue
value = params.getall(key)
# Blank values become ''
if not value:
value = ''
# A list with only one item is stripped of being a list
if len(value) == 1:
value = value[0]
parsed[key] = value
Expand Down Expand Up @@ -87,12 +94,14 @@ def clean_dict(data_dict):
return data_dict

def tuplize_dict(data_dict):
''' gets a dict with keys of the form 'table__0__key' and converts them
'''Takes a dict with keys of the form 'table__0__key' and converts them
to a tuple like ('table', 0, 'key').
Dict should be put through parse_dict before this function, to have
values standardized.
May raise a DataError if the format of the key is incorrect.
'''

tuplized_dict = {}
for key, value in data_dict.iteritems():
key_list = key.split('__')
Expand Down
15 changes: 15 additions & 0 deletions ckan/tests/functional/test_package.py
Expand Up @@ -17,6 +17,7 @@
from ckan.lib.create_test_data import CreateTestData
import ckan.lib.helpers as h
import ckan.lib.search as search
from ckan.lib.i18n import set_session_locale
from ckan.logic.action import get, update
from ckan.controllers.package import PackageController
from ckan.plugins import SingletonPlugin, implements, IPackageController
Expand Down Expand Up @@ -1301,6 +1302,20 @@ def test_new_indexerror(self):
plugins.unload('synchronous_search')
SolrSettings.init(solr_url)

def test_change_locale(self):
offset = url_for(controller='package', action='new')
res = self.app.get(offset)
res = res.click('Deutsch')
try:
res = res.follow()
assert 'Datensatz' in res.body, res.body


finally:
# revert back to English for the other tests
res = self.app.get(offset)
res = res.click('English')

class TestSearch(TestPackageForm):
pkg_names = []

Expand Down

0 comments on commit beaeaed

Please sign in to comment.