Permalink
Browse files

Generalized language middleware exceptions. Issue #878.

  • Loading branch information...
1 parent 32239cb commit 625e8de8b4a4c390c88c37c42c2df6884738045d @mitar committed Jul 14, 2012
Showing with 35 additions and 0 deletions.
  1. +11 −0 cms/middleware/multilingual.py
  2. +11 −0 docs/advanced/i18n.rst
  3. +13 −0 docs/getting_started/configuration.rst
@@ -9,6 +9,7 @@
import urlparse
SUPPORTED = dict(settings.CMS_LANGUAGES)
+CMS_LANGUAGES_URL_IGNORE_PREFIXES = getattr(settings, 'CMS_LANGUAGES_URL_IGNORE_PREFIXES', ())
HAS_LANG_PREFIX_RE = re.compile(r"^/(%s)/.*" % "|".join([re.escape(l[0]) for l in settings.CMS_LANGUAGES]))
@@ -19,6 +20,12 @@ def has_lang_prefix(path):
else:
return False
+def starts_with_ignore_prefix(path):
+ for p in CMS_LANGUAGES_URL_IGNORE_PREFIXES:
+ if path.startswith(p):
+ return True
+ return False
+
def patch_response(content, pages_root, language):
# Customarily user pages are served from http://the.server.com/~username/
# When a user uses django-cms for his pages, the '~' of the url appears quoted in href links.
@@ -40,6 +47,7 @@ def patch_response(content, pages_root, language):
quoted_root = urllib.quote(pages_root)
ignore_paths = ['%s%s/' % (quoted_root, l[0]) for l in settings.CMS_LANGUAGES]
ignore_paths += [settings.MEDIA_URL, settings.STATIC_URL]
+ ignore_paths += CMS_LANGUAGES_URL_IGNORE_PREFIXES
if getattr(settings,'STATIC_URL', False):
ignore_paths += [settings.STATIC_URL]
@@ -55,6 +63,7 @@ def patch_response(content, pages_root, language):
ignore_paths = ['%s%s/' % (pages_root, l[0]) for l in settings.CMS_LANGUAGES]
ignore_paths += [settings.MEDIA_URL, settings.STATIC_URL]
+ ignore_paths += CMS_LANGUAGES_URL_IGNORE_PREFIXES
if getattr(settings,'STATIC_URL', False):
ignore_paths += [settings.STATIC_URL]
FORM_URL_FIX_RE = re.compile(ur'<form([^>]+)action=("|\')(?=%s)(?!(%s))(%s(.*?))("|\')(.*?)>' % (
@@ -112,6 +121,7 @@ def process_response(self, request, response):
if not path.startswith(settings.MEDIA_URL) and \
not path.startswith(settings.STATIC_URL) and \
+ not starts_with_ignore_prefix(path) and \
not (getattr(settings,'STATIC_URL', False) and path.startswith(settings.STATIC_URL)) and \
response.status_code == 200 and \
response._headers['content-type'][1].split(';')[0] == "text/html":
@@ -134,6 +144,7 @@ def process_response(self, request, response):
response['Location'] = location
if not has_lang_prefix(location) and location.startswith("/") and \
not location.startswith(settings.MEDIA_URL) and \
+ not starts_with_ignore_prefix(location) and \
not (getattr(settings,'STATIC_URL', False) and location.startswith(settings.STATIC_URL)):
response['Location'] = "/%s%s" % (language, location)
response.set_cookie("django_language", language)
View
@@ -135,3 +135,14 @@ If :setting:`CMS_HIDE_UNTRANSLATED` is ``True`` in your ``settings.py``
and you are on a page that doesn't yet have an English translatio and you view the
German version then the language chooser will redirect to ``/``. The same goes
for urls that are not handled by the cms and display a language chooser.
+
+
+*********************************
+CMS_LANGUAGES_URL_IGNORE_PREFIXES
+*********************************
+
+In complex Django CMS installations there could be URLs on the same site as
+Django CMS but which are processed by your HTTP server in some other manner and
+never passed to Django CMS. Multilingual middleware would process also links to
+such URLs, so you can use :setting:`CMS_LANGUAGES_URL_IGNORE_PREFIXES` to
+configure which URLs should be left as they are.
@@ -263,6 +263,19 @@ Example::
'en': ['de'],
}
+.. setting:: CMS_LANGUAGES_URL_IGNORE_PREFIXES
+
+CMS_LANGUAGES_URL_IGNORE_PREFIXES
+=================================
+
+Default: ``()``
+
+In complex Django CMS installations there could be URLs on the same site as
+Django CMS but which are processed by your HTTP server in some other manner and
+never passed to Django CMS. Multilingual middleware would process also links to
+such URLs, so you can use this setting to configure which URLs should be left
+as they are.
+
.. setting:: CMS_SITE_LANGUAGES
CMS_SITE_LANGUAGES

0 comments on commit 625e8de

Please sign in to comment.