Reformat django settings export

This commit is contained in:
Mark Veidemanis 2022-10-12 07:22:22 +01:00
parent 68448c5926
commit b8f5d5a1cc
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 10 additions and 19 deletions

View File

@ -7,13 +7,10 @@ https://github.com/jakubroztocil/django-settings-export
from django.conf import settings as django_settings from django.conf import settings as django_settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
__version__ = "1.2.1"
__version__ = '1.2.1'
VARIABLE_NAME = getattr(django_settings, VARIABLE_NAME = getattr(django_settings, "SETTINGS_EXPORT_VARIABLE_NAME", "settings")
'SETTINGS_EXPORT_VARIABLE_NAME',
'settings')
class SettingsExportError(ImproperlyConfigured): class SettingsExportError(ImproperlyConfigured):
@ -35,16 +32,13 @@ def settings_export(request):
set, the context variable will be `settings`. set, the context variable will be `settings`.
""" """
variable_name = getattr(django_settings, variable_name = getattr(
'SETTINGS_EXPORT_VARIABLE_NAME', django_settings, "SETTINGS_EXPORT_VARIABLE_NAME", "settings"
'settings') )
return { return {variable_name: _get_exported_settings()}
variable_name: _get_exported_settings()
}
class ExportedSettings(dict): class ExportedSettings(dict):
def __getitem__(self, item): def __getitem__(self, item):
"""Fail loudly if accessing a setting that is not exported.""" """Fail loudly if accessing a setting that is not exported."""
try: try:
@ -55,24 +49,21 @@ class ExportedSettings(dict):
# can access the existing attribute (e.g. `items()`). # can access the existing attribute (e.g. `items()`).
raise raise
raise UnexportedSettingError( raise UnexportedSettingError(
'The `{key}` setting key is not accessible' "The `{key}` setting key is not accessible"
' from templates: add "{key}" to' ' from templates: add "{key}" to'
' `settings.SETTINGS_EXPORT` to change that.' " `settings.SETTINGS_EXPORT` to change that.".format(key=item)
.format(key=item)
) )
def _get_exported_settings(): def _get_exported_settings():
exported_settings = ExportedSettings() exported_settings = ExportedSettings()
for key in getattr(django_settings, 'SETTINGS_EXPORT', []): for key in getattr(django_settings, "SETTINGS_EXPORT", []):
try: try:
value = getattr(django_settings, key) value = getattr(django_settings, key)
except AttributeError: except AttributeError:
raise UndefinedSettingError( raise UndefinedSettingError(
'"settings.%s" is included in settings.SETTINGS_EXPORT ' '"settings.%s" is included in settings.SETTINGS_EXPORT '
'but it does not exist. ' "but it does not exist. " % key
% key
) )
exported_settings[key] = value exported_settings[key] = value
return exported_settings return exported_settings