From 341f53511625914d14321d37ad23efb5c51c4afa Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 24 Jun 2014 12:37:37 +0100 Subject: [PATCH] Allow purging of subpaths of pages --- wagtail/contrib/wagtailfrontendcache/utils.py | 8 +++++--- wagtail/wagtailcore/models.py | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/wagtail/contrib/wagtailfrontendcache/utils.py b/wagtail/contrib/wagtailfrontendcache/utils.py index 250f62840f..9737ebbb03 100644 --- a/wagtail/contrib/wagtailfrontendcache/utils.py +++ b/wagtail/contrib/wagtailfrontendcache/utils.py @@ -25,9 +25,11 @@ class CustomHTTPAdapter(HTTPAdapter): def purge_page_from_cache(page): + # Get session varnish_url = getattr(settings, 'WAGTAILFRONTENDCACHE_LOCATION', 'http://127.0.0.1:8000/') - - # Purge session = requests.Session() session.mount('http://', CustomHTTPAdapter(varnish_url)) - session.request('PURGE', page.full_url) + + # Purge paths from cache + for path in page.get_cached_paths(): + session.request('PURGE', page.full_url + path[1:]) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index ee34ecf26c..04a05a42c7 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -629,13 +629,27 @@ class Page(MP_Node, ClusterableModel, Indexed): """ return self.serve(self.dummy_request()) + def get_internal_paths(self): + """ + This returns a list of paths within this page. + This is used for static sites, sitemaps and cache invalidation. + """ + return ['/'] + + def get_cached_paths(self): + """ + This returns a list of paths to invalidate in a frontend cache + """ + return self.get_internal_paths() + def get_static_site_paths(self): """ This is a generator of URL paths to feed into a static site generator Override this if you would like to create static versions of subpages """ - # Yield paths for this page - yield '/' + # Yield paths for this page + for url in self.get_internal_paths(): + yield url # Yield paths for child pages for child in self.get_children().live():