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. another request logging middleware with request time and extra info by yoav 2 years, 4 months ago
  2. Accept Header Middleware by kioopi 6 years ago
  3. Middleware to reload translation for each request by asksol 4 years, 8 months ago
  4. Cookieless Session Middleware by juliocarlos 5 years, 4 months ago
  5. Other approach of making middleware (by decorators) by diverman 3 years, 1 month ago

Comments

(Forgotten your password?)