GlobalRequest middleware

 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

  1. Making the HTTP object available globally across the app by sleepycal 2 years, 3 months ago
  2. Get current user without a request object by t_rybik 2 years, 8 months ago
  3. localsettings by elpaso66 3 years, 2 months ago
  4. Scoped Cache Compatible with Django Caching Helpers by axiak 5 years, 2 months ago
  5. RequestMiddleware by martync 3 years, 11 months ago

Comments

(Forgotten your password?)