forked from django-cms/django-cms
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
se agrego soporte para subdominio para el cambio de internacionaliza…
…cion
- Loading branch information
Showing
10 changed files
with
896 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.conf import settings | ||
|
||
def subdomain(request): | ||
""" | ||
Agrega datos para generar url en los templates | ||
""" | ||
return { | ||
'CORE': { | ||
'DOMAIN':settings.DOMAIN, | ||
'PORT':settings.PORT, | ||
#dominio con puerto incluido | ||
'DOMWPORT':DOMWPORT, | ||
#mirar en setting para entender que significan | ||
'SUBDOM_ASSOC': settings.SUBDOM_ASSOC, | ||
'SUBDOM_ASSOC_INV': settings.SUBDOM_ASSOC_INV, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
# -*- coding: utf-8 -*- | ||
from cms.utils.i18n import get_default_language | ||
from django.conf import settings | ||
from django.core.urlresolvers import reverse | ||
from django.middleware.locale import LocaleMiddleware | ||
from django.utils import translation | ||
import re | ||
import urllib | ||
|
||
SUPPORTED = dict(settings.CMS_LANGUAGES) | ||
|
||
HAS_LANG_PREFIX_RE = re.compile(r"^/(%s)/.*" % "|".join([re.escape(l[0]) for l in settings.CMS_LANGUAGES])) | ||
|
||
def has_lang_prefix(path): | ||
check = HAS_LANG_PREFIX_RE.match(path) | ||
if check is not None: | ||
return check.group(1) | ||
else: | ||
return False | ||
|
||
def patch_response(content, pages_root, language): | ||
print "tratando de cambiar las url" | ||
return content | ||
# 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. | ||
# We have to quote pages_root for the regular expression to match. | ||
# | ||
# The used regex is quite complex. The exact pattern depends on the used settings. | ||
# The regex extracts the path of the url without the leading page root, but only matches urls | ||
# that don't already contain a language string or aren't considered multilingual. | ||
# | ||
# Here is an annotated example pattern (_r_ is a shorthand for the value of pages_root): | ||
# pattern: <a([^>]+)href=("|\')(?=_r_)(?!(/fr/|/de/|/en/|/pt-br/|/media/|/media/admin/))(_r_(.*?))("|\')(.*?)> | ||
# |-\1--| |-\2-| |---------------------\3---------------------| | |-\5--|||-\6-||-\7-| | ||
# |---\4---| | ||
# input (_r_=/): <a href="/admin/password_change/" class="foo"> | ||
# matched groups: (u' ', None, u'/admin/password_change/', u'admin/password_change/', u' class="foo"') | ||
# | ||
# Notice that (?=...) and (?!=...) do not consume input or produce a group in the match object. | ||
# If the regex matches, the extracted path we want is stored in the fourth group (\4). | ||
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.ADMIN_MEDIA_PREFIX] | ||
if getattr(settings,'STATIC_URL', False): | ||
ignore_paths += [settings.STATIC_URL] | ||
HREF_URL_FIX_RE = re.compile(ur'<a([^>]+)href=("|\')(?=%s)(?!(%s))(%s(.*?))("|\')(.*?)>' % ( | ||
quoted_root, | ||
"|".join([re.escape(p) for p in ignore_paths]), | ||
quoted_root | ||
)) | ||
|
||
# Unlike in href links, the '~' (see above) the '~' in form actions appears unquoted. | ||
# | ||
# For understanding this regex, please read the documentation for HREF_URL_FIX_RE above. | ||
|
||
ignore_paths = ['%s%s/' % (pages_root, l[0]) for l in settings.CMS_LANGUAGES] | ||
ignore_paths += [settings.MEDIA_URL, settings.ADMIN_MEDIA_PREFIX] | ||
if getattr(settings,'STATIC_URL', False): | ||
ignore_paths += [settings.STATIC_URL] | ||
FORM_URL_FIX_RE = re.compile(ur'<form([^>]+)action=("|\')(?=%s)(?!(%s))(%s(.*?))("|\')(.*?)>' % ( | ||
pages_root, | ||
"|".join([re.escape(p) for p in ignore_paths]), | ||
pages_root | ||
)) | ||
|
||
content = HREF_URL_FIX_RE.sub(ur'<a\1href=\2/%s%s\5\6\7>' % (language, pages_root), content) | ||
content = FORM_URL_FIX_RE.sub(ur'<form\1action=\2%s%s/\5\6\7>' % (pages_root, language), content).encode("utf8") | ||
return content | ||
|
||
class MultilingualURLMiddleware(object): | ||
|
||
def get_language_from_request (self,request): | ||
url = request.get_host() | ||
#!! regex del codigo de fran le agrego la deteccion de puerto | ||
match = re.search(r"^(http://)?(www.)?(?P<subdomain>[\w\d\-]+)?.%s" % settings.DOMAIN, url) | ||
#TODO corregir el lenguaje por defecto para sacarlo del setting | ||
if match is None: | ||
#si no hay subdominio pongo por defecto | ||
lang=settings.CMS_LANG_DEFAULT | ||
else: | ||
subdomain = match.group('subdomain') | ||
#obtengo el lenguaje apartir del subdominio | ||
lang=settings.SUBDOM_ASSOC.get(subdomain) | ||
if lang is None: | ||
#el lenguaje es nulo va al lenguaje por defecto | ||
lang='es_AR' | ||
#asigno este atributo para que cambie realmente el lenguaje del I18N | ||
if hasattr(request, "session"): | ||
request.session["django_language"] = lang | ||
|
||
#verifico el prefijo de la url | ||
prefix = has_lang_prefix(request.path_info) | ||
if prefix: | ||
request.path = "/" + "/".join(request.path.split("/")[2:]) | ||
request.path_info = "/" + "/".join(request.path_info.split("/")[2:]) | ||
t = prefix | ||
|
||
return lang | ||
|
||
#TODO revisar si falla algo por falta del codigo de abajo | ||
#anulo el código de abajo | ||
|
||
changed = False | ||
prefix = has_lang_prefix(request.path_info) | ||
|
||
#obtiene el lenguaje del prefijo( que no va a estar mas) y lo pone en la session si es que esta soportado | ||
if prefix: | ||
request.path = "/" + "/".join(request.path.split("/")[2:]) | ||
request.path_info = "/" + "/".join(request.path_info.split("/")[2:]) | ||
t = prefix | ||
if t in SUPPORTED: | ||
lang = t | ||
if hasattr(request, "session"): | ||
request.session["django_language"] = lang | ||
changed = True | ||
else: | ||
lang = translation.get_language_from_request(request) | ||
|
||
|
||
if not changed:# si no cambio quiere decir que lo setteo antes | ||
if hasattr(request, "session"):#trata de obtenerlo de la session | ||
lang = request.session.get("django_language", None) | ||
if lang in SUPPORTED and lang is not None: | ||
return lang | ||
elif "django_language" in request.COOKIES.keys():#trata de obtenerlo de las cookies | ||
lang = request.COOKIES.get("django_language", None) | ||
if lang in SUPPORTED and lang is not None: | ||
return lang | ||
if not lang: | ||
lang = translation.get_language_from_request(request) | ||
lang = get_default_language(lang) | ||
|
||
return lang | ||
|
||
def process_request(self, request): | ||
language = self.get_language_from_request(request) | ||
translation.activate(language) | ||
request.LANGUAGE_CODE = language | ||
|
||
def process_response(self, request, response): | ||
language = getattr(request, 'LANGUAGE_CODE', self.get_language_from_request(request)) | ||
local_middleware = LocaleMiddleware() | ||
response =local_middleware.process_response(request, response) | ||
path = unicode(request.path) | ||
|
||
# note: pages_root is assumed to end in '/'. | ||
# testing this and throwing an exception otherwise, would probably be a good idea | ||
|
||
if not path.startswith(settings.MEDIA_URL) and\ | ||
not path.startswith(settings.ADMIN_MEDIA_PREFIX) 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": | ||
pages_root = urllib.unquote(reverse("pages-root")) | ||
try: | ||
decoded_response = response.content.decode('utf-8') | ||
except UnicodeDecodeError: | ||
decoded_response = response.content | ||
|
||
response.content = patch_response( | ||
decoded_response, | ||
pages_root, | ||
request.LANGUAGE_CODE | ||
) | ||
|
||
if (response.status_code == 301 or response.status_code == 302 ): | ||
location = response['Location'] | ||
if not has_lang_prefix(location) and location.startswith("/") and\ | ||
not location.startswith(settings.MEDIA_URL) and\ | ||
not (getattr(settings,'STATIC_URL', False) and location.startswith(settings.STATIC_URL)) and\ | ||
not location.startswith(settings.ADMIN_MEDIA_PREFIX): | ||
response['Location'] = "/%s%s" % (language, location) | ||
|
||
response.set_cookie("django_language", language) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# -*- coding: utf-8 -*- | ||
__author__ = 'andres' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# -*- coding: utf-8 -*- | ||
__author__ = 'andres' | ||
|
||
from django import template | ||
from django.conf import settings | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
@register.filter | ||
def preview_link_with_domain(path, language): | ||
subdomain=settings.SUBDOM_ASSOC_INV.get(language,settings.CMS_LANG_DEFAULT) | ||
return subdomain+'.'+settings.DOMAIN+path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.utils import translation | ||
|
||
__author__ = 'andres' | ||
|
||
from django.http import HttpResponse | ||
from django.utils.translation import ugettext as _ | ||
|
||
def test(request): | ||
t = """ | ||
key: %(key)s | ||
lang: %(lang)s | ||
""" % { | ||
'key': _("PRUEBA"), | ||
'lang': translation.get_language() | ||
} | ||
|
||
return HttpResponse(t) | ||
|
||
|
Oops, something went wrong.