diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py index 8590740658..88a7c4115b 100644 --- a/django/contrib/sites/models.py +++ b/django/contrib/sites/models.py @@ -1,4 +1,5 @@ from django.db import models +from django.db.models.signals import pre_save, pre_delete from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import python_2_unicode_compatible @@ -49,20 +50,6 @@ class Site(models.Model): def __str__(self): return self.domain - def save(self, *args, **kwargs): - super(Site, self).save(*args, **kwargs) - # Cached information will likely be incorrect now. - if self.id in SITE_CACHE: - del SITE_CACHE[self.id] - - def delete(self): - pk = self.pk - super(Site, self).delete() - try: - del SITE_CACHE[pk] - except KeyError: - pass - @python_2_unicode_compatible class RequestSite(object): @@ -96,3 +83,16 @@ def get_current_site(request): else: current_site = RequestSite(request) return current_site + + +def clear_site_cache(sender, **kwargs): + """ + Clears the cache (if primed) each time a site is saved or deleted + """ + instance = kwargs['instance'] + try: + del SITE_CACHE[instance.pk] + except KeyError: + pass +pre_save.connect(clear_site_cache, sender=Site) +pre_delete.connect(clear_site_cache, sender=Site) diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py index cdbd78b80d..ee6224bc7a 100644 --- a/django/contrib/sites/tests.py +++ b/django/contrib/sites/tests.py @@ -42,6 +42,13 @@ class SitesFrameworkTests(TestCase): site = Site.objects.get_current() self.assertEqual("Example site", site.name) + def test_delete_all_sites_clears_cache(self): + # When all site objects are deleted the cache should also + # be cleared and get_current() should raise a DoesNotExist. + self.assertIsInstance(Site.objects.get_current(), Site) + Site.objects.all().delete() + self.assertRaises(Site.DoesNotExist, Site.objects.get_current) + @override_settings(ALLOWED_HOSTS=['example.com']) def test_get_current_site(self): # Test that the correct Site object is returned