Skip to content

Commit

Permalink
Suppressed the if Site._meta.installed pattern.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aaugustin committed Feb 1, 2014
1 parent 4a488c1 commit f9698c4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
5 changes: 3 additions & 2 deletions django/contrib/auth/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions django/contrib/contenttypes/views.py
Original file line number Diff line number Diff line change
@@ -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 _

Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions django/contrib/sitemaps/__init__.py
Original file line number Diff line number Diff line change
@@ -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"


Expand Down Expand Up @@ -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})
Expand Down Expand Up @@ -75,7 +79,8 @@ def get_urls(self, page=1, site=None, protocol=None):

# 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:
Expand Down Expand Up @@ -111,6 +116,9 @@ def get_urls(self, page=1, site=None, protocol=None):

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)

Expand Down
6 changes: 3 additions & 3 deletions django/contrib/sitemaps/tests/base.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -20,7 +19,8 @@ def get_absolute_url(self):

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):
Expand Down

0 comments on commit f9698c4

Please sign in to comment.