~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

Viewing changes to django/contrib/redirects/middleware.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import unicode_literals
 
2
 
 
3
from django.conf import settings
1
4
from django.contrib.redirects.models import Redirect
 
5
from django.contrib.sites.models import get_current_site
 
6
from django.core.exceptions import ImproperlyConfigured
2
7
from django import http
3
 
from django.conf import settings
 
8
 
4
9
 
5
10
class RedirectFallbackMiddleware(object):
 
11
    def __init__(self):
 
12
        if 'django.contrib.sites' not in settings.INSTALLED_APPS:
 
13
            raise ImproperlyConfigured(
 
14
                "You cannot use RedirectFallbackMiddleware when "
 
15
                "django.contrib.sites is not installed."
 
16
            )
 
17
 
6
18
    def process_response(self, request, response):
7
19
        if response.status_code != 404:
8
20
            return response # No need to check for a redirect for non-404 responses.
9
 
        path = request.get_full_path()
 
21
 
 
22
        full_path = request.get_full_path()
 
23
        current_site = get_current_site(request)
 
24
 
 
25
        r = None
10
26
        try:
11
 
            r = Redirect.objects.get(site__id__exact=settings.SITE_ID, old_path=path)
 
27
            r = Redirect.objects.get(site=current_site, old_path=full_path)
12
28
        except Redirect.DoesNotExist:
13
 
            r = None
14
 
        if r is None and settings.APPEND_SLASH:
15
 
            # Try removing the trailing slash.
 
29
            pass
 
30
        if settings.APPEND_SLASH and not request.path.endswith('/'):
 
31
            # Try appending a trailing slash.
 
32
            path_len = len(request.path)
 
33
            full_path = full_path[:path_len] + '/' + full_path[path_len:]
16
34
            try:
17
 
                r = Redirect.objects.get(site__id__exact=settings.SITE_ID,
18
 
                    old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:])
 
35
                r = Redirect.objects.get(site=current_site, old_path=full_path)
19
36
            except Redirect.DoesNotExist:
20
37
                pass
21
38
        if r is not None: