Ticket #19698: 19698-2.patch

File 19698-2.patch, 8.0 KB (added by ddavies@…, 7 years ago)

Refactored the previous patch as Site no longer needed a save() method either.

  • django/contrib/sites/tests.py

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
    <+>from __future__ import unicode_literals\n\nfrom django.conf import settings\nfrom django.contrib.sites.models import Site, RequestSite, get_current_site\nfrom django.core.exceptions import ObjectDoesNotExist\nfrom django.http import HttpRequest\nfrom django.test import TestCase\n\n\nclass SitesFrameworkTests(TestCase):\n\n    def setUp(self):\n        Site(id=settings.SITE_ID, domain=\"example.com\", name=\"example.com\").save()\n        self.old_Site_meta_installed = Site._meta.installed\n        Site._meta.installed = True\n\n    def tearDown(self):\n        Site._meta.installed = self.old_Site_meta_installed\n\n    def test_save_another(self):\n        # Regression for #17415\n        # On some backends the sequence needs reset after save with explicit ID.\n        # Test that there is no sequence collisions by saving another site.\n        Site(domain=\"example2.com\", name=\"example2.com\").save()\n\n    def test_site_manager(self):\n        # Make sure that get_current() does not return a deleted Site object.\n        s = Site.objects.get_current()\n        self.assertTrue(isinstance(s, Site))\n        s.delete()\n        self.assertRaises(ObjectDoesNotExist, Site.objects.get_current)\n\n    def test_site_cache(self):\n        # After updating a Site object (e.g. via the admin), we shouldn't return a\n        # bogus value from the SITE_CACHE.\n        site = Site.objects.get_current()\n        self.assertEqual(\"example.com\", site.name)\n        s2 = Site.objects.get(id=settings.SITE_ID)\n        s2.name = \"Example site\"\n        s2.save()\n        site = Site.objects.get_current()\n        self.assertEqual(\"Example site\", site.name)\n\n    def test_get_current_site(self):\n        # Test that the correct Site object is returned\n        request = HttpRequest()\n        request.META = {\n            \"SERVER_NAME\": \"example.com\",\n            \"SERVER_PORT\": \"80\",\n        }\n        site = get_current_site(request)\n        self.assertTrue(isinstance(site, Site))\n        self.assertEqual(site.id, settings.SITE_ID)\n\n        # Test that an exception is raised if the sites framework is installed\n        # but there is no matching Site\n        site.delete()\n        self.assertRaises(ObjectDoesNotExist, get_current_site, request)\n\n        # A RequestSite is returned if the sites framework is not installed\n        Site._meta.installed = False\n        site = get_current_site(request)\n        self.assertTrue(isinstance(site, RequestSite))\n        self.assertEqual(site.name, \"example.com\")\n
    4141        site = Site.objects.get_current()
    4242        self.assertEqual("Example site", site.name)
     44    def test_delete_all_sites_clears_cache(self):
     45        # When all site objects are deleted the cache should also
     46        # be cleared and get_current should raise a DoesNotExist
     47        self.assertIsInstance(Site.objects.get_current(), Site)
     48        Site.objects.all().delete()
     49        self.assertRaises(Site.DoesNotExist, Site.objects.get_current)
    4451    def test_get_current_site(self):
    4552        # Test that the correct Site object is returned
    4653        request = HttpRequest()
  • django/contrib/sites/models.py

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    Subsystem: com.intellij.openapi.diff.impl.patch.BaseRevisionTextPatchEP
    <+>from django.db import models\nfrom django.utils.translation import ugettext_lazy as _\nfrom django.utils.encoding import python_2_unicode_compatible\n\n\nSITE_CACHE = {}\n\n\nclass SiteManager(models.Manager):\n\n    def get_current(self):\n        \"\"\"\n        Returns the current ``Site`` based on the SITE_ID in the\n        project's settings. The ``Site`` object is cached the first\n        time it's retrieved from the database.\n        \"\"\"\n        from django.conf import settings\n        try:\n            sid = settings.SITE_ID\n        except AttributeError:\n            from django.core.exceptions import ImproperlyConfigured\n            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.\")\n        try:\n            current_site = SITE_CACHE[sid]\n        except KeyError:\n            current_site = self.get(pk=sid)\n            SITE_CACHE[sid] = current_site\n        return current_site\n\n    def clear_cache(self):\n        \"\"\"Clears the ``Site`` object cache.\"\"\"\n        global SITE_CACHE\n        SITE_CACHE = {}\n\n\n@python_2_unicode_compatible\nclass Site(models.Model):\n\n    domain = models.CharField(_('domain name'), max_length=100)\n    name = models.CharField(_('display name'), max_length=50)\n    objects = SiteManager()\n\n    class Meta:\n        db_table = 'django_site'\n        verbose_name = _('site')\n        verbose_name_plural = _('sites')\n        ordering = ('domain',)\n\n    def __str__(self):\n        return self.domain\n\n    def save(self, *args, **kwargs):\n        super(Site, self).save(*args, **kwargs)\n        # Cached information will likely be incorrect now.\n        if self.id in SITE_CACHE:\n            del SITE_CACHE[self.id]\n\n    def delete(self):\n        pk = self.pk\n        super(Site, self).delete()\n        try:\n            del SITE_CACHE[pk]\n        except KeyError:\n            pass\n\n\n@python_2_unicode_compatible\nclass RequestSite(object):\n    \"\"\"\n    A class that shares the primary interface of Site (i.e., it has\n    ``domain`` and ``name`` attributes) but gets its data from a Django\n    HttpRequest object rather than from a database.\n\n    The save() and delete() methods raise NotImplementedError.\n    \"\"\"\n    def __init__(self, request):\n        self.domain = self.name = request.get_host()\n\n    def __str__(self):\n        return self.domain\n\n    def save(self, force_insert=False, force_update=False):\n        raise NotImplementedError('RequestSite cannot be saved.')\n\n    def delete(self):\n        raise NotImplementedError('RequestSite cannot be deleted.')\n\n\ndef get_current_site(request):\n    \"\"\"\n    Checks if contrib.sites is installed and returns either the current\n    ``Site`` object or a ``RequestSite`` object based on the request.\n    \"\"\"\n    if Site._meta.installed:\n        current_site = Site.objects.get_current()\n    else:\n        current_site = RequestSite(request)\n    return current_site\n
    11from django.db import models
     2from django.db.models.signals import pre_save, pre_delete
     3from django.dispatch import receiver
    24from django.utils.translation import ugettext_lazy as _
    35from django.utils.encoding import python_2_unicode_compatible
    4951    def __str__(self):
    5052        return self.domain
    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]
    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
     55@receiver(pre_save, sender=Site)
     56def clear_cache(sender, **kwargs):
     57    """
     58    Clears the cache (if primed) each time a site is saved or deleted
     59    """
     60    instance = kwargs['instance']
     61    try:
     62        del SITE_CACHE[instance.pk]
     63    except KeyError:
     64        pass
Back to Top