add support for getting current request object from within a model
diff --git a/planetstack/core/middleware.py b/planetstack/core/middleware.py
new file mode 100644
index 0000000..1401cd6
--- /dev/null
+++ b/planetstack/core/middleware.py
@@ -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
diff --git a/planetstack/planetstack/settings.py b/planetstack/planetstack/settings.py
index 215501a..b694373 100644
--- a/planetstack/planetstack/settings.py
+++ b/planetstack/planetstack/settings.py
@@ -111,6 +111,7 @@
'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 @@
BIGQUERY_TABLE = getattr(config, "bigquery_table", "demoevents")
DISABLE_MINIDASHBOARD = getattr(config, "gui_disable_minidashboard", False)
+