Add exception middleware and related files.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Tue, 01 Feb 2011 02:14:49 +0530
changeset 539 59e032315ab9
parent 538 478c7fc9a223
child 540 b07d52d49db7
Add exception middleware and related files.
pytask/helpers/exceptions.py
pytask/middleware/__init__.py
pytask/middleware/exceptions.py
pytask/settings.py
pytask/static/css/base.css
pytask/templates/error.html
--- /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" <madhusudancs@fossee.in>',
+    ]
+
+
+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__()
--- /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" <madhusudancs@fossee.in>',
+    ]
--- /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" <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
--- 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'
--- 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;
+}
--- /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 %}
+  <div class="exception">
+    {{ error_message }}
+  </div>
+{% endblock %}