diff options
| author | Chris Rossi <chris@archimedeanco.com> | 2014-07-21 16:46:35 -0400 |
|---|---|---|
| committer | Chris Rossi <chris@archimedeanco.com> | 2014-07-21 16:46:35 -0400 |
| commit | 6596304446f8369dfbcf264d143fe85d75832dba (patch) | |
| tree | 786afd855d50a0e0a592f10420320781544b6a53 | |
| parent | aa96dda157d39c57c0d2fe8399db0b2175fa83d2 (diff) | |
| download | pyramid-6596304446f8369dfbcf264d143fe85d75832dba.tar.gz pyramid-6596304446f8369dfbcf264d143fe85d75832dba.tar.bz2 pyramid-6596304446f8369dfbcf264d143fe85d75832dba.zip | |
Add 'prevent_cachebuster' setting.
| -rw-r--r-- | docs/narr/assets.rst | 11 | ||||
| -rw-r--r-- | docs/narr/environment.rst | 20 | ||||
| -rw-r--r-- | pyramid/config/settings.py | 9 | ||||
| -rw-r--r-- | pyramid/config/views.py | 12 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_settings.py | 31 | ||||
| -rw-r--r-- | pyramid/tests/test_config/test_views.py | 11 |
6 files changed, 84 insertions, 10 deletions
diff --git a/docs/narr/assets.rst b/docs/narr/assets.rst index 7987d03a6..fea3fae48 100644 --- a/docs/narr/assets.rst +++ b/docs/narr/assets.rst @@ -349,6 +349,15 @@ headers instructing clients to cache the asset for ten years, unless the restarting your application, you may still generate URLs with a stale md5 checksum. +Disabling the Cache Buster +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +It can be useful in some situations (e.g. development) to globally disable all +configured cache busters without changing calls to +:meth:`~pyramid.config.Configurator.add_static_view`. To do this set the +``PYRAMID_PREVENT_CACHEBUSTER`` environment variable or the +``pyramid.prevent_cachebuster`` configuration value to a true value. + Customizing the Cache Buster ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -387,7 +396,7 @@ checksums as asset tokens. Many HTTP caching proxy implementations will fail to cache any URL which has a query string. For this reason, you should probably prefer - :class:`~pyramid.static.PathSegementCacheBuster` to + :class:`~pyramid.static.PathSegmentCacheBuster` to :class:`~pyramid.static.QueryStringCacheBuster`. In order to implement your own cache buster, you can write your own class from diff --git a/docs/narr/environment.rst b/docs/narr/environment.rst index 412635f08..7e2f19278 100644 --- a/docs/narr/environment.rst +++ b/docs/narr/environment.rst @@ -158,6 +158,26 @@ feature when this is true. | | | +---------------------------------+----------------------------------+ +Preventing Cache Busting +------------------------ + +Prevent the ``cachebuster`` static view configuration argument from having any +effect globally in this process when this value is true. No cache buster will +be configured or used when this is true. + +.. seealso:: + + See also :ref:`cache_busting`. + ++---------------------------------+----------------------------------+ +| Environment Variable Name | Config File Setting Name | ++=================================+==================================+ +| ``PYRAMID_PREVENT_CACHEBUSTER`` | ``pyramid.prevent_cachebuster`` | +| | or ``prevent_cachebuster`` | +| | | +| | | ++---------------------------------+----------------------------------+ + Debugging All ------------- diff --git a/pyramid/config/settings.py b/pyramid/config/settings.py index 565a6699c..4d7af6015 100644 --- a/pyramid/config/settings.py +++ b/pyramid/config/settings.py @@ -17,7 +17,7 @@ class SettingsConfiguratorMixin(object): def add_settings(self, settings=None, **kw): """Augment the :term:`deployment settings` with one or more - key/value pairs. + key/value pairs. You may pass a dictionary:: @@ -117,6 +117,11 @@ class Settings(dict): config_prevent_http_cache) eff_prevent_http_cache = asbool(eget('PYRAMID_PREVENT_HTTP_CACHE', config_prevent_http_cache)) + config_prevent_cachebuster = self.get('prevent_cachebuster', '') + config_prevent_cachebuster = self.get('pyramid.prevent_cachebuster', + config_prevent_cachebuster) + eff_prevent_cachebuster = asbool(eget('PYRAMID_PREVENT_CACHEBUSTER', + config_prevent_cachebuster)) update = { 'debug_authorization': eff_debug_all or eff_debug_auth, @@ -128,6 +133,7 @@ class Settings(dict): 'reload_assets':eff_reload_all or eff_reload_assets, 'default_locale_name':eff_locale_name, 'prevent_http_cache':eff_prevent_http_cache, + 'prevent_cachebuster':eff_prevent_cachebuster, 'pyramid.debug_authorization': eff_debug_all or eff_debug_auth, 'pyramid.debug_notfound': eff_debug_all or eff_debug_notfound, @@ -138,6 +144,7 @@ class Settings(dict): 'pyramid.reload_assets':eff_reload_all or eff_reload_assets, 'pyramid.default_locale_name':eff_locale_name, 'pyramid.prevent_http_cache':eff_prevent_http_cache, + 'pyramid.prevent_cachebuster':eff_prevent_cachebuster, } self.update(update) diff --git a/pyramid/config/views.py b/pyramid/config/views.py index f186a44ae..62feca77e 100644 --- a/pyramid/config/views.py +++ b/pyramid/config/views.py @@ -34,10 +34,7 @@ from pyramid.interfaces import ( ) from pyramid import renderers -from pyramid.static import ( - Md5AssetTokenGenerator, - PathSegmentCacheBuster, -) +from pyramid.static import PathSegmentCacheBuster from pyramid.compat import ( string_types, @@ -1786,7 +1783,7 @@ class ViewsConfiguratorMixin(object): Note that this argument has no effect when the ``name`` is a *url prefix*. By default, this argument is ``None``, meaning that no particular Expires or Cache-Control headers are set in the response, - unless ``cache_bust`` is specified. + unless ``cachebuster`` is specified. The ``cachebuster`` keyword argument may be set to cause :meth:`~pyramid.request.Request.static_url` to use cache busting when @@ -1958,7 +1955,10 @@ class StaticURLInfo(object): # make sure it ends with a slash name = name + '/' - cb = extra.pop('cachebuster', None) + if config.registry.settings.get('pyramid.prevent_cachebuster'): + cb = None + else: + cb = extra.pop('cachebuster', None) if cb is True: cb = self._default_cachebuster() if cb: diff --git a/pyramid/tests/test_config/test_settings.py b/pyramid/tests/test_config/test_settings.py index c74f96375..7cf550c1d 100644 --- a/pyramid/tests/test_config/test_settings.py +++ b/pyramid/tests/test_config/test_settings.py @@ -57,7 +57,7 @@ class TestSettingsConfiguratorMixin(unittest.TestCase): self.assertEqual(settings['a'], 1) class TestSettings(unittest.TestCase): - + def _getTargetClass(self): from pyramid.config.settings import Settings return Settings @@ -131,6 +131,35 @@ class TestSettings(unittest.TestCase): self.assertEqual(result['prevent_http_cache'], True) self.assertEqual(result['pyramid.prevent_http_cache'], True) + def test_prevent_cachebuster(self): + settings = self._makeOne({}) + self.assertEqual(settings['prevent_cachebuster'], False) + self.assertEqual(settings['pyramid.prevent_cachebuster'], False) + result = self._makeOne({'prevent_cachebuster':'false'}) + self.assertEqual(result['prevent_cachebuster'], False) + self.assertEqual(result['pyramid.prevent_cachebuster'], False) + result = self._makeOne({'prevent_cachebuster':'t'}) + self.assertEqual(result['prevent_cachebuster'], True) + self.assertEqual(result['pyramid.prevent_cachebuster'], True) + result = self._makeOne({'prevent_cachebuster':'1'}) + self.assertEqual(result['prevent_cachebuster'], True) + self.assertEqual(result['pyramid.prevent_cachebuster'], True) + result = self._makeOne({'pyramid.prevent_cachebuster':'t'}) + self.assertEqual(result['prevent_cachebuster'], True) + self.assertEqual(result['pyramid.prevent_cachebuster'], True) + result = self._makeOne({}, {'PYRAMID_PREVENT_CACHEBUSTER':'1'}) + self.assertEqual(result['prevent_cachebuster'], True) + self.assertEqual(result['pyramid.prevent_cachebuster'], True) + result = self._makeOne({'prevent_cachebuster':'false', + 'pyramid.prevent_cachebuster':'1'}) + self.assertEqual(result['prevent_cachebuster'], True) + self.assertEqual(result['pyramid.prevent_cachebuster'], True) + result = self._makeOne({'prevent_cachebuster':'false', + 'pyramid.prevent_cachebuster':'f'}, + {'PYRAMID_PREVENT_CACHEBUSTER':'1'}) + self.assertEqual(result['prevent_cachebuster'], True) + self.assertEqual(result['pyramid.prevent_cachebuster'], True) + def test_reload_templates(self): settings = self._makeOne({}) self.assertEqual(settings['reload_templates'], False) diff --git a/pyramid/tests/test_config/test_views.py b/pyramid/tests/test_config/test_views.py index 10a2f6f53..8f600c1d2 100644 --- a/pyramid/tests/test_config/test_views.py +++ b/pyramid/tests/test_config/test_views.py @@ -3951,6 +3951,14 @@ class TestStaticURLInfo(unittest.TestCase): self.assertEqual(subpath, 'some/path') self.assertEqual(kw['x'], 'foo') + def test_add_cachebust_prevented(self): + config = self._makeConfig() + config.registry.settings['pyramid.prevent_cachebuster'] = True + inst = self._makeOne() + inst.add(config, 'view', 'mypackage:path', cachebuster=True) + cachebuster = config.registry._static_url_registrations[0][3] + self.assertEqual(cachebuster, None) + def test_add_cachebust_custom(self): config = self._makeConfig() inst = self._makeOne() @@ -3980,7 +3988,8 @@ class Test_view_description(unittest.TestCase): class DummyRegistry: - pass + def __init__(self): + self.settings = {} from zope.interface import implementer from pyramid.interfaces import IResponse |
