Ticket #8329: startproject_disabled_warning.patch

File startproject_disabled_warning.patch, 3.2 KB (added by msabramo, 5 years ago)

Patch to display warning when "startproject" subcommand is disabled because of DJANGO_SETTINGS_MODULE

  • django/core/management/__init__.py

    diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
    index 85bf324..0e69364 100644
    a b def get_commands(): 
    102102        except (AttributeError, EnvironmentError, ImportError):
    103103            apps = []
    104104
    105         # Find the project directory
    106         try:
    107             from django.conf import settings
    108             module = import_module(settings.SETTINGS_MODULE)
    109             project_directory = setup_environ(module, settings.SETTINGS_MODULE)
    110         except (AttributeError, EnvironmentError, ImportError, KeyError):
    111             project_directory = None
    112 
    113105        # Find and load the management module for each installed app.
    114106        for app_name in apps:
    115107            try:
    def get_commands(): 
    119111            except ImportError:
    120112                pass # No management module - ignore this app
    121113
     114        project_directory = get_project_directory()
    122115        if project_directory:
    123             # Remove the "startproject" command from self.commands, because
    124             # that's a django-admin.py command, not a manage.py command.
    125             del _commands['startproject']
     116            _commands = revise_commands_for_manage_py(_commands, project_directory)
     117
     118    return _commands
     119
     120def revise_commands_for_manage_py(_commands, project_directory):
     121    # Remove the "startproject" command, because that's a
     122    # django-admin.py command, not a manage.py command.
     123    del _commands['startproject']
    126124
    127             # Override the startapp command so that it always uses the
    128             # project_directory, not the current working directory
    129             # (which is default).
    130             from django.core.management.commands.startapp import ProjectCommand
    131             _commands['startapp'] = ProjectCommand(project_directory)
     125    # Override the startapp command so that it always uses the
     126    # project_directory, not the current working directory
     127    # (which is default).
     128    from django.core.management.commands.startapp import ProjectCommand
     129    _commands['startapp'] = ProjectCommand(project_directory)
    132130
    133131    return _commands
    134132
     133def get_project_directory():
     134    """
     135    Find the project directory
     136    """
     137    try:
     138        from django.conf import settings
     139        module = import_module(settings.SETTINGS_MODULE)
     140        project_directory = setup_environ(module, settings.SETTINGS_MODULE)
     141    except (AttributeError, EnvironmentError, ImportError, KeyError):
     142        project_directory = None
     143
     144    return project_directory
     145
    135146def call_command(name, *args, **options):
    136147    """
    137148    Calls the given command, with the given options and args/kwargs.
    class ManagementUtility(object): 
    240251        commands.sort()
    241252        for cmd in commands:
    242253            usage.append('  %s' % cmd)
     254        project_directory = get_project_directory()
     255        if project_directory and os.getenv('DJANGO_SETTINGS_MODULE'):
     256            usage.append('\nNote that the "startproject" subcommand is disabled, ' +
     257                         'because the DJANGO_SETTINGS_MODULE environment variable is set ' +
     258                         '(to %r).' % os.getenv('DJANGO_SETTINGS_MODULE'))
    243259        return '\n'.join(usage)
    244260
    245261    def fetch_command(self, subcommand):
Back to Top