milton / middleware /

from django.conf import settings
from django.http import Http404

from sitepoet.models import Page
from sitepoet.views import view_page

class PageMiddleware(object):
    def process_response(self, request, response):
        # If the response found something, return it.
        if response.status_code != 404:
            return response
        # Otherwise, see if we can get a response from the view
            return view_page(request, request.path_info)
        # If the view couldn't find a page, then return the original response (don't pollute)
        except Http404:
            return response
        # If anything else goes wrong here, just ignore it and return the original response (again, don't pollute)
            if settings.DEBUG:
            return response