Permalink
Browse files

Committing a newer version (1.3.3) of the URL middleware.

  • Loading branch information...
1 parent d261e20 commit a7a9642bf0c7fb1da7b7df7e3f9a21e19e14e26a @bryanveloso bryanveloso committed Jun 22, 2008
Showing with 33 additions and 20 deletions.
  1. +33 −19 applications/core/middleware/url.py
  2. +0 −1 settings.py
View
52 applications/core/middleware/url.py
@@ -2,7 +2,7 @@
URL Middleware
Stefano J. Attardi (attardi.org)
-$Id: url.py 203 2007-03-06 17:04:55Z asolovyov $
+$Id$
$URL$
Cleans up urls by adding/removing trailing slashes, adding/removing
@@ -22,6 +22,18 @@
Changelog
+1.3.3
+Fixed a bug which made the middleware use sessions for languages even
+when they were not used by anything else.
+
+1.3.2
+Fixed an indentation issue. Added a check for those backends
+which set an empty path (e.g. runfcgi).
+
+1.3.1
+Added support for running in a test
+suite (doesn't assume that HTTP_HOST is set)
+
1.3
Only use sessions for the language preference if the session
cookie has already been set (regardless of whether session middleware
@@ -39,9 +51,9 @@
1.0
First release.
"""
-__version__ = "1.3"
+__version__ = "1.3.3"
__license__ = "Python"
-__copyright__ = "Copyright (C) 2006, Stefano J. Attardi"
+__copyright__ = "Copyright (C) 2006-2008, Stefano J. Attardi"
__author__ = "Stefano J. Attardi <http://attardi.org/>"
__contributors__ = ["Antonio Cavedoni <http://cavedoni.com/>"]
@@ -57,23 +69,25 @@ def process_request(self, request):
# Change the language setting for the current page
if "lang" in request.GET and check_for_language(request.GET["lang"]):
- if hasattr(request, 'session'):
- request.session['django_language'] = request.GET["lang"]
+ if 'sessionid' in request.COOKIES:
+ request.session["django_language"] = request.GET["lang"]
else:
- request.COOKIES['django_language'] = request.GET["lang"]
+ request.COOKIES["django_language"] = request.GET["lang"]
+
+ # work-around for runfcgi
+ if request.path == "": request.path = "/"
# Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW
- httpHost = request.META.get('HTTP_HOST', '')
- old_url = [httpHost, request.path]
+ old_url = [request.META.get("HTTP_HOST", "localhost"), request.path]
new_url = old_url[:]
# if REMOVE_WWW is True, remove the www. from the urls if necessary
- if hasattr(settings, "REMOVE_WWW") and settings.REMOVE_WWW and old_url[0].startswith('www.'):
+ if hasattr(settings, "REMOVE_WWW") and settings.REMOVE_WWW and old_url[0].startswith("www."):
new_url[0] = old_url[0][4:]
if hasattr(settings, "APPEND_SLASH") and not settings.APPEND_SLASH:
# if the url is not found, try with(out) the trailing slash
- if not self._urlExists(old_url[1]):
+ if old_url[1] != "/" and not self._urlExists(old_url[1]):
if old_url[1][-1] == "/":
other = old_url[1][:-1]
@@ -83,12 +97,13 @@ def process_request(self, request):
if self._urlExists(other):
new_url[1] = other
- if new_url != old_url:
- # Redirect
- newurl = "%s://%s%s" % (os.environ.get('HTTPS') == 'on' and 'https' or 'http', new_url[0], new_url[1])
- if request.GET:
- newurl += '?' + request.GET.urlencode()
- return HttpResponseRedirect(newurl)
+ if new_url != old_url:
+ # Redirect
+ newurl = "%s://%s%s" % (os.environ.get("HTTPS") == "on" and "https" or "http", new_url[0], new_url[1])
+ if request.GET:
+ newurl += "?" + request.GET.urlencode()
+
+ return HttpResponseRedirect(newurl)
return None
@@ -97,9 +112,9 @@ def process_response(self, request, response):
# Change the language setting for future pages
if "lang" in request.GET and check_for_language(request.GET["lang"]):
if 'sessionid' in request.COOKIES:
- request.session['django_language'] = request.GET["lang"]
+ request.session["django_language"] = request.GET["lang"]
else:
- response.set_cookie('django_language', request.GET["lang"])
+ response.set_cookie("django_language", request.GET["lang"])
return response
@@ -112,4 +127,3 @@ def _urlExists(self, path):
if "django.contrib.flatpages.middleware.FlatpageFallbackMiddleware" in settings.MIDDLEWARE_CLASSES:
from django.contrib.flatpages.models import FlatPage
return FlatPage.objects.filter(url=path, sites__id=settings.SITE_ID).count() == 1
-
View
1 settings.py
@@ -29,7 +29,6 @@
# Middleware
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
- 'django.middleware.http.SetRemoteAddrFromForwardedFor',
'django.contrib.sessions.middleware.SessionMiddleware',
'applications.core.middleware.url.UrlMiddleware', # Custom Middleware
'django.contrib.auth.middleware.AuthenticationMiddleware',

0 comments on commit a7a9642

Please sign in to comment.