milton / middleware / __init__.py

from django.conf import settings
from django.http import HttpResponsePermanentRedirect, HttpResponseRedirect, HttpResponseGone, Http404
from django.template import Template, RequestContext, loader

from milton.models import Redirect, Page, Story
from milton.views import view_page

class RedirectionMiddleware(object):
    def process_response(self, request, response):
        # If the response found something, return it.
        if response.status_code != 404:
            return response
        try:
            # Look for an exact redirect
            path = request.path
            paths = [path]
            
            # Also try without the trailing slash if someone tacked it on for us
            if settings.APPEND_SLASH:
                paths.append(path[:path.rfind('/')] + path[path.rfind('/')+1:])
            
            r = Redirect.objects.get(site__id__exact=settings.SITE_ID, original__in=paths)
            
            # If the destination is not visible, let the 404 shine.
            if hasattr(r.target_object, 'visible') and r.target_object.visible == False:
                return response
            
            # If there's a redirect, process it
            destination = r.destination()
            
            # If we're about to tell someone to go where we already are, stop and think...
            if destination == request.path_info:
                return response
            
            if destination == None:
                # No destination means it's been removed, so make it a dead end.
                context = RequestContext(request, {
                    'title':'410 Gone',
                    'content':'the resource you seek / has been taken by time / seek answers within'
                })
                template = loader.get_template("error.html")
                return HttpResponseGone(template.render(context))
            
            if r.permanent == True:
                # 301 Moved
                return HttpResponsePermanentRedirect(destination)
            else:
                # 302 Found
                return HttpResponseRedirect(destination)
        
        except Redirect.DoesNotExist, e:
            # Otherwise, return the original response
            return response
        except:
            if settings.DEBUG:
                raise
        # If we're here, we didn't catch something -- just pass along the 404.
        return response


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
        try:
            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)
        except:
            if settings.DEBUG:
                raise
            return response
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.