Commit 6a46887a authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

ERP5Web: redirect to the minimum URL if verbose languages exist in URL.

parent 38a53420
...@@ -37,6 +37,7 @@ from Persistence import Persistent ...@@ -37,6 +37,7 @@ from Persistence import Persistent
from ZPublisher import BeforeTraverse from ZPublisher import BeforeTraverse
from ZPublisher.HTTPRequest import HTTPRequest from ZPublisher.HTTPRequest import HTTPRequest
from warnings import warn from warnings import warn
from zExceptions import Redirect
WEBSITE_KEY = 'web_site_value' WEBSITE_KEY = 'web_site_value'
WEBSITE_LANGUAGE_KEY = 'web_site_language' WEBSITE_LANGUAGE_KEY = 'web_site_language'
...@@ -171,9 +172,16 @@ class WebSite(WebSection): ...@@ -171,9 +172,16 @@ class WebSite(WebSection):
# as a language selection only if language_list # as a language selection only if language_list
# was defined or set default language # was defined or set default language
if name in language_list: if name in language_list:
default_language = self.getDefaultAvailableLanguage()
if request.get('AcceptLanguage') is not None: if request.get('AcceptLanguage') is not None:
request['AcceptLanguage'].set(name, 100) request['AcceptLanguage'].set(name, 100)
request.set(WEBSITE_LANGUAGE_KEY, name) request.set(WEBSITE_LANGUAGE_KEY, name)
if self.isTempObject() or name == default_language:
redirect_path_list = [self.getOriginalDocument().absolute_url()]
if name != default_language:
redirect_path_list.append(name)
redirect_path_list.extend(reversed(request['TraversalRequestNameStack']))
request['minimum_language_redirect_url'] = '/'.join(redirect_path_list)
return self.getOriginalDocument().asContext(id=name) return self.getOriginalDocument().asContext(id=name)
return WebSection.getExtensibleContent(self, request, name) return WebSection.getExtensibleContent(self, request, name)
...@@ -188,6 +196,12 @@ class WebSite(WebSection): ...@@ -188,6 +196,12 @@ class WebSite(WebSection):
def _getTraversalHookClass(self): def _getTraversalHookClass(self):
return WebSiteTraversalHook return WebSiteTraversalHook
def __before_publishing_traverse__(self, self2, request):
redirect_url = request.get('minimum_language_redirect_url')
if redirect_url:
raise Redirect(redirect_url)
return super(WebSite, self).__before_publishing_traverse__(self2, request)
security.declareProtected(Permissions.AccessContentsInformation, 'getPermanentURLList') security.declareProtected(Permissions.AccessContentsInformation, 'getPermanentURLList')
def getPermanentURLList(self, document): def getPermanentURLList(self, document):
""" """
......
...@@ -966,6 +966,48 @@ Hé Hé Hé!""", page.asText().strip()) ...@@ -966,6 +966,48 @@ Hé Hé Hé!""", page.asText().strip())
request['HTTP_REFERER'] = webpage_bg_fr.absolute_url() request['HTTP_REFERER'] = webpage_bg_fr.absolute_url()
self.assertEqual(webpage_bg_fr.Base_doLanguage('de'), webpage_bg_fr.absolute_url().replace('/bg/fr/', '/de/')) self.assertEqual(webpage_bg_fr.Base_doLanguage('de'), webpage_bg_fr.absolute_url().replace('/bg/fr/', '/de/'))
# /bg/en/fr/xxx should be redirected to /fr/xxx
website_bg_en_fr = self.portal.restrictedTraverse(
'web_site_module/%s/bg/en/fr' % website_id)
websection_bg_en_fr = self.portal.restrictedTraverse(
'web_site_module/%s/bg/en/fr/%s' % (website_id, websection_id))
webpage_bg_en_fr = self.portal.restrictedTraverse(
'web_site_module/%s/bg/en/fr/%s/%s' % (website_id, websection_id, page_ref))
self.assertEqual(self.publish(website_bg_en_fr.absolute_url(relative=1)).getHeader('location'),
website_fr.absolute_url())
self.assertEqual(self.publish(websection_bg_en_fr.absolute_url(relative=1)).getHeader('location'),
websection_fr.absolute_url())
self.assertEqual(self.publish(webpage_bg_en_fr.absolute_url(relative=1)).getHeader('location'),
webpage_fr.absolute_url())
# /bg/en/xxx should be redirected to /xxx where en is the default language
website_bg_en = self.portal.restrictedTraverse(
'web_site_module/%s/bg/en' % website_id)
websection_bg_en = self.portal.restrictedTraverse(
'web_site_module/%s/bg/en/%s' % (website_id, websection_id))
webpage_bg_en = self.portal.restrictedTraverse(
'web_site_module/%s/bg/en/%s/%s' % (website_id, websection_id, page_ref))
self.assertEqual(self.publish(website_bg_en.absolute_url(relative=1)).getHeader('location'),
website.absolute_url())
self.assertEqual(self.publish(websection_bg_en.absolute_url(relative=1)).getHeader('location'),
websection.absolute_url())
self.assertEqual(self.publish(webpage_bg_en.absolute_url(relative=1)).getHeader('location'),
webpage.absolute_url())
# /en/xxx should be redirected to /xxx where en is the default language
website_en = self.portal.restrictedTraverse(
'web_site_module/%s/en' % website_id)
websection_en = self.portal.restrictedTraverse(
'web_site_module/%s/en/%s' % (website_id, websection_id))
webpage_en = self.portal.restrictedTraverse(
'web_site_module/%s/en/%s/%s' % (website_id, websection_id, page_ref))
self.assertEqual(self.publish(website_en.absolute_url(relative=1)).getHeader('location'),
website.absolute_url())
self.assertEqual(self.publish(websection_en.absolute_url(relative=1)).getHeader('location'),
websection.absolute_url())
self.assertEqual(self.publish(webpage_en.absolute_url(relative=1)).getHeader('location'),
webpage.absolute_url())
def test_13_DocumentCache(self): def test_13_DocumentCache(self):
""" """
Test that when a document is modified, it can be accessed through a Test that when a document is modified, it can be accessed through a
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment