4
TIMELINE_VAR = 'timeline'
7
_thread_local = threading.local()
11
"""Get the timeline associated with the current request, if any.
13
If `TimelineMiddleware` saved a `Timeline` object at the start of
14
the request calling this function will return it. If there isn't
15
a saved timeline then `None` will be returned.
17
return getattr(_thread_local, TIMELINE_VAR, None)
20
class TimelineMiddleware(object):
21
"""Save a `Timeline` as a thread local.
23
This middleware looks in `request.environ` for a `timeline.timeline`
24
key, and if it is present it stores it in a thread local variable,
25
so that `get_timeline` will return it when later called in that
29
TIMELINE_ENVIRON_KEY = 'timeline.timeline'
31
def process_request(self, request):
32
if self.TIMELINE_ENVIRON_KEY in request.environ:
33
setattr(_thread_local, TIMELINE_VAR,
34
request.environ[self.TIMELINE_ENVIRON_KEY])