From f9698c43918c118a29516cbef4e23c197eb2dc25 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 1 Feb 2014 17:37:08 +0100 Subject: [PATCH] Suppressed the `if Site._meta.installed` pattern. The purpose of this construct is to test if the django.contrib.sites application is installed. But in Django 1.9 it will be forbidden to import the Site model when the django.contrib.sites application isn't installed. No model besides Site used this pattern. Refs #21719, #21923. --- django/contrib/auth/tests/test_views.py | 5 +++-- django/contrib/contenttypes/views.py | 24 +++++++++++++++--------- django/contrib/sitemaps/__init__.py | 12 ++++++++++-- django/contrib/sitemaps/tests/base.py | 6 +++--- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/django/contrib/auth/tests/test_views.py b/django/contrib/auth/tests/test_views.py index d2910886a9..d37afa3eee 100644 --- a/django/contrib/auth/tests/test_views.py +++ b/django/contrib/auth/tests/test_views.py @@ -3,8 +3,8 @@ import itertools import os import re +from django.apps import apps from django.conf import global_settings, settings -from django.contrib.sites.models import Site from django.contrib.sites.requests import RequestSite from django.contrib.admin.models import LogEntry from django.contrib.auth.models import User @@ -446,7 +446,8 @@ class LoginTest(AuthViewsTestCase): def test_current_site_in_context_after_login(self): response = self.client.get(reverse('login')) self.assertEqual(response.status_code, 200) - if Site._meta.installed: + if apps.is_installed('django.contrib.sites'): + Site = apps.get_model('sites.Site') site = Site.objects.get_current() self.assertEqual(response.context['site'], site) self.assertEqual(response.context['site_name'], site.name) diff --git a/django/contrib/contenttypes/views.py b/django/contrib/contenttypes/views.py index 22df1444a1..430ed800da 100644 --- a/django/contrib/contenttypes/views.py +++ b/django/contrib/contenttypes/views.py @@ -1,9 +1,9 @@ from __future__ import unicode_literals from django import http +from django.apps import apps from django.contrib.contenttypes.models import ContentType -from django.contrib.sites.models import Site -from django.contrib.sites.shortcuts import get_current_site +from django.contrib.sites.requests import RequestSite from django.core.exceptions import ObjectDoesNotExist from django.utils.translation import ugettext as _ @@ -41,7 +41,9 @@ def shortcut(request, content_type_id, object_id): # relation to the Site object object_domain = None - if Site._meta.installed: + if apps.is_installed('django.contrib.sites'): + Site = apps.get_model('sites.Site') + opts = obj._meta # First, look for an many-to-many relationship to Site. @@ -67,12 +69,16 @@ def shortcut(request, content_type_id, object_id): if object_domain is not None: break - # Fall back to the current site (if possible). - if object_domain is None: - try: - object_domain = get_current_site(request).domain - except Site.DoesNotExist: - pass + # Fall back to the current site (if possible). + if object_domain is None: + try: + object_domain = Site.objects.get_current().domain + except Site.DoesNotExist: + pass + + else: + # Fall back to the current request's site. + object_domain = RequestSite(request).domain # If all that malarkey found an object domain, use it. Otherwise, fall back # to whatever get_absolute_url() returned. diff --git a/django/contrib/sitemaps/__init__.py b/django/contrib/sitemaps/__init__.py index ee3d22090d..86bb71b402 100644 --- a/django/contrib/sitemaps/__init__.py +++ b/django/contrib/sitemaps/__init__.py @@ -1,9 +1,10 @@ -from django.contrib.sites.models import Site +from django.apps import apps as django_apps from django.core import urlresolvers, paginator from django.core.exceptions import ImproperlyConfigured from django.utils.six.moves.urllib.parse import urlencode from django.utils.six.moves.urllib.request import urlopen + PING_URL = "http://www.google.com/webmasters/tools/ping" @@ -32,6 +33,9 @@ def ping_google(sitemap_url=None, ping_url=PING_URL): if sitemap_url is None: raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") + if not django_apps.is_installed('django.contrib.sites'): + raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.") + Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() url = "http://%s%s" % (current_site.domain, sitemap_url) params = urlencode({'sitemap': url}) @@ -75,7 +79,8 @@ class Sitemap(object): # Determine domain if site is None: - if Site._meta.installed: + if django_apps.is_installed('django.contrib.sites'): + Site = django_apps.get_model('sites.Site') try: site = Site.objects.get_current() except Site.DoesNotExist: @@ -111,6 +116,9 @@ class Sitemap(object): class FlatPageSitemap(Sitemap): def items(self): + if not django_apps.is_installed('django.contrib.sites'): + raise ImproperlyConfigured("ping_google requires django.contrib.sites, which isn't installed.") + Site = django_apps.get_model('sites.Site') current_site = Site.objects.get_current() return current_site.flatpage_set.filter(registration_required=False) diff --git a/django/contrib/sitemaps/tests/base.py b/django/contrib/sitemaps/tests/base.py index ecddcc737b..cc92c023ce 100644 --- a/django/contrib/sitemaps/tests/base.py +++ b/django/contrib/sitemaps/tests/base.py @@ -1,11 +1,10 @@ -from django.contrib.sites.models import Site +from django.apps import apps from django.core.cache import cache from django.db import models from django.test import TestCase class TestModel(models.Model): - "A test model for " name = models.CharField(max_length=100) class Meta: @@ -20,7 +19,8 @@ class TestModel(models.Model): class SitemapTestsBase(TestCase): protocol = 'http' - domain = 'example.com' if Site._meta.installed else 'testserver' + sites_installed = apps.is_installed('django.contrib.sites') + domain = 'example.com' if sites_installed else 'testserver' urls = 'django.contrib.sitemaps.tests.urls.http' def setUp(self):