2015-01-28 20:35:27 +08:00
|
|
|
from django.conf import settings
|
2005-11-11 12:45:05 +08:00
|
|
|
from django.contrib.flatpages.views import flatpage
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.http import Http404
|
2015-11-07 23:12:37 +08:00
|
|
|
from django.middleware.exception import ExceptionMiddleware
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
class FlatpageFallbackMiddleware(ExceptionMiddleware):
|
|
|
|
|
|
|
|
def __init__(self, get_response=None):
|
|
|
|
# This override makes get_response optional during the
|
|
|
|
# MIDDLEWARE_CLASSES deprecation.
|
|
|
|
super(FlatpageFallbackMiddleware, self).__init__(get_response)
|
|
|
|
|
|
|
|
def __call__(self, request):
|
|
|
|
response = super(FlatpageFallbackMiddleware, self).__call__(request)
|
|
|
|
return self.process_response(request, response)
|
|
|
|
|
2005-11-11 12:45:05 +08:00
|
|
|
def process_response(self, request, response):
|
|
|
|
if response.status_code != 404:
|
2013-11-03 05:02:56 +08:00
|
|
|
return response # No need to check for a flatpage for non-404 responses.
|
2005-11-11 12:45:05 +08:00
|
|
|
try:
|
2008-08-21 09:40:22 +08:00
|
|
|
return flatpage(request, request.path_info)
|
2005-11-11 12:45:05 +08:00
|
|
|
# Return the original response if any errors happened. Because this
|
|
|
|
# is a middleware, we can't assume the errors will be caught elsewhere.
|
2005-11-20 12:55:57 +08:00
|
|
|
except Http404:
|
|
|
|
return response
|
2013-09-30 23:55:14 +08:00
|
|
|
except Exception:
|
2006-05-02 09:31:56 +08:00
|
|
|
if settings.DEBUG:
|
2005-11-11 12:45:05 +08:00
|
|
|
raise
|
|
|
|
return response
|