Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #19698 -- Cleared sites cache with signals

Thanks ddavies at nomensa.com for the report and the patch and
serdaroncode for reviewing the patch.
  • Loading branch information...
commit 98ab8e8876e130de24e1de9473a4db716d5b002e 1 parent bfd4a71
@claudep claudep authored
Showing with 21 additions and 14 deletions.
  1. +14 −14 django/contrib/sites/models.py
  2. +7 −0 django/contrib/sites/tests.py
View
28 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 Meta:
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)
View
7 django/contrib/sites/tests.py
@@ -42,6 +42,13 @@ def test_site_cache(self):
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
Please sign in to comment.
Something went wrong with that request. Please try again.