summaryrefslogtreecommitdiff
path: root/pgweb/util/middleware.py
diff options
context:
space:
mode:
authorMagnus Hagander2009-09-14 12:39:25 +0000
committerMagnus Hagander2009-09-14 12:39:25 +0000
commit90b758c247ad4f630f1775c6154daaef62284f52 (patch)
tree800d94792715e6b57d14cc7da618a771abb036e9 /pgweb/util/middleware.py
A first very basic import.
Contains basic functionality, and an import of most of the static content from the old site. There is still plenty more to do...
Diffstat (limited to 'pgweb/util/middleware.py')
-rw-r--r--pgweb/util/middleware.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/pgweb/util/middleware.py b/pgweb/util/middleware.py
new file mode 100644
index 00000000..0646673a
--- /dev/null
+++ b/pgweb/util/middleware.py
@@ -0,0 +1,37 @@
+from django.http import HttpResponseRedirect, HttpResponse
+
+# Use thread local storage to pass the username down.
+# http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
+try:
+ from threading import local, currentThread
+except ImportError:
+ from django.utils._threading_local import local
+
+_thread_locals = local()
+def get_current_user():
+ return getattr(_thread_locals, 'user', None)
+
+
+# General middleware for all middleware functionality specific to the pgweb
+# project.
+class PgMiddleware(object):
+ def process_view(self, request, view_func, view_args, view_kwargs):
+ # We implement the SSL verification in a middleware and not just a decorator, because
+ # if we do it just in a decorator we'd have to add a decorator for each and every
+ # view that *doesn't* require SSL. This is much easier, of course.
+ if view_func.__name__ == '_require_ssl':
+ # This view requires SSL, so check if we have it
+ if not request.is_secure():
+ # May need to deal with ports specified here?
+ return HttpResponseRedirect(request.build_absolute_uri().replace('http://','https://',1))
+ else:
+ # This view must not use SSL, so make sure we don't have it
+ if request.is_secure():
+ return HttpResponseRedirect(request.build_absolute_uri().replace('https://','http://',1))
+
+ return None
+
+# Thread local store for username, see comment at the top of this file
+ def process_request(self, request):
+ _thread_locals.user = getattr(request, 'user', None)
+