Repository: wagtail/wagtail

Issue:
wagtail_site_root_paths doesnt get cleared when deleting a site
We do this for saving a site: https://github.com/torchbox/wagtail/blob/master/wagtail/wagtailcore/models.py#L117-L120

This should also be done when deleting one as well



Discussion:

Resolving patch:
diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py
--- a/wagtail/wagtailcore/models.py
+++ b/wagtail/wagtailcore/models.py
@@ -9,7 +9,7 @@
 
 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 @@ def clean_fields(self, exclude=None):
                     ]}
                 )
 
-    # 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 @@ def get_site_root_paths():
         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 = []
 


Test patch:
diff --git a/wagtail/wagtailcore/tests/tests.py b/wagtail/wagtailcore/tests/tests.py
--- 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 @@ def test_slugurl_tag(self):
                             '<a href="/events/">Back to events index</a>')
 
 
-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.
+class TestSiteRootPathsCache(TestCase):
+    fixtures = ['test.json']
 
-    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.
+    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
 
-    Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682
-    Discussion: https://github.com/torchbox/wagtail/issues/7
-    """
+        # Check that the cache has been set correctly
+        self.assertEqual(cache.get('wagtail_site_root_paths'), [(1, '/home/', 'http://localhost')])
 
-    fixtures = ['test.json']
+    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/')
 
-    def test_issue7(self):
+        # 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 @@ def test_issue7(self):
         # 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.
-
-    Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682
-    Discussion: https://github.com/torchbox/wagtail/issues/157
-    """
-
-    fixtures = ['test.json']
+        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.
 
-    def test_issue157(self):
+        Fix: d6cce69a397d08d5ee81a8cbc1977ab2c9db2682
+        Discussion: https://github.com/torchbox/wagtail/issues/157
+        """
         # Get homepage
         homepage = Page.objects.get(url_path='/home/')