From 703ddbbb9481637a3e5c275f11ae19cad35b93ea Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Wed, 8 Oct 2014 10:45:13 +0100 Subject: [PATCH] WAGTAILFRONTENDCACHE_LOCATION compatibility --- wagtail/contrib/wagtailfrontendcache/tests.py | 21 +++++++++++++++---- wagtail/contrib/wagtailfrontendcache/utils.py | 14 +++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/wagtail/contrib/wagtailfrontendcache/tests.py b/wagtail/contrib/wagtailfrontendcache/tests.py index c68e7a7ed0..f3f8bcf3bf 100644 --- a/wagtail/contrib/wagtailfrontendcache/tests.py +++ b/wagtail/contrib/wagtailfrontendcache/tests.py @@ -1,22 +1,27 @@ -from django.test import TestCase +from django.test import TestCase, override_settings from wagtail.contrib.wagtailfrontendcache.utils import get_backends from wagtail.contrib.wagtailfrontendcache.backends import HTTPBackend, CloudflareBackend class TestBackendConfiguration(TestCase): + def test_default(self): + backends = get_backends() + + self.assertEqual(len(backends), 0) + def test_varnish(self): backends = get_backends(backend_settings={ 'varnish': { 'BACKEND': 'wagtail.contrib.wagtailfrontendcache.backends.HTTPBackend', - 'LOCATION': 'http://localhost:8000/', + 'LOCATION': 'http://localhost:8000', }, }) self.assertEqual(len(backends), 1) self.assertIsInstance(backends[0], HTTPBackend) - self.assertEqual(backends[0].cache_location, 'http://localhost:8000/') + self.assertEqual(backends[0].cache_location, 'http://localhost:8000') def test_cloudflare(self): backends = get_backends(backend_settings={ @@ -33,7 +38,7 @@ class TestBackendConfiguration(TestCase): self.assertEqual(backends[0].cloudflare_email, 'test@test.com') self.assertEqual(backends[0].cloudflare_token, 'this is the token') - def test_both(self): + def test_multiple(self): backends = get_backends(backend_settings={ 'varnish': { 'BACKEND': 'wagtail.contrib.wagtailfrontendcache.backends.HTTPBackend', @@ -63,3 +68,11 @@ class TestBackendConfiguration(TestCase): self.assertEqual(len(backends), 1) self.assertIsInstance(backends[0], CloudflareBackend) + + @override_settings(WAGTAILFRONTENDCACHE_LOCATION='http://localhost:8000') + def test_backwards_compatibility(self): + backends = get_backends() + + self.assertEqual(len(backends), 1) + self.assertIsInstance(backends[0], HTTPBackend) + self.assertEqual(backends[0].cache_location, 'http://localhost:8000') diff --git a/wagtail/contrib/wagtailfrontendcache/utils.py b/wagtail/contrib/wagtailfrontendcache/utils.py index 264795dc96..35544001e8 100644 --- a/wagtail/contrib/wagtailfrontendcache/utils.py +++ b/wagtail/contrib/wagtailfrontendcache/utils.py @@ -45,9 +45,23 @@ def import_string(dotted_path): def get_backends(backend_settings=None, backends=None): + # Get backend settings from WAGTAILFRONTENDCACHE setting if backend_settings is None: backend_settings = getattr(settings, 'WAGTAILFRONTENDCACHE', None) + # Fallback to using WAGTAILFRONTENDCACHE_LOCATION setting (backwards compatibility) + if backend_settings is None: + cache_location = getattr(settings, 'WAGTAILFRONTENDCACHE_LOCATION', None) + + if cache_location is not None: + backend_settings = { + 'default': { + 'BACKEND': 'wagtail.contrib.wagtailfrontendcache.backends.HTTPBackend', + 'LOCATION': cache_location, + }, + } + + # No settings found, return empty list if backend_settings is None: return []