view middleware/http.py @ 195:baf8776dc44d

added code for the integration of comment system into bundles and snippets. database sync must be run so that necessary tables is created for the comments to work properly.
author ahsanalishahid <ahsan.ali.shahid@gmail.com>
date Tue, 02 Jul 2013 02:09:21 +0500
parents 290dd9208cc4
children
line wrap: on
line source

from django.template import RequestContext
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseForbidden
from django.template import loader

class Http403(Exception):
    pass

def render_to_403(*args, **kwargs):
    """
        Returns a HttpResponseForbidden whose content is filled with
        the result of calling
        django.template.loader.render_to_string() with the passed
        arguments.
    """
    if not isinstance(args,list):
        args = []
        args.append('403.html')

    httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
    response = HttpResponseForbidden(loader.render_to_string(*args, **kwargs),
                                     **httpresponse_kwargs)

    return response

class Http403Middleware(object):
    def process_exception(self,request,exception):
        if isinstance(exception,Http403):
            return render_to_403(context_instance=RequestContext(request))