summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMadhusudan.C.S2011-02-01 02:14:49 +0530
committerMadhusudan.C.S2011-02-01 02:14:49 +0530
commit9878c2dfdd15581b53bfb9ccbbc59023997a7690 (patch)
tree07e457f5e650b149ed8e3b685a681cf1bcb3b558
parent02bf8214d3911cf9f67c16ee1276643ea907874f (diff)
downloadpytask-9878c2dfdd15581b53bfb9ccbbc59023997a7690.tar.gz
pytask-9878c2dfdd15581b53bfb9ccbbc59023997a7690.tar.bz2
pytask-9878c2dfdd15581b53bfb9ccbbc59023997a7690.zip
Add exception middleware and related files.
-rwxr-xr-xpytask/helpers/exceptions.py33
-rwxr-xr-xpytask/middleware/__init__.py6
-rwxr-xr-xpytask/middleware/exceptions.py32
-rwxr-xr-xpytask/settings.py1
-rw-r--r--pytask/static/css/base.css7
-rw-r--r--pytask/templates/error.html6
6 files changed, 85 insertions, 0 deletions
diff --git a/pytask/helpers/exceptions.py b/pytask/helpers/exceptions.py
new file mode 100755
index 0000000..060feca
--- /dev/null
+++ b/pytask/helpers/exceptions.py
@@ -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__()
diff --git a/pytask/middleware/__init__.py b/pytask/middleware/__init__.py
new file mode 100755
index 0000000..dc28655
--- /dev/null
+++ b/pytask/middleware/__init__.py
@@ -0,0 +1,6 @@
+"""Package containing the middlewares for PyTask.
+"""
+
+__authors__ = [
+ '"Madhusudan.C.S" <madhusudancs@fossee.in>',
+ ]
diff --git a/pytask/middleware/exceptions.py b/pytask/middleware/exceptions.py
new file mode 100755
index 0000000..689d9f3
--- /dev/null
+++ b/pytask/middleware/exceptions.py
@@ -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
diff --git a/pytask/settings.py b/pytask/settings.py
index a7aa2e9..9202109 100755
--- a/pytask/settings.py
+++ b/pytask/settings.py
@@ -81,6 +81,7 @@ MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
+ 'pytask.middleware.exceptions.ExceptionMiddleware',
)
ROOT_URLCONF = 'pytask.urls'
diff --git a/pytask/static/css/base.css b/pytask/static/css/base.css
index fc7f269..72594bd 100644
--- a/pytask/static/css/base.css
+++ b/pytask/static/css/base.css
@@ -321,3 +321,10 @@ div #modification a:hover {
width:98%;
text-align: center;
}
+
+/* Styling for displaying page errors. */
+.exception {
+ font-size: 18px;
+ color: red;
+ font-weight: bold;
+}
diff --git a/pytask/templates/error.html b/pytask/templates/error.html
new file mode 100644
index 0000000..3ed9b6a
--- /dev/null
+++ b/pytask/templates/error.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+{% block content %}
+ <div class="exception">
+ {{ error_message }}
+ </div>
+{% endblock %}