Skip to content

Commit

Permalink
Fix django.contrib.sites detection in set_default_site command when u…
Browse files Browse the repository at this point in the history
…sing AppConfig in INSTALLED_APPS: (#1662)

django.contrib.sites application can be specified in installed
application list either via its module path, or via its AppConfig
path. This can be either the default
one (django.contrib.sites.apps.SitesConfig), or user-provided
AppConfig class.

Instead of relying on a fixed name in the settings files, use the
application registry to check if the sites application is installed.
  • Loading branch information
azaghal committed May 2, 2021
1 parent c17f8a5 commit 1a1e50e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion django_extensions/management/commands/set_default_site.py
Expand Up @@ -3,6 +3,7 @@

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.apps import apps

from django_extensions.management.utils import signalcommand

Expand All @@ -29,7 +30,7 @@ def add_arguments(self, parser):

@signalcommand
def handle(self, *args, **options):
if 'django.contrib.sites' not in settings.INSTALLED_APPS:
if not apps.is_installed('django.contrib.sites'):
raise CommandError('The sites framework is not installed.')

from django.contrib.sites.models import Site
Expand Down
7 changes: 7 additions & 0 deletions tests/management/commands/test_set_default_site.py
Expand Up @@ -82,3 +82,10 @@ def test_should_set_domain_only(self):

self.assertEqual(result.name, 'example.com')
self.assertEqual(result.domain, 'bar')

def test_should_not_raise_if_sites_installed_through_appconfig(self):
with self.modify_settings(INSTALLED_APPS={
'append': 'django.contrib.sites.apps.SitesConfig',
'remove': 'django.contrib.sites',
}):
call_command('set_default_site', '--name=foo', '--domain=foo.bar')

0 comments on commit 1a1e50e

Please sign in to comment.