Ticket #19698: 19698-rebase-and-cleaned.diff

File 19698-rebase-and-cleaned.diff, 2.2 KB (added by serdaroncode, 2 years ago)
  • django/contrib/sites/models.py

    diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
    index 8590740..5e6a330 100644
    a b  
    11from django.db import models
     2from django.db.models.signals import pre_save, pre_delete
    23from django.utils.translation import ugettext_lazy as _
    34from django.utils.encoding import python_2_unicode_compatible
    45
    class Site(models.Model): 
    4950    def __str__(self):
    5051        return self.domain
    5152
    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 
    6653
    6754@python_2_unicode_compatible
    6855class RequestSite(object):
    def get_current_site(request): 
    9683    else:
    9784        current_site = RequestSite(request)
    9885    return current_site
     86
     87
     88def 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
     97pre_save.connect(clear_cache)
     98pre_delete.connect(clear_cache)
  • django/contrib/sites/tests.py

    diff --git a/django/contrib/sites/tests.py b/django/contrib/sites/tests.py
    index cdbd78b..ee6224b 100644
    a b class SitesFrameworkTests(TestCase): 
    4242        site = Site.objects.get_current()
    4343        self.assertEqual("Example site", site.name)
    4444
     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
    4552    @override_settings(ALLOWED_HOSTS=['example.com'])
    4653    def test_get_current_site(self):
    4754        # Test that the correct Site object is returned
Back to Top