# HG changeset patch # User Madhusudan.C.S # Date 1296506689 -19800 # Node ID 59e032315ab92ab558ffc276552ccbaf1ea032a6 # Parent 478c7fc9a223bcbc62af347354b7cbfd28aeec83 Add exception middleware and related files. diff -r 478c7fc9a223 -r 59e032315ab9 pytask/helpers/exceptions.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pytask/helpers/exceptions.py Tue Feb 01 02:14:49 2011 +0530 @@ -0,0 +1,33 @@ +"""Module containing the exceptions that can be raised. +""" + + +__authors__ = [ + '"Madhusudan.C.S" ', + ] + + +from django.utils.translation import ugettext + + +DEFAULT_LOGIN_MESSAGE = ugettext( + "You have to login to view this page.") + + +class UnauthorizedAccess(Exception): + """Exception that is raised when some one tries to access a view + without the right priviliges. + """ + + def __init__(self, message=None, **response_args): + """Constructor specifying the exception specific attributes + """ + + if not message: + message = DEFAULT_LOGIN_MESSAGE + + self.message = message + self.response_args = response_args + self.response_args['status'] = 401 + + super(UnauthorizedAccess, self).__init__() diff -r 478c7fc9a223 -r 59e032315ab9 pytask/middleware/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pytask/middleware/__init__.py Tue Feb 01 02:14:49 2011 +0530 @@ -0,0 +1,6 @@ +"""Package containing the middlewares for PyTask. +""" + +__authors__ = [ + '"Madhusudan.C.S" ', + ] diff -r 478c7fc9a223 -r 59e032315ab9 pytask/middleware/exceptions.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pytask/middleware/exceptions.py Tue Feb 01 02:14:49 2011 +0530 @@ -0,0 +1,32 @@ +"""Module containing the middleware that processes exceptions for PyTask. +""" + +__authors__ = [ + '"Madhusudan.C.S" ', + ] + + +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 diff -r 478c7fc9a223 -r 59e032315ab9 pytask/settings.py --- a/pytask/settings.py Tue Feb 01 02:14:28 2011 +0530 +++ b/pytask/settings.py Tue Feb 01 02:14:49 2011 +0530 @@ -81,6 +81,7 @@ 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', + 'pytask.middleware.exceptions.ExceptionMiddleware', ) ROOT_URLCONF = 'pytask.urls' diff -r 478c7fc9a223 -r 59e032315ab9 pytask/static/css/base.css --- a/pytask/static/css/base.css Tue Feb 01 02:14:28 2011 +0530 +++ b/pytask/static/css/base.css Tue Feb 01 02:14:49 2011 +0530 @@ -321,3 +321,10 @@ width:98%; text-align: center; } + +/* Styling for displaying page errors. */ +.exception { + font-size: 18px; + color: red; + font-weight: bold; +} diff -r 478c7fc9a223 -r 59e032315ab9 pytask/templates/error.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pytask/templates/error.html Tue Feb 01 02:14:49 2011 +0530 @@ -0,0 +1,6 @@ +{% extends "base.html" %} +{% block content %} +
+ {{ error_message }} +
+{% endblock %}