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
.http
import HttpResponseForbidden
7 from django
.template
import RequestContext
, loader
10 class Http403(Exception):
12 A 403 error should be sent back to our client.
16 def render_403(request
, message
):
18 Render a 403 response.
21 template
= loader
.get_template('403.html')
22 context
= RequestContext(request
, {
26 return HttpResponseForbidden(template
.render(context
))
29 class Http403Middleware(object):
31 Middleware which intercepts ``Http403`` exceptions and returns 403
35 def process_exception(self
, request
, e
):
36 if isinstance(e
, Http403
):
37 return render_403(request
, ", ".join(e
.args
))