From: Scott Baker Date: Wed, 27 Aug 2014 00:40:36 +0000 (-0700) Subject: add support for getting current request object from within a model X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=6810db24d99643479924046a6587c10fc9ad5ad8;p=plstackapi.git 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 @@ 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) +