Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

frontend langs working

  • Loading branch information...
commit b6fde0af63969b27feb704dca175f89229f21f15 1 parent 5806d99
@digi604 digi604 authored
View
5 cms/test_utils/cli.py
@@ -111,11 +111,6 @@ def configure(**extra):
('pt-br', gettext('Brazilian Portuguese')),
('nl', gettext("Dutch")),
),
- CMS_FRONTEND_LANGUAGES = (
- 'fr',
- 'de',
- 'nl',
- ),
CMS_LANGUAGE_CONF = {
'de':['fr', 'en'],
'en':['fr', 'de'],
View
31 cms/tests/menu.py
@@ -267,21 +267,22 @@ def test_show_breadcrumb(self):
self.assertEqual(nodes[1].get_absolute_url(), page2.get_absolute_url())
def test_language_chooser(self):
- # test simple language chooser with default args
- context = self.get_context(path=self.get_page(3).get_absolute_url())
- tpl = Template("{% load menu_tags %}{% language_chooser %}")
- tpl.render(context)
- self.assertEqual(len(context['languages']), 2)
- # try a different template and some different args
- tpl = Template("{% load menu_tags %}{% language_chooser 'menu/test_language_chooser.html' %}")
- tpl.render(context)
- self.assertEqual(context['template'], 'menu/test_language_chooser.html')
- tpl = Template("{% load menu_tags %}{% language_chooser 'short' 'menu/test_language_chooser.html' %}")
- tpl.render(context)
- self.assertEqual(context['template'], 'menu/test_language_chooser.html')
- for lang in context['languages']:
- self.assertEqual(*lang)
-
+ # test simple language chooser with default args
+ with SettingsOverride(CMS_FRONTEND_LANGUAGES = ('fr','de','nl')):
+ context = self.get_context(path=self.get_page(3).get_absolute_url())
+ tpl = Template("{% load menu_tags %}{% language_chooser %}")
+ tpl.render(context)
+ self.assertEqual(len(context['languages']), 2)
+ # try a different template and some different args
+ tpl = Template("{% load menu_tags %}{% language_chooser 'menu/test_language_chooser.html' %}")
+ tpl.render(context)
+ self.assertEqual(context['template'], 'menu/test_language_chooser.html')
+ tpl = Template("{% load menu_tags %}{% language_chooser 'short' 'menu/test_language_chooser.html' %}")
+ tpl.render(context)
+ self.assertEqual(context['template'], 'menu/test_language_chooser.html')
+ for lang in context['languages']:
+ self.assertEqual(*lang)
+
def test_page_language_url(self):
path = self.get_page(3).get_absolute_url()
context = self.get_context(path=path)
View
93 cms/tests/multilingual.py
@@ -1,10 +1,22 @@
# -*- coding: utf-8 -*-
+from __future__ import with_statement
from cms.api import create_page, create_title, publish_page, add_plugin
-from cms.test_utils.testcases import CMSTestCase
+from cms.test_utils.testcases import SettingsOverrideTestCase
+from cms.test_utils.util.context_managers import SettingsOverride
+from cms.test_utils.util.mock import AttributeObject
from django.contrib.auth.models import User
+from django.http import Http404, HttpResponseRedirect
+TEMPLATE_NAME = 'tests/rendering/base.html'
+
+
+class MultilingualTestCase(SettingsOverrideTestCase):
+ settings_overrides = {
+ 'CMS_TEMPLATES': [(TEMPLATE_NAME, TEMPLATE_NAME), ('extra_context.html', 'extra_context.html'),
+ ('nav_playground.html', 'nav_playground.html')],
+ 'CMS_MODERATOR': False,
+ }
-class MultilingualTestCase(CMSTestCase):
def test_multilingual_page(self):
page = create_page("mlpage", "nav_playground.html", "en")
@@ -24,5 +36,80 @@ def test_multilingual_page(self):
self.assertEqual(placeholder.cmsplugin_set.filter(language='en').count(), 1)
def test_frontend_lang(self):
- pass
+ with SettingsOverride(CMS_FRONTEND_LANGUAGES=('fr', 'de', 'nl')):
+ page = create_page("page1", "nav_playground.html", "en")
+ create_title("de", page.get_title(), page, slug=page.get_slug())
+ page2 = create_page("page2", "nav_playground.html", "en")
+ create_title("de", page2.get_title(), page2, slug=page2.get_slug())
+ page3 = create_page("page2", "nav_playground.html", "en")
+ create_title("de", page3.get_title(), page3, slug=page3.get_slug())
+ page.publish()
+ page2.publish()
+ page3.publish()
+ response = self.client.get("/en/")
+ self.assertRedirects(response, "/de/")
+ response = self.client.get("/en/page2/")
+ self.assertEqual(response.status_code, 404)
+ response = self.client.get("/de/")
+ self.assertEqual(response.status_code, 200)
+ response = self.client.get("/de/page2/")
+ self.assertEqual(response.status_code, 200)
+
+ def test_detail_view_404_when_no_language_is_found(self):
+ page = create_page("page1", "nav_playground.html", "en")
+ create_title("de", page.get_title(), page, slug=page.get_slug())
+ page.publish()
+
+ with SettingsOverride(TEMPLATE_CONTEXT_PROCESSORS=[],
+ CMS_LANGUAGES=[
+ ('x-klingon', 'Klingon'),
+ ('x-elvish', 'Elvish')
+ ], CMS_FRONTEND_LANGUAGES=('x-klingon', 'x-elvish')):
+ from cms.views import details
+
+ request = AttributeObject(
+ REQUEST={'language': 'x-elvish'},
+ GET=[],
+ session={},
+ path='/',
+ current_page=None,
+ method='GET',
+ COOKIES={},
+ META={},
+ )
+ self.assertRaises(Http404, details, request, '')
+
+ def test_detail_view_fallback_language(self):
+ '''
+ Ask for a page in elvish (doesn't exist), and assert that it fallsback
+ to English
+ '''
+ page = create_page("page1", "nav_playground.html", "en")
+ with SettingsOverride(TEMPLATE_CONTEXT_PROCESSORS=[],
+ CMS_LANGUAGE_CONF={
+ 'x-elvish': ['x-klingon', 'en', ]
+ },
+ CMS_LANGUAGES=[
+ ('x-klingon', 'Klingon'),
+ ('x-elvish', 'Elvish'),
+ ],
+ CMS_FRONTEND_LANGUAGES=('x-klingon', 'x-elvish')):
+ create_title("x-klingon", "futla ak", page, slug=page.get_slug())
+ page.publish()
+ from cms.views import details
+
+ request = AttributeObject(
+ REQUEST={'language': 'x-elvish'},
+ GET=[],
+ session={},
+ path='/',
+ current_page=None,
+ method='GET',
+ COOKIES={},
+ META={},
+ )
+
+ response = details(request, '')
+ self.assertTrue(isinstance(response, HttpResponseRedirect))
+
View
45 cms/tests/rendering.py
@@ -288,52 +288,7 @@ def test_inherit_placeholder(self):
u'|{% placeholder "main" inherit %}|{% placeholder "sub" %}'
r = self.render(t, self.test_page3)
self.assertEqual(r, u'|'+self.test_data['text_main']+'|'+self.test_data3['text_sub'])
-
- def test_detail_view_404_when_no_language_is_found(self):
- with SettingsOverride(TEMPLATE_CONTEXT_PROCESSORS=[],
- CMS_LANGUAGES=[
- ('x-klingon', 'Klingon'),
- ('x-elvish', 'Elvish')
- ]):
- from cms.views import details
- request = AttributeObject(
- REQUEST={'language': 'x-elvish'},
- GET=[],
- session={},
- path='/',
- user=self.test_user,
- current_page=None,
- method='GET',
- )
- self.assertRaises(Http404, details, request, '')
- def test_detail_view_fallback_language(self):
- '''
- Ask for a page in elvish (doesn't exist), and assert that it fallsback
- to English
- '''
- with SettingsOverride(TEMPLATE_CONTEXT_PROCESSORS=[],
- CMS_LANGUAGE_CONF={
- 'x-elvish': ['x-klingon', 'en',]
- },
- CMS_LANGUAGES=[
- ('x-klingon', 'Klingon'),
- ('x-elvish', 'Elvish'),
- ]):
- from cms.views import details
- request = AttributeObject(
- REQUEST={'language': 'x-elvish'},
- GET=[],
- session={},
- path='/',
- user=self.test_user,
- current_page=None,
- method='GET',
- )
-
- response = details(request, '')
- self.assertTrue(isinstance(response,HttpResponseRedirect))
-
def test_extra_context_isolation(self):
with ChangeModel(self.test_page, template='extra_context.html'):
response = self.client.get(self.test_page.get_absolute_url())
View
3  cms/utils/__init__.py
@@ -54,8 +54,7 @@ def get_language_from_request(request, current_page=None):
if not language in dict(settings.CMS_LANGUAGES).keys():
language = None
- # TODO: This smells like a refactoring oversight - was current_page ever a page object? It appears to be a string now
- if language is None and isinstance(current_page, Page):
+ if language is None and current_page:
# in last resort, get the first language available in the page
languages = current_page.get_languages()
View
52 cms/views.py
@@ -5,13 +5,14 @@
from cms.utils import get_template_from_request, get_language_from_request
from cms.utils.i18n import get_fallback_languages, force_language
from cms.utils.page_resolver import get_page_from_request
+from cms.test_utils.util.context_managers import SettingsOverride
from django.conf import settings
from django.conf.urls.defaults import patterns
-from django.core.urlresolvers import resolve, Resolver404
-
+from django.core.urlresolvers import resolve, Resolver404, reverse
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
+from django.utils import translation
from django.utils.http import urlquote
def _handle_no_page(request, slug):
@@ -31,11 +32,34 @@ def details(request, slug):
page = get_page_from_request(request, use_path=slug)
if not page:
return _handle_no_page(request, slug)
-
+
current_language = get_language_from_request(request)
# Check that the current page is available in the desired (current) language
- available_languages = page.get_languages()
-
+
+ available_languages = []
+ page_languages = page.get_languages()
+ for frontend_lang in settings.CMS_FRONTEND_LANGUAGES:
+ if frontend_lang in page_languages:
+ available_languages.append(frontend_lang)
+ # Check that the language is in FRONTEND_LANGUAGES:
+ if not current_language in settings.CMS_FRONTEND_LANGUAGES:
+ #are we on root?
+ if not slug:
+ #redirect to supported language
+ languages = []
+ for language in available_languages:
+ languages.append((language, language))
+ if languages:
+ with SettingsOverride(LANGUAGES=languages, LANGUAGE_CODE=languages[0][0]):
+ #get supported language
+ new_language = translation.get_language_from_request(request)
+ with force_language(new_language):
+ pages_root = reverse('pages-root')
+ return HttpResponseRedirect(pages_root)
+ else:
+ _handle_no_page(request, slug)
+ else:
+ return _handle_no_page(request, slug)
# We resolve an alternate language for the page if it's not available.
# Since the "old" details view had an exception for the root page, it is
# ported here. So no resolution if the slug is ''.
@@ -48,11 +72,11 @@ def details(request, slug):
if alt_lang in available_languages:
with force_language(alt_lang):
path = page.get_absolute_url(language=alt_lang, fallback=True)
- # In the case where the page is not available in the
+ # In the case where the page is not available in the
# preferred language, *redirect* to the fallback page. This
# is a design decision (instead of rendering in place)).
return HttpResponseRedirect(path)
- # There is a page object we can't find a proper language to render it
+ # There is a page object we can't find a proper language to render it
_handle_no_page(request, slug)
if apphook_pool.get_apphooks():
@@ -75,14 +99,14 @@ def details(request, slug):
return view(request, *args, **kwargs)
except Resolver404:
pass
- # Check if the page has a redirect url defined for this language.
+ # Check if the page has a redirect url defined for this language.
redirect_url = page.get_redirect(language=current_language)
if redirect_url:
if (settings.USE_I18N and redirect_url[0] == "/"
and not redirect_url.startswith('/%s/' % current_language)):
# add language prefix to url
redirect_url = "/%s/%s" % (current_language, redirect_url.lstrip("/"))
- # prevent redirect to self
+ # prevent redirect to self
own_urls = [
'http%s://%s%s' % ('s' if request.is_secure() else '', request.get_host(), request.path),
'/%s' % request.path,
@@ -90,21 +114,21 @@ def details(request, slug):
]
if redirect_url not in own_urls:
return HttpResponseRedirect(redirect_url)
-
+
# permission checks
if page.login_required and not request.user.is_authenticated():
path = urlquote(request.get_full_path())
- tup = settings.LOGIN_URL , "next", path
+ tup = settings.LOGIN_URL, "next", path
return HttpResponseRedirect('%s?%s=%s' % tup)
-
+
template_name = get_template_from_request(request, page, no_current_page=True)
# fill the context
context['lang'] = current_language
context['current_page'] = page
context['has_change_permissions'] = page.has_change_permission(request)
context['has_view_permissions'] = page.has_view_permission(request)
-
+
if not context['has_view_permissions']:
return _handle_no_page(request, slug)
-
+
return render_to_response(template_name, context_instance=context)
View
7 menus/templatetags/menu_tags.py
@@ -322,17 +322,14 @@ def get_context(self, context, template, i18n_mode):
current_lang = get_language()
site = Site.objects.get_current()
site_languages = settings.CMS_SITE_LANGUAGES.get(site.pk, cms_languages.keys())
- cache_key = '%s-language-chooser-%s-%s-%s' % (settings.CMS_CACHE_PREFIX, site.pk, current_lang, i18n_mode)
- languages = cache.get(cache_key, [])
+ languages = []
if not languages:
for lang in settings.CMS_FRONTEND_LANGUAGES:
if lang in cms_languages and lang in site_languages:
languages.append((lang, marker(cms_languages[lang], lang)))
- cache.set(cache_key, languages)
- lang = get_language()
context.update({
'languages':languages,
- 'current_language':lang,
+ 'current_language':current_lang,
'template':template,
})
return context
Please sign in to comment.
Something went wrong with that request. Please try again.