request.py 667 B

12345678910111213141516171819202122232425262728293031
  1. from __future__ import unicode_literals
  2. import threading
  3. from mezzanine.utils.deprecation import MiddlewareMixin
  4. _thread_local = threading.local()
  5. def current_request():
  6. """
  7. Retrieves the request from the current thread.
  8. """
  9. return getattr(_thread_local, "request", None)
  10. class CurrentRequestMiddleware(MiddlewareMixin):
  11. """
  12. Stores the request in the current thread for global access.
  13. """
  14. def process_request(self, request):
  15. _thread_local.request = request
  16. # def process_response(self, request, response):
  17. # try:
  18. # return response
  19. # finally:
  20. # _thread_local.request = None