diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
index 8590740..5e6a330 100644
a
|
b
|
|
1 | 1 | from django.db import models |
| 2 | from django.db.models.signals import pre_save, pre_delete |
2 | 3 | from django.utils.translation import ugettext_lazy as _ |
3 | 4 | from django.utils.encoding import python_2_unicode_compatible |
4 | 5 | |
… |
… |
class Site(models.Model):
|
49 | 50 | def __str__(self): |
50 | 51 | return self.domain |
51 | 52 | |
52 | | def save(self, *args, **kwargs): |
53 | | super(Site, self).save(*args, **kwargs) |
54 | | # Cached information will likely be incorrect now. |
55 | | if self.id in SITE_CACHE: |
56 | | del SITE_CACHE[self.id] |
57 | | |
58 | | def delete(self): |
59 | | pk = self.pk |
60 | | super(Site, self).delete() |
61 | | try: |
62 | | del SITE_CACHE[pk] |
63 | | except KeyError: |
64 | | pass |
65 | | |
66 | 53 | |
67 | 54 | @python_2_unicode_compatible |
68 | 55 | class RequestSite(object): |
… |
… |
def get_current_site(request):
|
96 | 83 | else: |
97 | 84 | current_site = RequestSite(request) |
98 | 85 | return current_site |
| 86 | |
| 87 | |
| 88 | def clear_cache(sender, **kwargs): |
| 89 | """ |
| 90 | Clears the cache (if primed) each time a site is saved or deleted |
| 91 | """ |
| 92 | instance = kwargs['instance'] |
| 93 | try: |
| 94 | del SITE_CACHE[instance.pk] |
| 95 | except KeyError: |
| 96 | pass |
| 97 | pre_save.connect(clear_cache) |
| 98 | pre_delete.connect(clear_cache) |
diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py
index cdbd78b..ee6224b 100644
a
|
b
|
class SitesFrameworkTests(TestCase):
|
42 | 42 | site = Site.objects.get_current() |
43 | 43 | self.assertEqual("Example site", site.name) |
44 | 44 | |
| 45 | def test_delete_all_sites_clears_cache(self): |
| 46 | # When all site objects are deleted the cache should also |
| 47 | # be cleared and get_current() should raise a DoesNotExist. |
| 48 | self.assertIsInstance(Site.objects.get_current(), Site) |
| 49 | Site.objects.all().delete() |
| 50 | self.assertRaises(Site.DoesNotExist, Site.objects.get_current) |
| 51 | |
45 | 52 | @override_settings(ALLOWED_HOSTS=['example.com']) |
46 | 53 | def test_get_current_site(self): |
47 | 54 | # Test that the correct Site object is returned |