pytask/middleware/exceptions.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 01 Feb 2011 02:31:53 +0530
changeset 540 b07d52d49db7
parent 539 59e032315ab9
child 541 a1007eb3fff9
permissions -rwxr-xr-x
Initial tags for the chapters should not contain Textbook as the keyword.

"""Module containing the middleware that processes exceptions for PyTask.
"""

__authors__ = [
    '"Madhusudan.C.S" <madhusudancs@fossee.in>',
    ]


from django.http import HttpResponse
from django.template import loader
from django.template import RequestContext

from pytask.helpers.exceptions import UnauthorizedAccess


class ExceptionMiddleware(object):
    """Middleware definition that processes exceptions raised in PyTaskViews.
    """
  
    def process_exception(self, request, exception):
        """Process the exception raised.
        """

        if isinstance(exception, UnauthorizedAccess):
            template = loader.get_template('error.html')
            context = RequestContext(request, {
              'error_message': exception.message
              })
            return HttpResponse(template.render(context))
    
        # let Django handle it
        return None