Ticket #15894: 15894_v2.diff

File 15894_v2.diff, 4.5 KB (added by krzysiumed, 3 years ago)
  • django/contrib/sites/models.py

    diff --git a/django/contrib/sites/models.py b/django/contrib/sites/models.py
    index fecbff7..d5419f9 100644
    a b  
    11from django.db import models
    22from django.utils.translation import ugettext_lazy as _
    3 
    4 
    5 SITE_CACHE = {}
     3from django.core.cache import cache
    64
    75
    86class SiteManager(models.Manager):
    class SiteManager(models.Manager): 
    1917        except AttributeError:
    2018            from django.core.exceptions import ImproperlyConfigured
    2119            raise ImproperlyConfigured("You're using the Django \"sites framework\" without having set the SITE_ID setting. Create a site in your database and set the SITE_ID setting to fix this error.")
    22         try:
    23             current_site = SITE_CACHE[sid]
    24         except KeyError:
     20
     21        current_site = cache.get('site-cache-%s' % sid)
     22        if current_site is None:
    2523            current_site = self.get(pk=sid)
    26             SITE_CACHE[sid] = current_site
     24            cache.set('site-cache-%s' % sid, current_site)
    2725        return current_site
    2826
    2927    def clear_cache(self):
    3028        """Clears the ``Site`` object cache."""
    31         global SITE_CACHE
    32         SITE_CACHE = {}
     29        for s in self.all():
     30            cache.delete('site-cache-%s' % s.id)
    3331
    3432
    3533class Site(models.Model):
    class Site(models.Model): 
    5048    def save(self, *args, **kwargs):
    5149        super(Site, self).save(*args, **kwargs)
    5250        # Cached information will likely be incorrect now.
    53         if self.id in SITE_CACHE:
    54             del SITE_CACHE[self.id]
     51        cache.delete('site-cache-%s' % self.id)
    5552
    5653    def delete(self):
    5754        pk = self.pk
    5855        super(Site, self).delete()
    59         try:
    60             del SITE_CACHE[pk]
    61         except KeyError:
    62             pass
     56        cache.delete('site-cache-%s' % self.id)
    6357
    6458
    6559class RequestSite(object):
  • docs/ref/contrib/sites.txt

    diff --git a/docs/ref/contrib/sites.txt b/docs/ref/contrib/sites.txt
    index 8fc434b..7762eac 100644
    a b fallback for cases where it is not installed. 
    178178    :class:`~django.contrib.sites.models.RequestSite` object based on
    179179    the request.
    180180
     181Note: ``get_current_site()`` and :attr:`~Site.objects.get_current()` use the cache backend specified in your CACHE_BACKEND setting.
     182
    181183Getting the current domain for display
    182184--------------------------------------
    183185
  • tests/regressiontests/sites_framework/tests.py

    diff --git a/tests/regressiontests/sites_framework/tests.py b/tests/regressiontests/sites_framework/tests.py
    index 8e664fd..ee0f277 100644
    a b  
    11from __future__ import absolute_import
    22
     3import time
     4from multiprocessing import Process, Queue, Value
     5
    36from django.conf import settings
    47from django.contrib.sites.models import Site
    58from django.test import TestCase
    class SitesFrameworkTestCase(TestCase): 
    3639    def test_invalid_field_type(self):
    3740        article = ConfusedArticle.objects.create(title="More Bad News!", site=settings.SITE_ID)
    3841        self.assertRaises(TypeError, ConfusedArticle.on_site.all)
     42
     43    def test_multiprocess(self):
     44        def check_if_domain_changed(phase, domain_changed):
     45            from django.contrib.sites.models import Site
     46
     47            # first phase - get domain
     48            site = Site.objects.get_current()
     49            first_try = site.domain
     50            phase.value = 2
     51
     52            # second phase - wait
     53            while phase.value == 2:
     54                pass
     55
     56            # third phase - again get domain
     57            s2 = Site.objects.get_current()
     58            second_try = s2.domain
     59
     60            # compare domains
     61            domain_changed.value = (first_try != second_try)
     62
     63        def change_domain(phase):
     64            from django.contrib.sites.models import Site
     65
     66            # first phase - wait
     67            while phase.value == 1:
     68                pass
     69
     70            # second phase
     71            site = Site.objects.get_current()
     72            site.domain = site.domain+'_changed'
     73            site.save()
     74            phase.value = 3
     75
     76        phase = Value('i', 1)
     77        domain_changed = Value('b', False)
     78        first_process = Process(target=check_if_domain_changed, args=(phase, domain_changed))
     79        second_process = Process(target=change_domain, args=(phase,))
     80
     81        first_process.start()
     82        second_process.start()
     83        first_process.join()
     84        second_process.join()
     85
     86        self.assertTrue(domain_changed.value)
Back to Top