summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-02-27 04:57:33 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-02-27 04:57:33 +0000
commit0dd4ec654f4b22854df841d9ea8f463fdbdf99c3 (patch)
tree84e15cddcd6f2c46eb1f35af3b4d5a2c68701b96
parent1306079a024ac7109487d2a560ceda3f842a4572 (diff)
Invalidate the appropriate SITE_CACHE entry when saving a Site model.
This avoids the problem of, for example, saving a change to the Site model in the admin interface and then seeing the wrong instanec returned in the next call to get_current_site(). It's still possible to end up with an inconsistent cache if update() is used to change the Site model, but that's pretty unavoidable. It's also a slightly odd way to update a Site model, so if you really need to do that, you can manage to call SiteManager.clear() at the same time. git-svn-id: http://code.djangoproject.com/svn/django/trunk@9908 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/contrib/sites/models.py10
-rw-r--r--django/contrib/sites/tests.py15
2 files changed, 22 insertions, 3 deletions
diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
index c8b395e36b..9b1697e251 100644
--- a/django/contrib/sites/models.py
+++ b/django/contrib/sites/models.py
@@ -41,12 +41,18 @@ class Site(models.Model):
def __unicode__(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])
+ del SITE_CACHE[pk]
except KeyError:
pass
diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py
index b607f021d5..e3fa81b9d1 100644
--- a/django/contrib/sites/tests.py
+++ b/django/contrib/sites/tests.py
@@ -3,7 +3,7 @@
>>> from django.conf import settings
>>> Site(id=settings.SITE_ID, domain="example.com", name="example.com").save()
->>> # Make sure that get_current() does not return a deleted Site object.
+# Make sure that get_current() does not return a deleted Site object.
>>> s = Site.objects.get_current()
>>> isinstance(s, Site)
True
@@ -13,4 +13,17 @@ True
Traceback (most recent call last):
...
DoesNotExist: Site matching query does not exist.
+
+# After updating a Site object (e.g. via the admin), we shouldn't return a
+# bogus value from the SITE_CACHE.
+>>> _ = Site.objects.create(id=settings.SITE_ID, domain="example.com", name="example.com")
+>>> site = Site.objects.get_current()
+>>> site.name
+u"example.com"
+>>> s2 = Site.objects.get(id=settings.SITE_ID)
+>>> s2.name = "Example site"
+>>> s2.save()
+>>> site = Site.objects.get_current()
+>>> site.name
+u"Example site"
"""