From c76bc9c30e974d2333a042a223b53abb6b913fa1 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Fri, 17 Oct 2014 17:12:44 +0100 Subject: [PATCH] Clear wagtail_site_root_paths on Site delete Fixes #706 --- wagtail/wagtailcore/models.py | 19 +++-- wagtail/wagtailcore/tests/tests.py | 121 +++++++++++++++++++++-------- 2 files changed, 102 insertions(+), 38 deletions(-) diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index fdc30af672..8750881f95 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -9,7 +9,7 @@ from modelcluster.models import ClusterableModel, get_all_child_relations from django.db import models, connection, transaction from django.db.models import Q -from django.db.models.signals import pre_delete, post_delete +from django.db.models.signals import post_save, pre_delete, post_delete from django.dispatch.dispatcher import receiver from django.http import Http404 from django.core.cache import cache @@ -116,12 +116,6 @@ class Site(models.Model): ]} ) - # clear the wagtail_site_root_paths cache whenever Site records are updated - def save(self, *args, **kwargs): - result = super(Site, self).save(*args, **kwargs) - cache.delete('wagtail_site_root_paths') - return result - @staticmethod def get_site_root_paths(): """ @@ -140,6 +134,17 @@ class Site(models.Model): return result +# Clear the wagtail_site_root_paths from the cache whenever Site records are updated +@receiver(post_save, sender=Site) +def clear_site_root_paths_on_save(sender, instance, **kwargs): + cache.delete('wagtail_site_root_paths') + + +@receiver(post_delete, sender=Site) +def clear_site_root_paths_on_delete(sender, instance, **kwargs): + cache.delete('wagtail_site_root_paths') + + PAGE_MODEL_CLASSES = [] _PAGE_CONTENT_TYPES = [] diff --git a/wagtail/wagtailcore/tests/tests.py b/wagtail/wagtailcore/tests/tests.py index 5fd5aa8025..febc3f4ae8 100644 --- a/wagtail/wagtailcore/tests/tests.py +++ b/wagtail/wagtailcore/tests/tests.py @@ -1,4 +1,5 @@ from django.test import TestCase +from django.core.cache import cache from wagtail.wagtailcore.models import Page, Site from wagtail.tests.models import SimplePage @@ -20,23 +21,86 @@ class TestPageUrlTags(TestCase): 'Back to events index') -class TestIssue7(TestCase): - """ - This tests for an issue where if a site root page was moved, all - the page urls in that site would change to None. - - The issue was caused by the 'wagtail_site_root_paths' cache - variable not being cleared when a site root page was moved. Which - left all the child pages thinking that they are no longer in the - site and return None as their url. - - Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 - Discussion: https://github.com/torchbox/wagtail/issues/7 - """ - +class TestSiteRootPathsCache(TestCase): fixtures = ['test.json'] - def test_issue7(self): + def test_cache(self): + """ + This tests that the cache is cleared whenever a site is deleted + """ + # Get homepage + homepage = Page.objects.get(url_path='/home/') + + # Warm up the cache by getting the url + _ = homepage.url + + # Check that the cache has been set correctly + self.assertEqual(cache.get('wagtail_site_root_paths'), [(1, '/home/', 'http://localhost')]) + + def test_cache_clears_when_site_saved(self): + """ + This tests that the cache is cleared whenever a site is deleted + """ + # Get homepage + homepage = Page.objects.get(url_path='/home/') + + # Warm up the cache by getting the url + _ = homepage.url + + # Check that the cache has been set + self.assertTrue(cache.get('wagtail_site_root_paths')) + + # Save the site + Site.objects.get(is_default_site=True).save() + + # Check that the cache has been cleared + self.assertFalse(cache.get('wagtail_site_root_paths')) + + def test_cache_clears_when_site_deleted(self): + """ + This tests that the cache is cleared whenever a site is deleted + """ + # Get homepage + homepage = Page.objects.get(url_path='/home/') + + # Warm up the cache by getting the url + _ = homepage.url + + # Check that the cache has been set + self.assertTrue(cache.get('wagtail_site_root_paths')) + + # Delete the site + Site.objects.get(is_default_site=True).delete() + + # Check that the cache has been cleared + self.assertFalse(cache.get('wagtail_site_root_paths')) + + def test_cache_clears_when_site_deleted(self): + """ + This tests that the cache is cleared whenever a site is deleted + """ + # Get homepage + homepage = Page.objects.get(url_path='/home/') + + # Warm up the cache by getting the url + _ = homepage.url + + # Check that the cache has been set + print(cache.get('wagtail_site_root_paths')) + + def test_cache_clears_when_site_root_moves(self): + """ + This tests for an issue where if a site root page was moved, all + the page urls in that site would change to None. + + The issue was caused by the 'wagtail_site_root_paths' cache + variable not being cleared when a site root page was moved. Which + left all the child pages thinking that they are no longer in the + site and return None as their url. + + Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 + Discussion: https://github.com/torchbox/wagtail/issues/7 + """ # Get homepage, root page and site root_page = Page.objects.get(id=1) homepage = Page.objects.get(url_path='/home/') @@ -62,24 +126,19 @@ class TestIssue7(TestCase): # Check url self.assertEqual(new_homepage.url, '/') + def test_cache_clears_when_site_root_slug_changes(self): + """ + This tests for an issue where if a site root pages slug was + changed, all the page urls in that site would change to None. -class TestIssue157(TestCase): - """ - This tests for an issue where if a site root pages slug was - changed, all the page urls in that site would change to None. + The issue was caused by the 'wagtail_site_root_paths' cache + variable not being cleared when a site root page was changed. + Which left all the child pages thinking that they are no longer in + the site and return None as their url. - The issue was caused by the 'wagtail_site_root_paths' cache - variable not being cleared when a site root page was changed. - Which left all the child pages thinking that they are no longer in - the site and return None as their url. - - Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 - Discussion: https://github.com/torchbox/wagtail/issues/157 - """ - - fixtures = ['test.json'] - - def test_issue157(self): + Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682 + Discussion: https://github.com/torchbox/wagtail/issues/157 + """ # Get homepage homepage = Page.objects.get(url_path='/home/')