Skip to content

Commit

Permalink
Fixed #5651 -- Correctly get language from request (#5664)
Browse files Browse the repository at this point in the history
If for some reason local variable language becomes empty string instead of None then treat is as None. (Duck typing)

Fixes #5651
  • Loading branch information
vstoykov authored and czpython committed Sep 7, 2016
1 parent f4c8448 commit 52d8099
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* Fixed a bug where moving a published page under a page marked as pending publishing
is left as published instead of being marked as pending publishing.
* Fixed AttributeError when using ``create_page`` in management command
* Fixed a bug in getting the language from current request which can cause error 500


=== 3.3.2 (2016-08-11) ===
Expand Down
38 changes: 37 additions & 1 deletion cms/tests/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from cms import api
from cms.test_utils.testcases import CMSTestCase
from cms.utils import i18n
from cms.utils import i18n, get_language_from_request


@override_settings(
Expand Down Expand Up @@ -371,3 +371,39 @@ def test_session_language(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/en/')


@override_settings(
LANGUAGE_CODE='en',
LANGUAGES=(('fr', 'French'),
('en', 'English'),
('de', 'German'),
('es', 'Spanish')),
CMS_LANGUAGES={
1: [{'code' : 'en',
'name': 'English',
'public': False},
{'code': 'fr',
'name': 'French',
'public': True},
],
'default': {
'fallbacks': ['en', 'fr'],
'redirect_on_fallback': False,
'public': True,
'hide_untranslated': False,
}
},
SITE_ID=1,
)
class TestGetLanguageFromRequest(CMSTestCase):

def test_get_language_from_request_does_not_return_empty_string_from_post(self):
request = self.get_request(language='en', post_data={
'language': '',
})
self.assertEqual(get_language_from_request(request), 'en')

def test_get_language_from_request_does_not_return_empty_string_from_get(self):
request = self.get_request('/en/?language=', language='en')
self.assertEqual(get_language_from_request(request), 'en')
6 changes: 3 additions & 3 deletions cms/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ def get_language_from_request(request, current_page=None):
language = get_language_code(language)
if not language in get_language_list(site_id):
language = None
if language is None:
if not language:
language = get_language_code(getattr(request, 'LANGUAGE_CODE', None))
if language:
if not language in get_language_list(site_id):
language = None

if language is None and current_page:
if not language and current_page:
# in last resort, get the first language available in the page
languages = current_page.get_languages()

if len(languages) > 0:
language = languages[0]

if language is None:
if not language:
# language must be defined in CMS_LANGUAGES, so check first if there
# is any language with LANGUAGE_CODE, otherwise try to split it and find
# best match
Expand Down

0 comments on commit 52d8099

Please sign in to comment.