1
0
mirror of https://github.com/django/django.git synced 2025-04-22 00:04:43 +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]
except KeyError:
if os.environ.get("DJANGO_SETTINGS_MODULE"):
# 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
if self.settings_exception:
sys.stderr.write(str(self.settings_exception) + "\n")
sys.exit(1)
else:
# 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:
sys.stderr.write("No Django settings specified.\n")
possible_matches = get_close_matches(subcommand, commands)
@ -273,6 +277,8 @@ class ManagementUtility:
klass = app_name
else:
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
def autocomplete(self):
@ -382,9 +388,6 @@ class ManagementUtility:
settings.INSTALLED_APPS
except ImproperlyConfigured as 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:
# 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
output_transaction = False # Whether to wrap the output in a "BEGIN; COMMIT;"
requires_migrations_checks = False
requires_settings = True
requires_system_checks = "__all__"
# Arguments, common to all commands, which aren't defined by the argument
# parser.

View File

@ -208,6 +208,7 @@ class Command(BaseCommand):
translatable_file_class = TranslatableFile
build_file_class = BuildFile
requires_settings = False
requires_system_checks = []
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."
)
missing_args_message = "You must provide an application name."
requires_settings = False
def handle(self, **options):
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."
)
missing_args_message = "You must provide a project name."
requires_settings = False
def handle(self, **options):
project_name = options.pop("name")

View File

@ -1223,7 +1223,9 @@ class ManageAlternateSettings(AdminScriptTestCase):
out, err = self.run_manage(args)
self.assertNoOutput(out)
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):
@ -1423,7 +1425,7 @@ class ManageSettingsWithSettingsErrors(AdminScriptTestCase):
args = ["help"]
out, err = self.run_manage(args)
self.assertOutput(out, "only Django core commands are listed")
self.assertOutput(err, "Improper configuration")
self.assertNoOutput(err)
class ManageCheck(AdminScriptTestCase):