Code

Ticket #5943: django-admin-settings-7229.diff

File django-admin-settings-7229.diff, 4.4 KB (added by jkocherhans, 6 years ago)
Line 
1diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
2index d78e2ed..77511e7 100644
3--- a/django/core/management/__init__.py
4+++ b/django/core/management/__init__.py
5@@ -52,7 +52,7 @@ def load_command_class(app_name, name):
6     return getattr(__import__('%s.management.commands.%s' % (app_name, name),
7                    {}, {}, ['Command']), 'Command')()
8 
9-def get_commands(load_user_commands=True, project_directory=None):
10+def get_commands():
11     """
12     Returns a dictionary mapping command names to their callback applications.
13 
14@@ -60,9 +60,9 @@ def get_commands(load_user_commands=True, project_directory=None):
15     in each installed application -- if a commands package exists, all commands
16     in that package are registered.
17 
18-    Core commands are always included. If a settings module has been
19-    specified, user-defined commands will also be included, the
20-    startproject command will be disabled, and the startapp command
21+    Core commands are always included. If a settings module has been
22+    specified, user-defined commands will also be included, the
23+    startproject command will be disabled, and the startapp command
24     will be modified to use the directory in which that module appears.
25 
26     The dictionary is in the format {command_name: app_name}. Key-value
27@@ -80,15 +80,25 @@ def get_commands(load_user_commands=True, project_directory=None):
28     if _commands is None:
29         _commands = dict([(name, 'django.core') for name in find_commands(__path__[0])])
30 
31-        if load_user_commands:
32+        # Get commands from all installed apps.
33+        try:
34             # Get commands from all installed apps.
35             from django.conf import settings
36-            for app_name in settings.INSTALLED_APPS:
37-                try:
38-                    path = find_management_module(app_name)
39-                    _commands.update(dict([(name, app_name) for name in find_commands(path)]))
40-                except ImportError:
41-                    pass # No management module -- ignore this app.
42+            apps = settings.INSTALLED_APPS
43+        except (AttributeError, EnvironmentError):
44+            apps = []
45+        try:
46+            from django.conf import settings
47+            project_directory = setup_environ(__import__(settings.SETTINGS_MODULE))
48+        except (AttributeError, EnvironmentError, ImportError):
49+            project_directory = None
50+        for app_name in apps:
51+            try:
52+                path = find_management_module(app_name)
53+                _commands.update(dict([(name, app_name)
54+                                       for name in find_commands(path)]))
55+            except ImportError:
56+                pass # No management module - ignore this app
57 
58         if project_directory:
59             # Remove the "startproject" command from self.commands, because
60@@ -145,8 +155,6 @@ class ManagementUtility(object):
61     def __init__(self, argv=None):
62         self.argv = argv or sys.argv[:]
63         self.prog_name = os.path.basename(self.argv[0])
64-        self.project_directory = None
65-        self.user_commands = False
66 
67     def main_help_text(self):
68         """
69@@ -156,7 +164,7 @@ class ManagementUtility(object):
70         usage.append('Django command line tool, version %s' % django.get_version())
71         usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name)
72         usage.append('Available subcommands:')
73-        commands = get_commands(self.user_commands, self.project_directory).keys()
74+        commands = get_commands().keys()
75         commands.sort()
76         for cmd in commands:
77             usage.append('  %s' % cmd)
78@@ -169,7 +177,7 @@ class ManagementUtility(object):
79         "django-admin.py" or "manage.py") if it can't be found.
80         """
81         try:
82-            app_name = get_commands(self.user_commands, self.project_directory)[subcommand]
83+            app_name = get_commands()[subcommand]
84             if isinstance(app_name, BaseCommand):
85                 # If the command is already loaded, use it directly.
86                 klass = app_name
87@@ -229,8 +237,6 @@ class ProjectManagementUtility(ManagementUtility):
88     """
89     def __init__(self, argv, project_directory):
90         super(ProjectManagementUtility, self).__init__(argv)
91-        self.project_directory = project_directory
92-        self.user_commands = True
93 
94 def setup_environ(settings_mod):
95     """