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
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
def module_to_dict(module, omittable=lambda k: k.startswith('_')):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Convert a module namespace to a Python dictionary."""
|
2014-12-07 05:00:09 +08:00
|
|
|
return {k: repr(v) for k, v in module.__dict__.items() 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
|
|
|
|
default settings. Settings that don't appear in the defaults are
|
|
|
|
followed by "###"."""
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
requires_system_checks = False
|
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(
|
2017-04-02 08:03:56 +08:00
|
|
|
'--all', action='store_true', dest='all',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Display all settings, regardless of their value. Default values are prefixed by "###".',
|
|
|
|
)
|
2016-11-05 19:31:37 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--default', dest='default', metavar='MODULE', default=None,
|
|
|
|
help=(
|
|
|
|
"The settings module to compare the current settings against. Leave empty to "
|
|
|
|
"compare against Django's default settings."
|
|
|
|
),
|
|
|
|
)
|
2014-06-07 04:39:33 +08:00
|
|
|
|
2014-06-18 07:07:54 +08:00
|
|
|
def handle(self, **options):
|
2007-08-16 14:06:55 +08:00
|
|
|
# Inspired by Postfix's "postconf -n".
|
2016-11-05 19:31:37 +08:00
|
|
|
from django.conf import settings, Settings, global_settings
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
# Because settings are imported lazily, we need to explicitly load them.
|
2009-03-03 10:48:02 +08:00
|
|
|
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)
|
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]))
|
2010-02-22 07:39:27 +08:00
|
|
|
return '\n'.join(output)
|