-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[feat] Database Option #1559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] Database Option #1559
Changes from all commits
80d8e3c
b940a1d
10132b8
7291e81
0cd8171
a54fa42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,72 +8,112 @@ | |
|
||
import json | ||
|
||
from django.conf import settings | ||
from django.conf import settings as default_settings | ||
from django.core.management.base import BaseCommand, CommandError | ||
from django.views.debug import SafeExceptionReporterFilter | ||
|
||
from django_extensions.management.utils import signalcommand | ||
|
||
DEFAULT_FORMAT = "simple" | ||
DEFAULT_INDENT = 4 | ||
DEFAULT_SECRETS = False | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Print the active Django settings." | ||
|
||
def add_arguments(self, parser): | ||
super().add_arguments(parser) | ||
parser.add_argument( | ||
'setting', | ||
nargs='*', | ||
help='Specifies setting to be printed.' | ||
"setting", nargs="*", help="Specifies setting to be printed." | ||
) | ||
parser.add_argument( | ||
'--format', | ||
default='simple', | ||
dest='format', | ||
help='Specifies output format.' | ||
"--format", | ||
default=DEFAULT_FORMAT, | ||
dest="format", | ||
help="Specifies output format.", | ||
) | ||
parser.add_argument( | ||
'--indent', | ||
default=4, | ||
dest='indent', | ||
"--indent", | ||
default=DEFAULT_INDENT, | ||
dest="indent", | ||
type=int, | ||
help='Specifies indent level for JSON and YAML' | ||
help="Specifies indent level for JSON and YAML", | ||
) | ||
parser.add_argument( | ||
"--show-secrets", | ||
default=DEFAULT_SECRETS, | ||
dest="show secrets", | ||
type=bool, | ||
help="Specifies if should be revealed the value of secrets", | ||
) | ||
parser.add_argument( | ||
"--database", | ||
dest="show database", | ||
action="store_true", | ||
help="Shows the current database engine", | ||
) | ||
|
||
@signalcommand | ||
def handle(self, *args, **options): | ||
|
||
show_secrets, output_format, indent, show_database = self.get_defaults(options) | ||
|
||
a_dict = {} | ||
|
||
for attr in dir(settings): | ||
if self.include_attr(attr, options['setting']): | ||
value = getattr(settings, attr) | ||
a_dict[attr] = value | ||
if show_secrets: | ||
for attr in dir(default_settings): | ||
if self.include_attr(attr, options["setting"]): | ||
value = getattr(default_settings, attr) | ||
a_dict[attr] = value | ||
else: | ||
settings = self.safe_settings() | ||
for key in settings.keys(): | ||
if key in options["setting"] or not options["setting"]: | ||
a_dict[key] = settings.get(key) | ||
|
||
if show_database: | ||
database = self.safe_settings().get('DATABASES', None) | ||
if database: | ||
engine = database['default']['ENGINE'] | ||
a_dict['DATABASE_ENGINE'] = self.get_database(engine) | ||
|
||
for setting in args: | ||
if setting not in a_dict: | ||
raise CommandError('%s not found in settings.' % setting) | ||
raise CommandError("%s not found in settings." % setting) | ||
|
||
output_format = options['format'] | ||
indent = options['indent'] | ||
|
||
if output_format == 'json': | ||
if output_format == "json": | ||
print(json.dumps(a_dict, indent=indent)) | ||
elif output_format == 'yaml': | ||
elif output_format == "yaml": | ||
import yaml # requires PyYAML | ||
|
||
print(yaml.dump(a_dict, indent=indent)) | ||
elif output_format == 'pprint': | ||
elif output_format == "pprint": | ||
from pprint import pprint | ||
|
||
pprint(a_dict) | ||
elif output_format == 'text': | ||
elif output_format == "text": | ||
for key, value in a_dict.items(): | ||
print("%s = %s" % (key, value)) | ||
elif output_format == 'value': | ||
elif output_format == "value": | ||
for value in a_dict.values(): | ||
print(value) | ||
else: | ||
self.print_simple(a_dict) | ||
|
||
@staticmethod | ||
def get_defaults(options): | ||
a_options = [ | ||
options.get("show secrets", DEFAULT_SECRETS), | ||
options.get("format", DEFAULT_FORMAT), | ||
options.get("indent", DEFAULT_INDENT), | ||
options.get("show database", None) | ||
] | ||
return a_options | ||
|
||
@staticmethod | ||
def include_attr(attr, settings): | ||
if attr.startswith('__'): | ||
if attr.startswith("__"): | ||
return False | ||
elif settings == []: | ||
return True | ||
|
@@ -83,4 +123,24 @@ def include_attr(attr, settings): | |
@staticmethod | ||
def print_simple(a_dict): | ||
for key, value in a_dict.items(): | ||
print('%-40s = %r' % (key, value)) | ||
print("%-40s = %r" % (key, value)) | ||
|
||
@staticmethod | ||
def safe_settings(): | ||
try: | ||
return SafeExceptionReporterFilter().get_safe_settings() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If django < 3.0 does not have this class then the import above will already have failed and this code is meaningless. Either the comment below is wrong or we should catch FYI: This entire code can be replaced to function at startup time which seem to be preferred to this way if it served no additional function. |
||
except AttributeError: | ||
# In django < 3.0 does not have this class | ||
# We have to make a different import | ||
from django.views.debug import get_safe_settings | ||
return get_safe_settings() | ||
|
||
@staticmethod | ||
def get_database(engine): | ||
db_backend_options = { | ||
'django.db.backends.postgresql': 'PostgreSQL', | ||
'django.db.backends.sqlite3': 'SQLite', | ||
'django.db.backends.oracle': 'Oracle', | ||
'django.db.backends.mysql': 'MySQL/MariaDB', | ||
} | ||
return db_backend_options.get(engine, 'UNKNOWN') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no spaces, just underscores :-)