def process_response(self, request, response): key_prefix = getattr(request, '_cache_key_prefix', self.key_prefix) cache_timeout = getattr(request, '_cache_cache_timeout', self.cache_timeout) if isinstance(response, HttpResponseNotModified): patch_response_headers(response, cache_timeout=cache_timeout) return response elif 'Expires' in response: # Replace 'max-age' value of 'Cache-Control' header by one # calculated from the 'Expires' header's date. # This is necessary because of Django's `FetchFromCacheMiddleware` # gets 'Cache-Control' header from the cache # where 'max-age' corresponds to the moment original response # was generated and thus may be already stale for the current time expires = parse_http_date(response['Expires']) timeout = expires - int(now().timestamp()) patch_cache_control(response, max_age=timeout) with patch(self, 'key_prefix', key_prefix): with patch(self, 'cache_timeout', cache_timeout): return super().process_response(request, response)
def request(self, **request): with patch(http.HttpResponse, 'wsgi_request', _dummy_setter): with patch(http.HttpResponse, 'request', _dummy_setter): return super().request(**request)