2014-06-18 07:07:54 +08:00
|
|
|
from django.core.management.base import BaseCommand
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2019-01-12 00:11:36 +08:00
|
|
|
def module_to_dict(module, omittable=lambda k: k.startswith('_') or not k.isupper()):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Convert a module namespace to a Python dictionary."""
|
2019-01-12 00:11:36 +08:00
|
|
|
return {k: repr(getattr(module, k)) for k in dir(module) if not omittable(k)}
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2014-06-18 07:07:54 +08:00
|
|
|
class Command(BaseCommand):
|
2007-08-16 14:06:55 +08:00
|
|
|
help = """Displays differences between the current settings.py and Django's
|
2017-03-08 16:54:17 +08:00
|
|
|
default settings."""
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2020-05-14 06:00:41 +08:00
|
|
|
requires_system_checks = []
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2014-06-07 04:39:33 +08:00
|
|
|
def add_arguments(self, parser):
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--all', action='store_true',
|
2017-03-08 16:54:17 +08:00
|
|
|
help=(
|
|
|
|
'Display all settings, regardless of their value. In "hash" '
|
|
|
|
'mode, default values are prefixed by "###".'
|
|
|
|
),
|
2016-03-29 06:33:29 +08:00
|
|
|
)
|
2016-11-05 19:31:37 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--default', metavar='MODULE',
|
2016-11-05 19:31:37 +08:00
|
|
|
help=(
|
|
|
|
"The settings module to compare the current settings against. Leave empty to "
|
|
|
|
"compare against Django's default settings."
|
|
|
|
),
|
|
|
|
)
|
2017-03-08 16:54:17 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--output', default='hash', choices=('hash', 'unified'),
|
2017-03-08 16:54:17 +08:00
|
|
|
help=(
|
|
|
|
"Selects the output format. 'hash' mode displays each changed "
|
|
|
|
"setting, with the settings that don't appear in the defaults "
|
|
|
|
"followed by ###. 'unified' mode prefixes the default setting "
|
|
|
|
"with a minus sign, followed by the changed setting prefixed "
|
|
|
|
"with a plus sign."
|
|
|
|
),
|
|
|
|
)
|
2014-06-07 04:39:33 +08:00
|
|
|
|
2014-06-18 07:07:54 +08:00
|
|
|
def handle(self, **options):
|
2020-07-24 14:25:47 +08:00
|
|
|
from django.conf import Settings, global_settings, settings
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Because settings are imported lazily, we need to explicitly load them.
|
2018-08-19 03:27:39 +08:00
|
|
|
if not settings.configured:
|
|
|
|
settings._setup()
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2009-03-03 10:48:02 +08:00
|
|
|
user_settings = module_to_dict(settings._wrapped)
|
2016-11-05 19:31:37 +08:00
|
|
|
default = options['default']
|
|
|
|
default_settings = module_to_dict(Settings(default) if default else global_settings)
|
2017-03-08 16:54:17 +08:00
|
|
|
output_func = {
|
|
|
|
'hash': self.output_hash,
|
|
|
|
'unified': self.output_unified,
|
|
|
|
}[options['output']]
|
|
|
|
return '\n'.join(output_func(user_settings, default_settings, **options))
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2017-03-08 16:54:17 +08:00
|
|
|
def output_hash(self, user_settings, default_settings, **options):
|
|
|
|
# Inspired by Postfix's "postconf -n".
|
2007-08-16 14:06:55 +08:00
|
|
|
output = []
|
2013-03-18 03:48:30 +08:00
|
|
|
for key in sorted(user_settings):
|
2007-08-16 14:06:55 +08:00
|
|
|
if key not in default_settings:
|
|
|
|
output.append("%s = %s ###" % (key, user_settings[key]))
|
|
|
|
elif user_settings[key] != default_settings[key]:
|
|
|
|
output.append("%s = %s" % (key, user_settings[key]))
|
2013-03-18 03:48:30 +08:00
|
|
|
elif options['all']:
|
|
|
|
output.append("### %s = %s" % (key, user_settings[key]))
|
2017-03-08 16:54:17 +08:00
|
|
|
return output
|
|
|
|
|
|
|
|
def output_unified(self, user_settings, default_settings, **options):
|
|
|
|
output = []
|
|
|
|
for key in sorted(user_settings):
|
|
|
|
if key not in default_settings:
|
|
|
|
output.append(self.style.SUCCESS("+ %s = %s" % (key, user_settings[key])))
|
|
|
|
elif user_settings[key] != default_settings[key]:
|
|
|
|
output.append(self.style.ERROR("- %s = %s" % (key, default_settings[key])))
|
|
|
|
output.append(self.style.SUCCESS("+ %s = %s" % (key, user_settings[key])))
|
|
|
|
elif options['all']:
|
|
|
|
output.append(" %s = %s" % (key, user_settings[key]))
|
|
|
|
return output
|