mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-07-10 06:42:49 +02:00
feat: add local access to user and request from anywhere
This commit is contained in:
@@ -77,6 +77,7 @@ INSTALLED_APPS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
"apps.common.middleware.thread_local.ThreadLocalMiddleware",
|
||||||
"debug_toolbar.middleware.DebugToolbarMiddleware",
|
"debug_toolbar.middleware.DebugToolbarMiddleware",
|
||||||
"django.middleware.security.SecurityMiddleware",
|
"django.middleware.security.SecurityMiddleware",
|
||||||
"whitenoise.middleware.WhiteNoiseMiddleware",
|
"whitenoise.middleware.WhiteNoiseMiddleware",
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""
|
||||||
|
threadlocals middleware
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
make the request object everywhere available (e.g. in model instance).
|
||||||
|
|
||||||
|
based on: http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
|
||||||
|
|
||||||
|
Put this into your settings:
|
||||||
|
--------------------------------------------------------------------------
|
||||||
|
MIDDLEWARE_CLASSES = (
|
||||||
|
...
|
||||||
|
'django_tools.middlewares.ThreadLocal.ThreadLocalMiddleware',
|
||||||
|
...
|
||||||
|
)
|
||||||
|
--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
--------------------------------------------------------------------------
|
||||||
|
from django_tools.middlewares import ThreadLocal
|
||||||
|
|
||||||
|
# Get the current request object:
|
||||||
|
request = ThreadLocal.get_current_request()
|
||||||
|
|
||||||
|
# You can get the current user directly with:
|
||||||
|
user = ThreadLocal.get_current_user()
|
||||||
|
--------------------------------------------------------------------------
|
||||||
|
|
||||||
|
:copyleft: 2009-2017 by the django-tools team, see AUTHORS for more details.
|
||||||
|
:license: GNU GPL v3 or above, see LICENSE for more details.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
from threading import local
|
||||||
|
except ImportError:
|
||||||
|
from django.utils._threading_local import local
|
||||||
|
|
||||||
|
try:
|
||||||
|
from django.utils.deprecation import MiddlewareMixin
|
||||||
|
except ImportError:
|
||||||
|
MiddlewareMixin = object # fallback for Django < 1.10
|
||||||
|
|
||||||
|
|
||||||
|
_thread_locals = local()
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_request():
|
||||||
|
"""returns the request object for this thread"""
|
||||||
|
return getattr(_thread_locals, "request", None)
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_user():
|
||||||
|
"""returns the current user, if exist, otherwise returns None"""
|
||||||
|
request = get_current_request()
|
||||||
|
if request:
|
||||||
|
return getattr(request, "user", None)
|
||||||
|
|
||||||
|
|
||||||
|
class ThreadLocalMiddleware(MiddlewareMixin):
|
||||||
|
"""Simple middleware that adds the request object in thread local storage."""
|
||||||
|
|
||||||
|
def process_request(self, request):
|
||||||
|
_thread_locals.request = request
|
||||||
|
|
||||||
|
def process_response(self, request, response):
|
||||||
|
if hasattr(_thread_locals, "request"):
|
||||||
|
del _thread_locals.request
|
||||||
|
return response
|
||||||
|
|
||||||
|
def process_exception(self, request, exception):
|
||||||
|
if hasattr(_thread_locals, "request"):
|
||||||
|
del _thread_locals.request
|
||||||
Reference in New Issue
Block a user