From 99d99019e75a7190a0154ec12845e67fc99aafb9 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 9 Jul 2011 23:53:30 -0400 Subject: add prevent_auto flag to cache control --- pyramid/config.py | 10 +++++++++- pyramid/tests/test_config.py | 9 +++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pyramid/config.py b/pyramid/config.py index aa02c3d69..53daf3b56 100644 --- a/pyramid/config.py +++ b/pyramid/config.py @@ -1140,6 +1140,12 @@ class Configurator(object): tuple as ``http_cache`` with the first element of ``None``, e.g.: ``(None, {'public':True})``. + If you wish to prevent a view that uses ``http_cache`` in its + configuration from having its response changed by ``http_cache`` , + set ``response.cache_control.prevent_auto = True`` before returning + the response. This effectively disables any HTTP caching done by + ``http_cache`` for that response. + wrapper The :term:`view name` of a different :term:`view @@ -2990,7 +2996,9 @@ class ViewDeriver(object): def wrapper(context, request): response = view(context, request) - response.cache_expires(seconds, **options) + cache_control = response.cache_control + if not hasattr(cache_control, 'prevent_auto'): + response.cache_expires(seconds, **options) return response return wrapper diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py index ad5e5a89a..d7e62da0a 100644 --- a/pyramid/tests/test_config.py +++ b/pyramid/tests/test_config.py @@ -4318,19 +4318,20 @@ class TestViewDeriver(unittest.TestCase): self.assertFalse('Expires' in headers) self.assertEqual(headers['Cache-Control'], 'public') - def test_http_cached_view_nonresponse_object_returned_downstream(self): + def test_http_cached_view_prevent_auto_set(self): from pyramid.response import Response response = Response() + response.cache_control.prevent_auto = True def inner_view(context, request): return response deriver = self._makeOne(http_cache=3600) result = deriver(inner_view) - self.assertFalse(result is inner_view) - self.assertEqual(inner_view.__module__, result.__module__) - self.assertEqual(inner_view.__doc__, result.__doc__) request = self._makeRequest() result = result(None, request) self.assertEqual(result, response) # doesn't blow up + headers = dict(result.headerlist) + self.assertFalse('Expires' in headers) + self.assertFalse('Cache-Control' in headers) def test_http_cached_view_bad_tuple(self): from pyramid.exceptions import ConfigurationError -- cgit v1.2.3