Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

diffsettings.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from django.core.management.base import BaseCommand
  2. def module_to_dict(module, omittable=lambda k: k.startswith('_')):
  3. """Convert a module namespace to a Python dictionary."""
  4. return {k: repr(v) for k, v in module.__dict__.items() if not omittable(k)}
  5. class Command(BaseCommand):
  6. help = """Displays differences between the current settings.py and Django's
  7. default settings."""
  8. requires_system_checks = False
  9. def add_arguments(self, parser):
  10. parser.add_argument(
  11. '--all', action='store_true', dest='all',
  12. help=(
  13. 'Display all settings, regardless of their value. In "hash" '
  14. 'mode, default values are prefixed by "###".'
  15. ),
  16. )
  17. parser.add_argument(
  18. '--default', dest='default', metavar='MODULE', default=None,
  19. help=(
  20. "The settings module to compare the current settings against. Leave empty to "
  21. "compare against Django's default settings."
  22. ),
  23. )
  24. parser.add_argument(
  25. '--output', default='hash', choices=('hash', 'unified'), dest='output',
  26. help=(
  27. "Selects the output format. 'hash' mode displays each changed "
  28. "setting, with the settings that don't appear in the defaults "
  29. "followed by ###. 'unified' mode prefixes the default setting "
  30. "with a minus sign, followed by the changed setting prefixed "
  31. "with a plus sign."
  32. ),
  33. )
  34. def handle(self, **options):
  35. from django.conf import settings, Settings, global_settings
  36. # Because settings are imported lazily, we need to explicitly load them.
  37. settings._setup()
  38. user_settings = module_to_dict(settings._wrapped)
  39. default = options['default']
  40. default_settings = module_to_dict(Settings(default) if default else global_settings)
  41. output_func = {
  42. 'hash': self.output_hash,
  43. 'unified': self.output_unified,
  44. }[options['output']]
  45. return '\n'.join(output_func(user_settings, default_settings, **options))
  46. def output_hash(self, user_settings, default_settings, **options):
  47. # Inspired by Postfix's "postconf -n".
  48. output = []
  49. for key in sorted(user_settings):
  50. if key not in default_settings:
  51. output.append("%s = %s ###" % (key, user_settings[key]))
  52. elif user_settings[key] != default_settings[key]:
  53. output.append("%s = %s" % (key, user_settings[key]))
  54. elif options['all']:
  55. output.append("### %s = %s" % (key, user_settings[key]))
  56. return output
  57. def output_unified(self, user_settings, default_settings, **options):
  58. output = []
  59. for key in sorted(user_settings):
  60. if key not in default_settings:
  61. output.append(self.style.SUCCESS("+ %s = %s" % (key, user_settings[key])))
  62. elif user_settings[key] != default_settings[key]:
  63. output.append(self.style.ERROR("- %s = %s" % (key, default_settings[key])))
  64. output.append(self.style.SUCCESS("+ %s = %s" % (key, user_settings[key])))
  65. elif options['all']:
  66. output.append(" %s = %s" % (key, user_settings[key]))
  67. return output