Commit 38a53420 authored by Kazuhiko Shiozaki's avatar Kazuhiko Shiozaki

fix Base_doLanguage in web mode.

* normalize URL in a case where we have several languages in URL.
* redirect to the web site root if HTTP_REFERER is not set.
parent 2b08800b
...@@ -174,7 +174,7 @@ class WebSite(WebSection): ...@@ -174,7 +174,7 @@ class WebSite(WebSection):
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)
return self.asContext(id=name) return self.getOriginalDocument().asContext(id=name)
return WebSection.getExtensibleContent(self, request, name) return WebSection.getExtensibleContent(self, request, name)
def _getExtensibleContent(self, request, name): def _getExtensibleContent(self, request, name):
......
import re
try: try:
website = context.getWebSiteValue() website = context.getWebSiteValue()
except AttributeError: except AttributeError:
...@@ -5,25 +7,24 @@ except AttributeError: ...@@ -5,25 +7,24 @@ except AttributeError:
if website is not None and website.isStaticLanguageSelection(): if website is not None and website.isStaticLanguageSelection():
# Web Mode # Web Mode
root_website = website.getOriginalDocument()
default_language = root_website.getDefaultAvailableLanguage()
root_website_url = root_website.absolute_url()
website_url_pattern = r'^%s(?:%s)*(/|$)' % (
re.escape(root_website_url),
'|'.join('/' + re.escape(x) for x in root_website.getAvailableLanguageList()))
referer_url = context.REQUEST.HTTP_REFERER referer_url = context.REQUEST.HTTP_REFERER
default_language = context.getDefaultAvailableLanguage() if referer_url:
current_language = context.Localizer.get_selected_language() if select_language == default_language:
web_site_url = context.getWebSiteValue().absolute_url() redirect_url = re.sub(website_url_pattern, r'%s\1' % root_website_url, referer_url)
if web_site_url.endswith('/%s' % current_language): else:
# Quick hack to handle acquisition of temp object redirect_url = re.sub(website_url_pattern, r'%s/%s\1' % (root_website_url, select_language),
# which is different in the case of a Web Site referer_url)
web_site_url = web_site_url[:-len(current_language) - 1]
if current_language == select_language:
redirect_url = referer_url
elif current_language == default_language:
redirect_url = referer_url.replace(web_site_url, '%s/%s' %
(web_site_url, select_language))
elif select_language == default_language:
redirect_url = referer_url.replace('%s/%s' % (web_site_url, current_language), web_site_url)
else: else:
redirect_url = referer_url.replace('%s/%s' % (web_site_url, current_language), if select_language == default_language:
'%s/%s' % (web_site_url, select_language)) redirect_url = root_website_url
else:
redirect_url = '%s/%s' % (root_website_url, select_language)
return context.REQUEST.RESPONSE.redirect(redirect_url) return context.REQUEST.RESPONSE.redirect(redirect_url)
else: else:
# ERP5 Mode # ERP5 Mode
......
...@@ -856,7 +856,7 @@ Hé Hé Hé!""", page.asText().strip()) ...@@ -856,7 +856,7 @@ Hé Hé Hé!""", page.asText().strip())
Test that getWebSiteValue() and getWebSectionValue() always Test that getWebSiteValue() and getWebSectionValue() always
include selected Language. include selected Language.
""" """
website_id = self.setupWebSite().getId() website_id = self.setupWebSite(default_available_language='en').getId()
website = self.portal.restrictedTraverse( website = self.portal.restrictedTraverse(
'web_site_module/%s' % website_id) 'web_site_module/%s' % website_id)
website_relative_url = website.absolute_url(relative=1) website_relative_url = website.absolute_url(relative=1)
...@@ -926,6 +926,46 @@ Hé Hé Hé!""", page.asText().strip()) ...@@ -926,6 +926,46 @@ Hé Hé Hé!""", page.asText().strip())
self.assertEqual(websection_relative_url_fr, self.assertEqual(websection_relative_url_fr,
webpage_module_fr.getWebSectionValue().absolute_url(relative=1)) webpage_module_fr.getWebSectionValue().absolute_url(relative=1))
# several languages in URL
website_bg_fr = self.portal.restrictedTraverse(
'web_site_module/%s/bg/fr' % website_id)
self.assertEqual(website_bg_fr.getOriginalDocument(), website)
websection_bg_fr = self.portal.restrictedTraverse(
'web_site_module/%s/bg/fr/%s' % (website_id, websection_id))
webpage_bg_fr = self.portal.restrictedTraverse(
'web_site_module/%s/bg/fr/%s/%s' % (website_id, websection_id, page_ref))
# change language without referer
request = self.portal.REQUEST
request['HTTP_REFERER'] = ''
website_absolute_url = website.absolute_url()
self.assertEqual(website_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
self.assertEqual(websection_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
self.assertEqual(webpage_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
self.assertEqual(website_fr.Base_doLanguage('en'), website_absolute_url)
self.assertEqual(websection_fr.Base_doLanguage('en'), website_absolute_url)
self.assertEqual(webpage_fr.Base_doLanguage('en'), website_absolute_url)
self.assertEqual(website_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
self.assertEqual(websection_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
self.assertEqual(webpage_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
self.assertEqual(website_bg_fr.Base_doLanguage('en'), website_absolute_url)
self.assertEqual(websection_bg_fr.Base_doLanguage('en'), website_absolute_url)
self.assertEqual(webpage_bg_fr.Base_doLanguage('en'), website_absolute_url)
# change language with referer
request['HTTP_REFERER'] = website_fr.absolute_url()
self.assertEqual(website_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
request['HTTP_REFERER'] = websection_fr.absolute_url()
self.assertEqual(websection_fr.Base_doLanguage('de'), websection_fr.absolute_url().replace('/fr/', '/de/'))
request['HTTP_REFERER'] = webpage_fr.absolute_url()
self.assertEqual(webpage_fr.Base_doLanguage('de'), webpage_fr.absolute_url().replace('/fr/', '/de/'))
request['HTTP_REFERER'] = website_bg_fr.absolute_url()
self.assertEqual(website_bg_fr.Base_doLanguage('de'), '%s/de' % website_absolute_url)
request['HTTP_REFERER'] = websection_bg_fr.absolute_url()
self.assertEqual(websection_bg_fr.Base_doLanguage('de'), websection_bg_fr.absolute_url().replace('/bg/fr/', '/de/'))
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/'))
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