6d1c1551841e6bf53239937403436805c2ec5ae1
[wolnelektury.git] / apps / piston / middleware.py
1 from django.middleware.http import ConditionalGetMiddleware
2 from django.middleware.common import CommonMiddleware
3
4 def compat_middleware_factory(klass):
5     """
6     Class wrapper that only executes `process_response`
7     if `streaming` is not set on the `HttpResponse` object.
8     Django has a bad habbit of looking at the content,
9     which will prematurely exhaust the data source if we're
10     using generators or buffers.
11     """
12     class compatwrapper(klass):
13         def process_response(self, req, resp):
14             if not hasattr(resp, 'streaming'):
15                 return klass.process_response(self, req, resp)
16             return resp
17     return compatwrapper
18
19 ConditionalMiddlewareCompatProxy = compat_middleware_factory(ConditionalGetMiddleware)
20 CommonMiddlewareCompatProxy = compat_middleware_factory(CommonMiddleware)