Ticket #15144: fix_15144_regressions.patch

File fix_15144_regressions.patch, 7.8 KB (added by jsdalton, 4 years ago)
  • tests/regressiontests/cache/tests.py

     
    11331133    def setUp(self):
    11341134        self.orig_cache_middleware_alias = settings.CACHE_MIDDLEWARE_ALIAS
    11351135        self.orig_cache_middleware_key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
     1136        self.orig_cache_middleware_seconds = settings.CACHE_MIDDLEWARE_SECONDS
     1137        self.orig_cache_middleware_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
    11361138        self.orig_caches = settings.CACHES
    11371139
    11381140        settings.CACHE_MIDDLEWARE_ALIAS = 'other'
    11391141        settings.CACHE_MIDDLEWARE_KEY_PREFIX = 'middlewareprefix'
     1142        settings.CACHE_MIDDLEWARE_SECONDS = 30
     1143        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = False
    11401144        settings.CACHES = {
    11411145            'default': {
    11421146                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
     
    11511155    def tearDown(self):
    11521156        settings.CACHE_MIDDLEWARE_ALIAS = self.orig_cache_middleware_alias
    11531157        settings.CACHE_MIDDLEWARE_KEY_PREFIX = self.orig_cache_middleware_key_prefix
     1158        settings.CACHE_MIDDLEWARE_SECONDS = self.orig_cache_middleware_seconds
     1159        settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY = self.orig_cache_middleware_anonymous_only
    11541160        settings.CACHES = self.orig_caches
     1161   
     1162    def test_constructor(self):
     1163        """
     1164        Ensure the constructor is correctly distinguishing between usage of CacheMiddleware as
     1165        Middleware vs. usage of CacheMiddleware as view decorator and setting attributes
     1166        appropriately.
     1167        """
     1168        # If no arguments are passed in construction, it's being used as middleware.
     1169        middleware = CacheMiddleware()
     1170       
     1171        # Now test object attributes against values defined in setUp above
     1172        self.assertEquals(middleware.cache_timeout, 30)
     1173        self.assertEquals(middleware.key_prefix, 'middlewareprefix')
     1174        self.assertEquals(middleware.cache_alias, 'other')
     1175        self.assertEquals(middleware.cache_anonymous_only, False)
     1176       
     1177        # If arguments are being passed in construction, it's being used as a decorator.
     1178        # First, test with "defaults":
     1179        as_view_decorator = CacheMiddleware(cache_alias=None, key_prefix=None)
     1180       
     1181        self.assertEquals(as_view_decorator.cache_timeout, 300) # Timeout value for 'default' cache, i.e. 300
     1182        self.assertEquals(as_view_decorator.key_prefix, '')
     1183        self.assertEquals(as_view_decorator.cache_alias, 'default') # Value of DEFAULT_CACHE_ALIAS from django.core.cache
     1184        self.assertEquals(as_view_decorator.cache_anonymous_only, False)
     1185       
     1186        # Next, test with custom values:
     1187        as_view_decorator_with_custom = CacheMiddleware(cache_anonymous_only=True, cache_timeout=60, cache_alias='other', key_prefix='foo')
     1188       
     1189        self.assertEquals(as_view_decorator_with_custom.cache_timeout, 60)
     1190        self.assertEquals(as_view_decorator_with_custom.key_prefix, 'foo')
     1191        self.assertEquals(as_view_decorator_with_custom.cache_alias, 'other')
     1192        self.assertEquals(as_view_decorator_with_custom.cache_anonymous_only, True)
    11551193
    11561194    def test_middleware(self):
    11571195        def view(request, value):
     
    12011239
    12021240        other_view = cache_page(cache='other')(view)
    12031241        other_with_prefix_view = cache_page(cache='other', key_prefix='prefix2')(view)
    1204 
     1242        other_with_timeout_view = cache_page(4, cache='other', key_prefix='prefix3')(view)
     1243       
    12051244        factory = RequestFactory()
    12061245        request = factory.get('/view/')
    12071246
     
    12401279        # And prefixing the alternate cache yields yet another cache entry
    12411280        response = other_with_prefix_view(request, '9')
    12421281        self.assertEquals(response.content, 'Hello World 9')
    1243 
     1282       
     1283        # Request from the alternate cache with a new prefix and a custom timeout
     1284        response = other_with_timeout_view(request, '20')
     1285        self.assertEquals(response.content, 'Hello World 20')
     1286       
    12441287        # But if we wait a couple of seconds...
    12451288        time.sleep(2)
    12461289
     
    12681311        # .. even if it has a prefix
    12691312        response = other_with_prefix_view(request, '15')
    12701313        self.assertEquals(response.content, 'Hello World 15')
     1314       
     1315        # ... but a view with a custom timeout will still hit
     1316        response = other_with_timeout_view(request, '21')
     1317        self.assertEquals(response.content, 'Hello World 20')
     1318       
     1319        # And if we wait a few more seconds
     1320        time.sleep(2)
     1321       
     1322        # the custom timeouot cache will miss
     1323        response = other_with_timeout_view(request, '22')
     1324        self.assertEquals(response.content, 'Hello World 22')
    12711325
    12721326if __name__ == '__main__':
    12731327    unittest.main()
  • django/core/cache/__init__.py

     
    153153            backend, location, params = parse_backend_uri(backend)
    154154            if backend in BACKENDS:
    155155                backend = 'django.core.cache.backends.%s' % BACKENDS[backend]
    156             params.update(kwargs)
    157156            mod = importlib.import_module(backend)
    158157            backend_cls = mod.CacheClass
    159158        else:
     
    164163    except (AttributeError, ImportError), e:
    165164        raise InvalidCacheBackendError(
    166165            "Could not find backend '%s': %s" % (backend, e))
     166    params.update(kwargs)
    167167    return backend_cls(location, params)
    168168
    169169cache = get_cache(DEFAULT_CACHE_ALIAS)
  • django/middleware/cache.py

     
    152152        # we need to use middleware defaults.
    153153
    154154        cache_kwargs = {}
    155         try:
    156             self.key_prefix = kwargs.get('key_prefix')
    157             if self.key_prefix is not None:
     155       
     156        if 'key_prefix' in kwargs:
     157            self.key_prefix = kwargs['key_prefix']
     158            if self.key_prefix:
    158159                cache_kwargs['KEY_PREFIX'] = self.key_prefix
    159160            else:
    160161                self.key_prefix = ''
    161         except KeyError:
     162        else:
    162163            self.key_prefix = settings.CACHE_MIDDLEWARE_KEY_PREFIX
    163164            cache_kwargs['KEY_PREFIX'] = self.key_prefix
    164         try:
    165             cache_alias = kwargs.get('cache_alias')
    166             if cache_alias is None:
    167                 cache_alias = DEFAULT_CACHE_ALIAS
    168             if cache_timeout is not None:
     165           
     166        if 'cache_alias' in kwargs:
     167            self.cache_alias = kwargs['cache_alias'] or DEFAULT_CACHE_ALIAS
     168            if cache_timeout:
    169169                cache_kwargs['TIMEOUT'] = cache_timeout
    170         except KeyError:
    171             cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
    172             if cache_timeout is None:
    173                 cache_kwargs['TIMEOUT'] = settings.CACHE_MIDDLEWARE_SECONDS
    174             else:
    175                 cache_kwargs['TIMEOUT'] = cache_timeout
    176 
    177         if cache_anonymous_only is None:
    178             self.cache_anonymous_only = getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
    179170        else:
    180             self.cache_anonymous_only = cache_anonymous_only
     171            self.cache_alias = settings.CACHE_MIDDLEWARE_ALIAS
     172            cache_kwargs['TIMEOUT'] = cache_timeout or settings.CACHE_MIDDLEWARE_SECONDS
    181173
    182         self.cache = get_cache(cache_alias, **cache_kwargs)
     174        self.cache_anonymous_only = cache_anonymous_only or getattr(settings, 'CACHE_MIDDLEWARE_ANONYMOUS_ONLY', False)
     175       
     176        self.cache = get_cache(self.cache_alias, **cache_kwargs)
    183177        self.cache_timeout = self.cache.default_timeout
Back to Top