1
from __future__ import unicode_literals
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
5
10
class RedirectFallbackMiddleware(object):
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."
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()
22
full_path = request.get_full_path()
23
current_site = get_current_site(request)
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:
14
if r is None and settings.APPEND_SLASH:
15
# Try removing the trailing slash.
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:]
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: