summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2014-05-27 18:57:53 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2014-05-27 21:46:30 +0200
commit430faebe3baa3daecc0ea083c1259c2b43b0557f (patch)
tree885894a080f76222cfc5cfded77f141dcfa38607
parentc0a56001e0e5c35615842f4ed223b7baeb234730 (diff)
[1.7.x] Fixed #22699 -- Configure default settings in some management commands.
This makes it possible to run django.setup() in management commands that don't need a settings module. In addition it simplifies error handling. Thanks Claude for the review. Backport of 4865326f from master.
-rw-r--r--django/core/management/__init__.py53
-rw-r--r--tests/admin_scripts/custom_templates/project_template/additional_dir/localized.py2
2 files changed, 30 insertions, 25 deletions
diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
index b6a7d53a0c..81bafb4f5f 100644
--- a/django/core/management/__init__.py
+++ b/django/core/management/__init__.py
@@ -107,18 +107,11 @@ def get_commands():
"""
commands = {name: 'django.core' for name in find_commands(__path__[0])}
- # Find the installed apps
- try:
- settings.INSTALLED_APPS
- except ImproperlyConfigured:
- # Still useful for commands that do not require functional
- # settings, like startproject or help.
- app_names = []
- else:
- app_configs = apps.get_app_configs()
- app_names = [app_config.name for app_config in app_configs]
+ if not settings.configured:
+ return commands
+
+ app_names = [app_config.name for app_config in apps.get_app_configs()]
- # Find and load the management module for each installed app.
for app_name in reversed(app_names):
try:
path = find_management_module(app_name)
@@ -232,6 +225,7 @@ class ManagementUtility(object):
def __init__(self, argv=None):
self.argv = argv or sys.argv[:]
self.prog_name = os.path.basename(self.argv[0])
+ self.settings_exception = None
def main_help_text(self, commands_only=False):
"""
@@ -260,12 +254,11 @@ class ManagementUtility(object):
for name in sorted(commands_dict[app]):
usage.append(" %s" % name)
# Output an extra note if settings are not properly configured
- try:
- settings.INSTALLED_APPS
- except ImproperlyConfigured as e:
+ if self.settings_exception is not None:
usage.append(style.NOTICE(
- "Note that only Django core commands are listed as settings "
- "are not properly configured (error: %s)." % e))
+ "Note that only Django core commands are listed "
+ "as settings are not properly configured (error: %s)."
+ % self.settings_exception))
return '\n'.join(usage)
@@ -383,20 +376,30 @@ class ManagementUtility(object):
pass # Ignore any option errors at this point.
try:
+ subcommand = self.argv[1]
+ except IndexError:
+ subcommand = 'help' # Display help if no arguments were given.
+
+ no_settings_commands = [
+ 'help', 'version', '--help', '--version', '-h',
+ 'compilemessages', 'makemessages',
+ 'startapp', 'startproject',
+ ]
+
+ try:
settings.INSTALLED_APPS
- except ImproperlyConfigured:
- # Some commands are supposed to work without configured settings
- pass
- else:
+ except ImproperlyConfigured as exc:
+ self.settings_exception = exc
+ # A handful of built-in management commands work without settings.
+ # Load the default settings -- where INSTALLED_APPS is empty.
+ if subcommand in no_settings_commands:
+ settings.configure()
+
+ if settings.configured:
django.setup()
self.autocomplete()
- try:
- subcommand = self.argv[1]
- except IndexError:
- subcommand = 'help' # Display help if no arguments were given.
-
if subcommand == 'help':
if len(args) <= 2:
parser.print_lax_help()
diff --git a/tests/admin_scripts/custom_templates/project_template/additional_dir/localized.py b/tests/admin_scripts/custom_templates/project_template/additional_dir/localized.py
new file mode 100644
index 0000000000..ab23cde161
--- /dev/null
+++ b/tests/admin_scripts/custom_templates/project_template/additional_dir/localized.py
@@ -0,0 +1,2 @@
+# Regression for #22699.
+# Generated at {% now "DATE_FORMAT" %}