Skip to content

Instantly share code, notes, and snippets.

@gepatino
Created May 19, 2017 13:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gepatino/e2d39957a9ef5bd27c28462e5593b147 to your computer and use it in GitHub Desktop.
Save gepatino/e2d39957a9ef5bd27c28462e5593b147 to your computer and use it in GitHub Desktop.
Global Requests for Django
"""
This file contains a middleware and functions that makes possible to access
requests globally. This is very usefull when you need to check the current user
or some request headers in model's save() method, for example.
The middleware will store the current request in the _requests dictionary, so
you can use it later calling the get_current_requet or get_current_user
functions.
You just have to add the middleware to the MIDDLEWARE list (at the bottom is
ok), and the use the provided functions to access the request data.
"""
from threading import current_thread
_requests = {}
def get_current_request():
"""
Returns the current request (or None)
"""
thread_id = current_thread().ident
return _requests.get(thread_id, None)
def get_current_user():
"""
Returns the current user (or None) extracted from the current request.
"""
current_request = get_current_request()
if current_request and current_request.user.is_authenticated:
return current_request.user
class GlobalRequestMiddleware(object):
"""
Middleware that stores the current request to be used from any part of the code.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# store request related to this thread id
thread_id = current_thread().ident
_requests[thread_id] = request
# call the next middleware/view
response = self.get_response(request)
# clenaup
del(_requests[thread_id])
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment