Procházet zdrojové kódy

Make frontend cache default to WAGTAIL_CONTENT_LANGUAGES

Karl Hobley před 4 roky
rodič
revize
b326f1d6eb

+ 26 - 0
wagtail/contrib/frontend_cache/tests.py

@@ -315,6 +315,32 @@ class TestCachePurgingSignals(TestCase):
             'http://localhost/pt-br/events/past/',
         ])
 
+    @override_settings(ROOT_URLCONF='wagtail.tests.urls_multilang',
+                       LANGUAGE_CODE='en',
+                       WAGTAIL_I18N_ENABLED=True,
+                       WAGTAIL_CONTENT_LANGUAGES=[('en', 'English'), ('fr', 'French')])
+    def test_purge_on_publish_with_i18n_enabled(self):
+        PURGED_URLS[:] = []  # reset PURGED_URLS to the empty list
+        page = EventIndex.objects.get(url_path='/home/events/')
+        page.save_revision().publish()
+
+        self.assertEqual(PURGED_URLS, [
+            'http://localhost/en/events/',
+            'http://localhost/en/events/past/',
+            'http://localhost/fr/events/',
+            'http://localhost/fr/events/past/',
+        ])
+
+    @override_settings(ROOT_URLCONF='wagtail.tests.urls_multilang',
+                       LANGUAGE_CODE='en',
+                       WAGTAIL_CONTENT_LANGUAGES=[('en', 'English'), ('fr', 'French')])
+    def test_purge_on_publish_without_i18n_enabled(self):
+        # It should ignore WAGTAIL_CONTENT_LANGUAGES as WAGTAIL_I18N_ENABLED isn't set
+        PURGED_URLS[:] = []  # reset PURGED_URLS to the empty list
+        page = EventIndex.objects.get(url_path='/home/events/')
+        page.save_revision().publish()
+        self.assertEqual(PURGED_URLS, ['http://localhost/en/events/', 'http://localhost/en/events/past/'])
+
 
 class TestPurgeBatchClass(TestCase):
     # Tests the .add_*() methods on PurgeBatch. The .purge() method is tested

+ 6 - 1
wagtail/contrib/frontend_cache/utils.py

@@ -6,6 +6,8 @@ from django.conf import settings
 from django.core.exceptions import ImproperlyConfigured
 from django.utils.module_loading import import_string
 
+from wagtail.core.utils import get_content_languages
+
 logger = logging.getLogger('wagtail.frontendcache')
 
 
@@ -63,7 +65,10 @@ def purge_urls_from_cache(urls, backend_settings=None, backends=None):
     # Convert each url to urls one for each managed language (WAGTAILFRONTENDCACHE_LANGUAGES setting).
     # The managed languages are common to all the defined backends.
     # This depends on settings.USE_I18N
-    languages = getattr(settings, 'WAGTAILFRONTENDCACHE_LANGUAGES', [])
+    # If WAGTAIL_I18N_ENABLED is True, this defaults to WAGTAIL_CONTENT_LANGUAGES
+    wagtail_i18n_enabled = getattr(settings, 'WAGTAIL_I18N_ENABLED', False)
+    content_languages = get_content_languages() if wagtail_i18n_enabled else {}
+    languages = getattr(settings, 'WAGTAILFRONTENDCACHE_LANGUAGES', list(content_languages.keys()))
     if settings.USE_I18N and languages:
         langs_regex = "^/(%s)/" % "|".join(languages)
         new_urls = []