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.
This commit is contained in:
Aymeric Augustin 2014-02-01 17:37:08 +01:00
parent 4a488c1483
commit f9698c4391
4 changed files with 31 additions and 16 deletions

View File

@ -3,8 +3,8 @@ import itertools
import os import os
import re import re
from django.apps import apps
from django.conf import global_settings, settings from django.conf import global_settings, settings
from django.contrib.sites.models import Site
from django.contrib.sites.requests import RequestSite from django.contrib.sites.requests import RequestSite
from django.contrib.admin.models import LogEntry from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -446,7 +446,8 @@ class LoginTest(AuthViewsTestCase):
def test_current_site_in_context_after_login(self): def test_current_site_in_context_after_login(self):
response = self.client.get(reverse('login')) response = self.client.get(reverse('login'))
self.assertEqual(response.status_code, 200) 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() site = Site.objects.get_current()
self.assertEqual(response.context['site'], site) self.assertEqual(response.context['site'], site)
self.assertEqual(response.context['site_name'], site.name) self.assertEqual(response.context['site_name'], site.name)

View File

@ -1,9 +1,9 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django import http from django import http
from django.apps import apps
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site from django.contrib.sites.requests import RequestSite
from django.contrib.sites.shortcuts import get_current_site
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
@ -41,7 +41,9 @@ def shortcut(request, content_type_id, object_id):
# relation to the Site object # relation to the Site object
object_domain = None object_domain = None
if Site._meta.installed: if apps.is_installed('django.contrib.sites'):
Site = apps.get_model('sites.Site')
opts = obj._meta opts = obj._meta
# First, look for an many-to-many relationship to Site. # 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: if object_domain is not None:
break break
# Fall back to the current site (if possible). # Fall back to the current site (if possible).
if object_domain is None: if object_domain is None:
try: try:
object_domain = get_current_site(request).domain object_domain = Site.objects.get_current().domain
except Site.DoesNotExist: except Site.DoesNotExist:
pass 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 # If all that malarkey found an object domain, use it. Otherwise, fall back
# to whatever get_absolute_url() returned. # to whatever get_absolute_url() returned.

View File

@ -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 import urlresolvers, paginator
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.utils.six.moves.urllib.parse import urlencode from django.utils.six.moves.urllib.parse import urlencode
from django.utils.six.moves.urllib.request import urlopen from django.utils.six.moves.urllib.request import urlopen
PING_URL = "http://www.google.com/webmasters/tools/ping" 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: if sitemap_url is None:
raise SitemapNotFound("You didn't provide a sitemap_url, and the sitemap URL couldn't be auto-detected.") 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() current_site = Site.objects.get_current()
url = "http://%s%s" % (current_site.domain, sitemap_url) url = "http://%s%s" % (current_site.domain, sitemap_url)
params = urlencode({'sitemap': url}) params = urlencode({'sitemap': url})
@ -75,7 +79,8 @@ class Sitemap(object):
# Determine domain # Determine domain
if site is None: if site is None:
if Site._meta.installed: if django_apps.is_installed('django.contrib.sites'):
Site = django_apps.get_model('sites.Site')
try: try:
site = Site.objects.get_current() site = Site.objects.get_current()
except Site.DoesNotExist: except Site.DoesNotExist:
@ -111,6 +116,9 @@ class Sitemap(object):
class FlatPageSitemap(Sitemap): class FlatPageSitemap(Sitemap):
def items(self): 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() current_site = Site.objects.get_current()
return current_site.flatpage_set.filter(registration_required=False) return current_site.flatpage_set.filter(registration_required=False)

View File

@ -1,11 +1,10 @@
from django.contrib.sites.models import Site from django.apps import apps
from django.core.cache import cache from django.core.cache import cache
from django.db import models from django.db import models
from django.test import TestCase from django.test import TestCase
class TestModel(models.Model): class TestModel(models.Model):
"A test model for "
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
class Meta: class Meta:
@ -20,7 +19,8 @@ class TestModel(models.Model):
class SitemapTestsBase(TestCase): class SitemapTestsBase(TestCase):
protocol = 'http' 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' urls = 'django.contrib.sitemaps.tests.urls.http'
def setUp(self): def setUp(self):