Permalink
Browse files

Add possibility to activate localized url just for subsections (domai…

…n or urlconf) in case of use of a multihosted project
  • Loading branch information...
1 parent 3189f75 commit 0b1e5530d87556bbd2672a50bedeae6e9ccf9a90 Mickaël Hoareau committed Apr 5, 2012
Showing with 52 additions and 14 deletions.
  1. +3 −1 localeurl/middleware.py
  2. +9 −2 localeurl/models.py
  3. +4 −0 localeurl/settings.py
  4. +36 −11 localeurl/utils.py
View
@@ -37,6 +37,8 @@ def __init__(self):
raise django.core.exceptions.MiddlewareNotUsed()
def process_request(self, request):
+ hostname = request.get_host()
+
locale, path = utils.strip_path(request.path_info)
if localeurl_settings.USE_SESSION and not locale:
slocale = request.session.get('django_language')
@@ -51,7 +53,7 @@ def process_request(self, request):
)
if accept_langs:
locale = accept_langs[0]
- locale_path = utils.locale_path(path, locale)
+ locale_path = utils.locale_path(path, locale, host = hostname)
# locale case might be different in the two paths, that doesn't require
# a redirect (besides locale they'll be identical anyway)
if locale_path.lower() != request.path_info.lower():
View
@@ -2,14 +2,21 @@
from django.core import urlresolvers
from django.utils import translation
from localeurl import utils
+from django.core.urlresolvers import get_urlconf
def reverse(*args, **kwargs):
reverse_kwargs = kwargs.get('kwargs') or {}
+ prefix = kwargs.get('prefix') or None
+ urlconf = kwargs.get('urlconf') or None
+
+ if urlconf is None:
+ urlconf = get_urlconf()
+
locale = utils.supported_language(reverse_kwargs.pop(
'locale', translation.get_language()))
url = django_reverse(*args, **kwargs)
- _, path = utils.strip_script_prefix(url)
- return utils.locale_url(path, locale)
+ stipped_prefix, path = utils.strip_script_prefix(url, prefix = prefix)
+ return utils.locale_url(path, locale, prefix = stipped_prefix, urlconf=urlconf)
django_reverse = None
View
@@ -24,3 +24,7 @@
USE_SESSION = getattr(settings, 'LOCALEURL_USE_SESSION', False)
LOCALE_REDIRECT_PERMANENT = getattr(settings, 'LOCALE_REDIRECT_PERMANENT', True)
+
+LOCALE_DEPENDENT_HOSTS = getattr(settings, 'LOCALE_DEPENDENT_HOSTS', [])
+
+LOCALE_DEPENDENT_URLCONFS = getattr(settings, 'LOCALE_DEPENDENT_URLCONFS', [])
View
@@ -1,6 +1,7 @@
from django.conf import settings
from django.core import urlresolvers
from localeurl import settings as localeurl_settings
+from django.core.urlresolvers import get_urlconf
def is_locale_independent(path):
"""
@@ -19,6 +20,24 @@ def is_locale_independent(path):
return True
return False
+def is_host_independent(host):
+ """
+ Returns whether the host is locale-independent.
+ """
+ deactivated = True
+ if host in localeurl_settings.LOCALE_DEPENDENT_HOSTS:
+ deactivated = False
+ return deactivated
+
+def is_urlconf_independent(urlconf):
+ """
+ Returns whether the urlconf is locale-independent.
+ """
+ deactivated = True
+ if urlconf in localeurl_settings.LOCALE_DEPENDENT_URLCONFS:
+ deactivated = False
+ return deactivated
+
def strip_path(path):
"""
Separates the locale prefix from the rest of the path. If the path does not
@@ -49,42 +68,48 @@ def is_default_locale(locale):
"""
return locale == supported_language(settings.LANGUAGE_CODE)
-def locale_path(path, locale=''):
+def locale_path(path, locale='', host=None, urlconf=None):
"""
Generate the localeurl-enabled path from a path without locale prefix. If
the locale is empty settings.LANGUAGE_CODE is used.
"""
locale = supported_language(locale)
if not locale:
locale = supported_language(settings.LANGUAGE_CODE)
- if is_locale_independent(path):
+ if is_host_independent(host) and is_urlconf_independent(urlconf):
+ return path
+ elif is_locale_independent(path):
return path
elif is_default_locale(locale) and not localeurl_settings.PREFIX_DEFAULT_LOCALE:
return path
else:
return ''.join([u'/', locale, path])
-def locale_url(path, locale=''):
+def locale_url(path, locale='', host=None, prefix='', urlconf=None):
"""
Generate the localeurl-enabled URL from a path without locale prefix. If
the locale is empty settings.LANGUAGE_CODE is used.
"""
- path = locale_path(path, locale)
- return add_script_prefix(path)
+ if urlconf is None:
+ urlconf = get_urlconf()
-def strip_script_prefix(url):
+ path = locale_path(path, locale, host=host, urlconf=urlconf)
+ return add_script_prefix(path, prefix=prefix)
+
+def strip_script_prefix(url, prefix = None):
"""
Strips the SCRIPT_PREFIX from the URL. Because this function is meant for
use in templates, it assumes the URL starts with the prefix.
"""
- assert url.startswith(urlresolvers.get_script_prefix()), \
+ script_prefix = prefix or urlresolvers.get_script_prefix()
+ assert url.startswith(script_prefix), \
"URL must start with SCRIPT_PREFIX: %s" % url
- pos = len(urlresolvers.get_script_prefix()) - 1
+ pos = len(script_prefix) - 1
return url[:pos], url[pos:]
-def add_script_prefix(path):
+def add_script_prefix(path, prefix=None):
"""
Prepends the SCRIPT_PREFIX to a path.
"""
-
- return ''.join([urlresolvers.get_script_prefix(), path[1:]])
+ script_prefix = prefix or urlresolvers.get_script_prefix()
+ return ''.join([script_prefix, path[1:]])

0 comments on commit 0b1e553

Please sign in to comment.