Code

Ticket #5943: django-admin-commands.diff

File django-admin-commands.diff, 3.9 KB (added by jkocherhans, 6 years ago)
Line 
1Index: django/core/management/__init__.py
2===================================================================
3--- django/core/management/__init__.py  (revision 7361)
4+++ django/core/management/__init__.py  (working copy)
5@@ -52,7 +52,7 @@
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@@ -80,15 +80,25 @@
15     if _commands is None:
16         _commands = dict([(name, 'django.core') for name in find_commands(__path__[0])])
17 
18-        if load_user_commands:
19+        # Get commands from all installed apps.
20+        try:
21             # Get commands from all installed apps.
22             from django.conf import settings
23-            for app_name in settings.INSTALLED_APPS:
24-                try:
25-                    path = find_management_module(app_name)
26-                    _commands.update(dict([(name, app_name) for name in find_commands(path)]))
27-                except ImportError:
28-                    pass # No management module -- ignore this app.
29+            apps = settings.INSTALLED_APPS
30+        except (AttributeError, EnvironmentError):
31+            apps = []
32+        try:
33+            from django.conf import settings
34+            project_directory = setup_environ(__import__(settings.SETTINGS_MODULE))
35+        except (AttributeError, EnvironmentError, ImportError):
36+            project_directory = None
37+        for app_name in apps:
38+            try:
39+                path = find_management_module(app_name)
40+                _commands.update(dict([(name, app_name)
41+                                       for name in find_commands(path)]))
42+            except ImportError:
43+                pass # No management module - ignore this app
44 
45         if project_directory:
46             # Remove the "startproject" command from self.commands, because
47@@ -145,8 +155,6 @@
48     def __init__(self, argv=None):
49         self.argv = argv or sys.argv[:]
50         self.prog_name = os.path.basename(self.argv[0])
51-        self.project_directory = None
52-        self.user_commands = False
53 
54     def main_help_text(self):
55         """
56@@ -156,7 +164,7 @@
57         usage.append('Django command line tool, version %s' % django.get_version())
58         usage.append("Type '%s help <subcommand>' for help on a specific subcommand." % self.prog_name)
59         usage.append('Available subcommands:')
60-        commands = get_commands(self.user_commands, self.project_directory).keys()
61+        commands = get_commands().keys()
62         commands.sort()
63         for cmd in commands:
64             usage.append('  %s' % cmd)
65@@ -169,7 +177,7 @@
66         "django-admin.py" or "manage.py") if it can't be found.
67         """
68         try:
69-            app_name = get_commands(self.user_commands, self.project_directory)[subcommand]
70+            app_name = get_commands()[subcommand]
71             if isinstance(app_name, BaseCommand):
72                 # If the command is already loaded, use it directly.
73                 klass = app_name
74@@ -229,8 +237,6 @@
75     """
76     def __init__(self, argv, project_directory):
77         super(ProjectManagementUtility, self).__init__(argv)
78-        self.project_directory = project_directory
79-        self.user_commands = True
80 
81 def setup_environ(settings_mod):
82     """
83@@ -251,8 +257,9 @@
84     project_module = __import__(project_name, {}, {}, [''])
85     sys.path.pop()
86 
87-    # Set DJANGO_SETTINGS_MODULE appropriately.
88-    os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
89+    # Set DJANGO_SETTINGS_MODULE appropriately if it hasn't already been set.
90+    if not os.environ.has_key('DJANGO_SETTINGS_MODULE'):
91+        os.environ['DJANGO_SETTINGS_MODULE'] = '%s.%s' % (project_name, settings_name)
92     return project_directory
93 
94 def execute_from_command_line(argv=None):