1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

add requires_settings attribute

This commit is contained in:
Ben Cail 2024-09-25 15:47:35 -04:00
parent eee0557112
commit 894b227d29
6 changed files with 19 additions and 10 deletions

View File

@ -255,11 +255,15 @@ class ManagementUtility:
app_name = commands[subcommand] app_name = commands[subcommand]
except KeyError: except KeyError:
if os.environ.get("DJANGO_SETTINGS_MODULE"): if os.environ.get("DJANGO_SETTINGS_MODULE"):
# If `subcommand` is missing due to misconfigured settings, the if self.settings_exception:
# following line will retrigger an ImproperlyConfigured exception sys.stderr.write(str(self.settings_exception) + "\n")
# (get_commands() swallows the original one) so the user is sys.exit(1)
# informed about it. else:
settings.INSTALLED_APPS # If `subcommand` is missing due to misconfigured settings, the
# following line will retrigger an ImproperlyConfigured exception
# (get_commands() swallows the original one) so the user is
# informed about it.
settings.INSTALLED_APPS
elif not settings.configured: elif not settings.configured:
sys.stderr.write("No Django settings specified.\n") sys.stderr.write("No Django settings specified.\n")
possible_matches = get_close_matches(subcommand, commands) possible_matches = get_close_matches(subcommand, commands)
@ -273,6 +277,8 @@ class ManagementUtility:
klass = app_name klass = app_name
else: else:
klass = load_command_class(app_name, subcommand) klass = load_command_class(app_name, subcommand)
if self.settings_exception and klass.requires_settings:
sys.stderr.write(str(self.settings_exception) + "\n")
return klass return klass
def autocomplete(self): def autocomplete(self):
@ -382,9 +388,6 @@ class ManagementUtility:
settings.INSTALLED_APPS settings.INSTALLED_APPS
except ImproperlyConfigured as exc: except ImproperlyConfigured as exc:
self.settings_exception = exc self.settings_exception = exc
# The following commands can be run without a valid settings file configured
if subcommand not in {"startproject", "startapp", "makemessages"}:
sys.stderr.write(str(exc) + "\n")
if settings.configured: if settings.configured:
# Start the auto-reloading dev server even if the code is broken. # Start the auto-reloading dev server even if the code is broken.

View File

@ -260,6 +260,7 @@ class BaseCommand:
_called_from_command_line = False _called_from_command_line = False
output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;" output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;"
requires_migrations_checks = False requires_migrations_checks = False
requires_settings = True
requires_system_checks = "__all__" requires_system_checks = "__all__"
# Arguments, common to all commands, which aren't defined by the argument # Arguments, common to all commands, which aren't defined by the argument
# parser. # parser.

View File

@ -208,6 +208,7 @@ class Command(BaseCommand):
translatable_file_class = TranslatableFile translatable_file_class = TranslatableFile
build_file_class = BuildFile build_file_class = BuildFile
requires_settings = False
requires_system_checks = [] requires_system_checks = []
msgmerge_options = ["-q", "--backup=none", "--previous", "--update"] msgmerge_options = ["-q", "--backup=none", "--previous", "--update"]

View File

@ -7,6 +7,7 @@ class Command(TemplateCommand):
"the current directory or optionally in the given directory." "the current directory or optionally in the given directory."
) )
missing_args_message = "You must provide an application name." missing_args_message = "You must provide an application name."
requires_settings = False
def handle(self, **options): def handle(self, **options):
app_name = options.pop("name") app_name = options.pop("name")

View File

@ -10,6 +10,7 @@ class Command(TemplateCommand):
"name in the current directory or optionally in the given directory." "name in the current directory or optionally in the given directory."
) )
missing_args_message = "You must provide a project name." missing_args_message = "You must provide a project name."
requires_settings = False
def handle(self, **options): def handle(self, **options):
project_name = options.pop("name") project_name = options.pop("name")

View File

@ -1223,7 +1223,9 @@ class ManageAlternateSettings(AdminScriptTestCase):
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertNoOutput(out) self.assertNoOutput(out)
self.assertOutput( self.assertOutput(
err, r"No module named '?(test_project\.)?settings'?", regex=True err,
r"Settings module '?(test_project\.)?settings'? could not be imported",
regex=True,
) )
def test_custom_command_with_settings(self): def test_custom_command_with_settings(self):
@ -1423,7 +1425,7 @@ class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
args = ["help"] args = ["help"]
out, err = self.run_manage(args) out, err = self.run_manage(args)
self.assertOutput(out, "only Django core commands are listed") self.assertOutput(out, "only Django core commands are listed")
self.assertOutput(err, "Improper configuration") self.assertNoOutput(err)
class ManageCheck(AdminScriptTestCase): class ManageCheck(AdminScriptTestCase):