|
1
|
import threading |
|
2
|
|
|
3
|
_thread_local = threading.local() |
|
4
|
|
|
5
|
|
|
6
|
def get_current_user(): |
|
7
|
return getattr(_thread_local, "user", None) |
|
8
|
|
|
9
|
|
|
10
|
class CurrentUserMiddleware: |
|
11
|
"""Store the current user on thread-local storage for use in signals and model save methods.""" |
|
12
|
|
|
13
|
def __init__(self, get_response): |
|
14
|
self.get_response = get_response |
|
15
|
|
|
16
|
def __call__(self, request): |
|
17
|
_thread_local.user = getattr(request, "user", None) |
|
18
|
response = self.get_response(request) |
|
19
|
return response |
|
20
|
|