Created
September 4, 2016 05:22
Star
You must be signed in to star a gist
Django (1.10-compatible) thread local (TLS) middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# Copyright (c) Alexandre Syenchuk (alexpirine), 2016 | |
try: | |
from threading import local | |
except ImportError: | |
from django.utils._threading_local import local | |
_thread_locals = local() | |
def get_current_request(): | |
return getattr(_thread_locals, 'request', None) | |
def get_current_user(): | |
request = get_current_request() | |
if request: | |
return getattr(request, 'user', None) | |
class ThreadLocalMiddleware(object): | |
def __init__(self, get_response): | |
self.get_response = get_response | |
def __call__(self, request): | |
_thread_locals.request = request | |
return self.get_response(request) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment