Thread-safe middleware that makes the current request
object available globally.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from threading import current_thread
class GlobalRequest(object):
_requests = {}
@staticmethod
def get_request():
try:
return GlobalRequest._requests[current_thread()]
except KeyError:
return None
def process_request(self, request):
GlobalRequest._requests[current_thread()] = request
def process_response(self, request, response):
# Cleanup
thread = current_thread()
try:
del GlobalRequest._requests[thread]
except KeyError:
pass
return response
def get_request():
return GlobalRequest.get_request()
|
More like this
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 week ago
- Help text hyperlinks by sa2812 1 month ago
- Stuff by NixonDash 3 months, 1 week ago
- Add custom fields to the built-in Group model by jmoppel 5 months, 1 week ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 8 months, 3 weeks ago
Comments
Please login first before commenting.