add support for getting current request object from within a model
authorScott Baker <smbaker@gmail.com>
Wed, 27 Aug 2014 00:40:36 +0000 (17:40 -0700)
committerScott Baker <smbaker@gmail.com>
Wed, 27 Aug 2014 00:40:36 +0000 (17:40 -0700)
planetstack/core/middleware.py [new file with mode: 0644]
planetstack/planetstack/settings.py

diff --git a/planetstack/core/middleware.py b/planetstack/core/middleware.py
new file mode 100644 (file)
index 0000000..1401cd6
--- /dev/null
@@ -0,0 +1,13 @@
+from threading import local
+
+_active = local()
+
+def get_request():
+    if not hasattr(_active, "request"):
+        raise Exception("Please add 'core.middleware.GlobalRequestMiddleware' to /opt/planetstack/planetstack/settings.py:MIDDLEWARE_CLASSES")
+    return _active.request
+
+class GlobalRequestMiddleware(object):
+    def process_view(self, request, view_func, view_args, view_kwargs):
+        _active.request = request
+        return None
index 215501a..b694373 100644 (file)
@@ -111,6 +111,7 @@ MIDDLEWARE_CLASSES = (
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
+    'core.middleware.GlobalRequestMiddleware',
     # Uncomment the next line for simple clickjacking protection:
     # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 )
@@ -261,3 +262,4 @@ LOGGING = {
 BIGQUERY_TABLE = getattr(config, "bigquery_table", "demoevents")
 
 DISABLE_MINIDASHBOARD = getattr(config, "gui_disable_minidashboard", False)
+