Merge tag '0.10.2'
[ganeti_webmgr.git] / ganeti_web / middleware.py
blobd4e8b570fba01c4361af0466c104fbea57c30c18
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):
11 """
12 Render a 403 response.
13 """
14 template = loader.get_template('403.html')
15 context = RequestContext(request, {
16 'message': message,
19 return HttpResponseForbidden(template.render(context))
22 class PermissionDeniedMiddleware(object):
23 """
24 Middleware which intercepts ``PermissionDenied`` exceptions and returns 403
25 responses.
26 """
28 def process_exception(self, request, e):
29 if isinstance(e, PermissionDenied):
30 return render_403(request, ", ".join(e.args))