PageRenderTime 157ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/redirects/middleware.py

https://code.google.com/p/mango-py/
Python | 27 lines | 25 code | 1 blank | 1 comment | 9 complexity | a859f6d39c514d2bd073f8651c169c7c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.contrib.redirects.models import Redirect
  2. from django import http
  3. from django.conf import settings
  4. class RedirectFallbackMiddleware(object):
  5. def process_response(self, request, response):
  6. if response.status_code != 404:
  7. return response # No need to check for a redirect for non-404 responses.
  8. path = request.get_full_path()
  9. try:
  10. r = Redirect.objects.get(site__id__exact=settings.SITE_ID, old_path=path)
  11. except Redirect.DoesNotExist:
  12. r = None
  13. if r is None and settings.APPEND_SLASH:
  14. # Try removing the trailing slash.
  15. try:
  16. r = Redirect.objects.get(site__id__exact=settings.SITE_ID,
  17. old_path=path[:path.rfind('/')]+path[path.rfind('/')+1:])
  18. except Redirect.DoesNotExist:
  19. pass
  20. if r is not None:
  21. if r.new_path == '':
  22. return http.HttpResponseGone()
  23. return http.HttpResponsePermanentRedirect(r.new_path)
  24. # No redirect was found. Return the response.
  25. return response