summaryrefslogtreecommitdiff
path: root/meetingtools/urlmiddleware.py
blob: 3d77caa252aa83b053a3a41bb2b653fc9327b400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
URL Middleware
Stefano J. Attardi (attardi.org)

$Id$
$URL$

Cleans up urls by adding/removing trailing slashes, adding/removing
the www. prefix, and allowing the language to be set from the url.

If APPEND_SLASH is set to False, trailing slashes are removed from the
urls, except for urls which have an explicit trailing slash in
urls.py, in which case a trailing slash is added.

If REMOVE_WWW is set to True, the www. prefix is removed.

Finally, ?lang=xx can be appended to any url to override the default
language setting. This override is remembered for the following
requests. For example, /article?lang=it would show the article in
Italian regardless of brower settings or cookies, and any following
request to the site would be shown in Italian by default.

Changelog

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
is active). Otherwise use the plain django_language cookie.
Only import the FlatPages module if it is active.

1.2
Added support for FlatPages.
Switched to Django's resolve function (with workaround for when it
returns None).

1.1
Various bugfixes.

1.0
First release.
"""
__version__ = "1.3.2"
__license__ = "Python"
__copyright__ = "Copyright (C) 2006-2007, Stefano J. Attardi"
__author__ = "Stefano J. Attardi <http://attardi.org/>"
__contributors__ = ["Antonio Cavedoni <http://cavedoni.com/>"]

from django.conf import settings
from django.http import HttpResponseRedirect, Http404
from django.core.urlresolvers import resolve
from django.utils.translation import check_for_language
import os

class UrlMiddleware:

    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"]
            else:
                request.COOKIES["django_language"] = request.GET["lang"]

        # work-around for runfcgi
        if request.path == "": request.path = "/"
        request.path = '/'+request.path.lstrip('/')

        # Check for a redirect based on settings.APPEND_SLASH and settings.PREPEND_WWW
        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."):
            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 old_url[1] != "/" and not self._urlExists(old_url[1]):

                if old_url[1][-1] == "/":
                    other = old_url[1][:-1]
                else:
                    other = old_url[1] + "/"

                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)

        return None

    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"]
            else:
                response.set_cookie("django_language", request.GET["lang"])

        return response

    def _urlExists(self, path):
        try:
            if resolve(path) is None: raise Http404 # None?!? You mean 404...
            return True
        except Http404:
            # check for flatpages
            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