Permalink
Browse files

Switched the middleware over to using process_exception

  • Loading branch information...
1 parent 74207de commit 16bfefd6d9521f76bd82ca096b51b4de4452a118 @MegaMark16 MegaMark16 committed Apr 13, 2011
Showing with 23 additions and 0 deletions.
  1. +23 −0 cms_redirects/middleware.py
View
23 cms_redirects/middleware.py
@@ -3,6 +3,7 @@
from django.conf import settings
class RedirectFallbackMiddleware(object):
+ """
def process_response(self, request, response):
if response.status_code != 404:
return response # No need to check for a redirect for non-404 responses.
@@ -27,3 +28,25 @@ def process_response(self, request, response):
# No redirect was found. Return the response.
return response
+ """
+ def process_exception(self, request, exception):
+ if isinstance(exception, Http404):
+ path = request.get_full_path()
+ try:
+ r = CMSRedirect.objects.get(site__id__exact=settings.SITE_ID, old_path=path)
+ except CMSRedirect.DoesNotExist:
+ r = None
+ if r is None and settings.APPEND_SLASH:
+ # Try removing the trailing slash.
+ try:
+ r = CMSRedirect.objects.get(site__id__exact=settings.SITE_ID,
+ old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:])
+ except CMSRedirect.DoesNotExist:
+ pass
+ if r is not None:
+ if r.page:
+ return http.HttpResponsePermanentRedirect(r.page.get_absolute_url())
+ if r.new_path == '':
+ return http.HttpResponseGone()
+ return http.HttpResponsePermanentRedirect(r.new_path)
+

0 comments on commit 16bfefd

Please sign in to comment.