Improved 'purge_page_from_cache' to purge the url for any managed language

pull/4462/merge
pyMan 2017-10-25 17:34:48 +01:00 zatwierdzone przez Matt Westcott
rodzic c309753378
commit b3fa09b95e
6 zmienionych plików z 59 dodań i 3 usunięć

Wyświetl plik

@ -24,6 +24,7 @@ Changelog
* Add new `wagtail_icon` template tag to facilitate making admin icons accessible (Sander Tuit)
* Set `ALLOWED_HOSTS` in the project template to allow any host in development (Tom Dyson)
* Expose reusable client-side code to build Draftail extensions (Thibaud Colas)
* Added `WAGTAILFRONTENDCACHE_LANGUAGES` setting to specify the languages whose URLs are to be purged when using `i18n_patterns` (PyMan Claudio Marinozzi)
* Fix: Status button on 'edit page' now links to the correct URL when live and draft slug differ (LB (Ben Johnston))
* Fix: Image title text in the gallery and in the chooser now wraps for long filenames (LB (Ben Johnston), Luiz Boaretto)
* Fix: Move image editor action buttons to the bottom of the form on mobile (Julian Gallo)

Wyświetl plik

@ -52,8 +52,11 @@ Add a new item into the ``WAGTAILFRONTENDCACHE`` setting and set the ``BACKEND``
'LOCATION': 'http://localhost:8000',
},
}
WAGTAILFRONTENDCACHE_LANGUAGES = []
Set ``WAGTAILFRONTENDCACHE_LANGUAGES`` to a list of languages (typically equal to ``[l[0] for l in settings.LANGUAGES]``) to also purge the urls for each language of a purging url. This setting needs ``settings.USE_I18N`` to be ``True`` to work. Its default is an empty list.
Finally, make sure you have configured your frontend cache to accept PURGE requests:
- `Varnish <https://www.varnish-cache.org/docs/3.0/tutorial/purging.html>`_

Wyświetl plik

@ -43,6 +43,7 @@ Other features
* Add new `wagtail_icon` template tag to facilitate making admin icons accessible (Sander Tuit)
* Set `ALLOWED_HOSTS` in the project template to allow any host in development (Tom Dyson)
* Expose reusable client-side code to build Draftail extensions (Thibaud Colas)
* Added ``WAGTAILFRONTENDCACHE_LANGUAGES`` setting to specify the languages whose URLs are to be purged when using ``i18n_patterns`` (PyMan Claudio Marinozzi)
Bug fixes
~~~~~~~~~

Wyświetl plik

@ -209,6 +209,18 @@ class TestCachePurgingSignals(TestCase):
page.save_revision().publish()
self.assertEqual(PURGED_URLS, [])
@override_settings(ROOT_URLCONF='wagtail.tests.urls_multilang',
LANGUAGE_CODE='en',
WAGTAILFRONTENDCACHE_LANGUAGES=['en'])
def test_purge_on_publish_in_multilang_env(self):
from django.conf import settings
PURGED_URLS[:] = [] # reset PURGED_URLS to the empty list
page = EventIndex.objects.get(url_path='/home/events/')
page.save_revision().publish()
self.assertEqual(len(PURGED_URLS), len(settings.WAGTAILFRONTENDCACHE_LANGUAGES) * 2)
for isocode, description in settings.WAGTAILFRONTENDCACHE_LANGUAGES:
self.assertIn('http://localhost/%s/events/' % isocode, PURGED_URLS)
class TestPurgeBatchClass(TestCase):
# Tests the .add_*() methods on PurgeBatch. The .purge() method is tested

Wyświetl plik

@ -1,8 +1,10 @@
import logging
import re
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.module_loading import import_string
from django.utils.six.moves.urllib.parse import urlparse, urlunparse
logger = logging.getLogger('wagtail.frontendcache')
@ -54,12 +56,40 @@ def get_backends(backend_settings=None, backends=None):
def purge_url_from_cache(url, backend_settings=None, backends=None):
for backend_name, backend in get_backends(backend_settings, backends).items():
logger.info("[%s] Purging URL: %s", backend_name, url)
backend.purge(url)
purge_urls_from_cache([url], backend_settings=backend_settings, backends=backends)
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 settings.USE_I18N and languages:
langs_regex = "^/(%s)/" % "|".join(languages)
new_urls = []
# Purge the given url for each managed language
for isocode, description in languages:
for url in urls:
up = urlparse(url)
new_url = urlunparse((
up.scheme,
up.netloc,
re.sub(langs_regex, "/%s/" % isocode, up.path),
up.params,
up.query,
up.fragment
))
# Check for best performance. True if re.sub found no match
# It happens when i18n_patterns was not used in urls.py to serve content for different languages from different URLs
if new_url in new_urls:
continue
new_urls.append(new_url)
urls = new_urls
for backend_name, backend in get_backends(backend_settings, backends).items():
for url in urls:
logger.info("[%s] Purging URL: %s", backend_name, url)

Wyświetl plik

@ -0,0 +1,9 @@
from __future__ import absolute_import, unicode_literals
from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns
from wagtail.core import urls as wagtail_urls
urlpatterns = i18n_patterns(url(r'', include(wagtail_urls)))