1 # Copyright (c) 2012 Oregon State University
2 # See license and copying files for copyright information.
4 # This module provides middleware which Django is too wimpy to provide itself.
6 from django
.core
.exceptions
import PermissionDenied
7 from django
.http
import HttpResponseForbidden
8 from django
.template
import RequestContext
, loader
10 def render_403(request
, message
):
12 Render a 403 response.
14 template
= loader
.get_template('403.html')
15 context
= RequestContext(request
, {
19 return HttpResponseForbidden(template
.render(context
))
22 class PermissionDeniedMiddleware(object):
24 Middleware which intercepts ``PermissionDenied`` exceptions and returns 403
28 def process_exception(self
, request
, e
):
29 if isinstance(e
, PermissionDenied
):
30 return render_403(request
, ", ".join(e
.args
))