2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Settings and configuration for Django.
|
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
Read values from the module specified by the DJANGO_SETTINGS_MODULE environment
|
|
|
|
variable, and then from django.conf.global_settings; see the global_settings.py
|
|
|
|
for a list of all possible variables.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
|
2013-07-29 21:50:58 +08:00
|
|
|
import importlib
|
2006-05-02 09:31:56 +08:00
|
|
|
import os
|
2015-06-16 02:07:31 +08:00
|
|
|
import time
|
2021-09-09 13:42:05 +08:00
|
|
|
import traceback
|
2021-05-14 21:58:45 +08:00
|
|
|
import warnings
|
2017-07-22 04:33:26 +08:00
|
|
|
from pathlib import Path
|
2009-03-02 12:48:22 +08:00
|
|
|
|
2021-09-09 13:42:05 +08:00
|
|
|
import django
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import global_settings
|
2020-12-31 20:18:57 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2021-05-14 21:58:45 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango50Warning
|
2011-06-02 04:45:47 +08:00
|
|
|
from django.utils.functional import LazyObject, empty
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
|
|
|
|
|
2021-09-09 21:15:44 +08:00
|
|
|
# RemovedInDjango50Warning
|
|
|
|
USE_DEPRECATED_PYTZ_DEPRECATED_MSG = (
|
|
|
|
'The USE_DEPRECATED_PYTZ setting, and support for pytz timezones is '
|
|
|
|
'deprecated in favor of the stdlib zoneinfo module. Please update your '
|
|
|
|
'code to use zoneinfo and remove the USE_DEPRECATED_PYTZ setting.'
|
|
|
|
)
|
|
|
|
|
2021-09-09 13:42:05 +08:00
|
|
|
USE_L10N_DEPRECATED_MSG = (
|
|
|
|
'The USE_L10N setting is deprecated. Starting with Django 5.0, localized '
|
|
|
|
'formatting of data will always be enabled. For example Django will '
|
|
|
|
'display numbers and dates using the format of the current locale.'
|
|
|
|
)
|
|
|
|
|
2021-08-17 21:13:13 +08:00
|
|
|
CSRF_COOKIE_MASKED_DEPRECATED_MSG = (
|
|
|
|
'The CSRF_COOKIE_MASKED transitional setting is deprecated. Support for '
|
|
|
|
'it will be removed in Django 5.0.'
|
|
|
|
)
|
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
|
2019-01-12 04:28:22 +08:00
|
|
|
class SettingsReference(str):
|
|
|
|
"""
|
|
|
|
String subclass which references a current settings value. It's treated as
|
|
|
|
the value in memory but serializes to a settings.NAME attribute reference.
|
|
|
|
"""
|
|
|
|
def __new__(self, value, setting_name):
|
|
|
|
return str.__new__(self, value)
|
|
|
|
|
|
|
|
def __init__(self, value, setting_name):
|
|
|
|
self.setting_name = setting_name
|
|
|
|
|
|
|
|
|
2009-03-02 12:48:22 +08:00
|
|
|
class LazySettings(LazyObject):
|
2006-05-17 05:28:06 +08:00
|
|
|
"""
|
|
|
|
A lazy proxy for either global Django settings or a custom settings object.
|
|
|
|
The user can manually configure settings prior to using them. Otherwise,
|
|
|
|
Django uses the settings module pointed to by DJANGO_SETTINGS_MODULE.
|
|
|
|
"""
|
2012-10-23 08:13:59 +08:00
|
|
|
def _setup(self, name=None):
|
2006-05-17 05:28:06 +08:00
|
|
|
"""
|
|
|
|
Load the settings module pointed to by the environment variable. This
|
2017-01-25 04:37:33 +08:00
|
|
|
is used the first time settings are needed, if the user hasn't
|
|
|
|
configured settings manually.
|
2006-05-17 05:28:06 +08:00
|
|
|
"""
|
2013-12-19 01:13:40 +08:00
|
|
|
settings_module = os.environ.get(ENVIRONMENT_VARIABLE)
|
|
|
|
if not settings_module:
|
2012-10-23 08:13:59 +08:00
|
|
|
desc = ("setting %s" % name) if name else "settings"
|
2012-09-09 04:12:18 +08:00
|
|
|
raise ImproperlyConfigured(
|
2012-10-23 08:13:59 +08:00
|
|
|
"Requested %s, but settings are not configured. "
|
2012-09-09 04:12:18 +08:00
|
|
|
"You must either define the environment variable %s "
|
|
|
|
"or call settings.configure() before accessing settings."
|
2012-10-23 08:13:59 +08:00
|
|
|
% (desc, ENVIRONMENT_VARIABLE))
|
2006-05-17 05:28:06 +08:00
|
|
|
|
2009-03-02 12:48:22 +08:00
|
|
|
self._wrapped = Settings(settings_module)
|
2012-09-09 04:12:18 +08:00
|
|
|
|
2015-07-28 22:37:41 +08:00
|
|
|
def __repr__(self):
|
|
|
|
# Hardcode the class name as otherwise it yields 'Settings'.
|
|
|
|
if self._wrapped is empty:
|
|
|
|
return '<LazySettings [Unevaluated]>'
|
|
|
|
return '<LazySettings "%(settings_module)s">' % {
|
|
|
|
'settings_module': self._wrapped.SETTINGS_MODULE,
|
|
|
|
}
|
|
|
|
|
2012-09-09 04:12:18 +08:00
|
|
|
def __getattr__(self, name):
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Return the value of a setting and cache it in self.__dict__."""
|
2012-09-09 04:12:18 +08:00
|
|
|
if self._wrapped is empty:
|
|
|
|
self._setup(name)
|
2016-12-23 23:58:06 +08:00
|
|
|
val = getattr(self._wrapped, name)
|
2020-07-28 18:52:28 +08:00
|
|
|
|
|
|
|
# Special case some settings which require further modification.
|
|
|
|
# This is done here for performance reasons so the modified value is cached.
|
|
|
|
if name in {'MEDIA_URL', 'STATIC_URL'} and val is not None:
|
|
|
|
val = self._add_script_prefix(val)
|
2020-07-29 15:06:54 +08:00
|
|
|
elif name == 'SECRET_KEY' and not val:
|
|
|
|
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
|
2020-07-28 18:52:28 +08:00
|
|
|
|
2016-12-23 23:58:06 +08:00
|
|
|
self.__dict__[name] = val
|
|
|
|
return val
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
"""
|
|
|
|
Set the value of setting. Clear all cached values if _wrapped changes
|
|
|
|
(@override_settings does this) or clear single values when set.
|
|
|
|
"""
|
|
|
|
if name == '_wrapped':
|
|
|
|
self.__dict__.clear()
|
|
|
|
else:
|
|
|
|
self.__dict__.pop(name, None)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__setattr__(name, value)
|
2016-12-23 23:58:06 +08:00
|
|
|
|
|
|
|
def __delattr__(self, name):
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Delete a setting and clear it from cache if needed."""
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__delattr__(name)
|
2016-12-23 23:58:06 +08:00
|
|
|
self.__dict__.pop(name, None)
|
2012-09-09 04:12:18 +08:00
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
def configure(self, default_settings=global_settings, **options):
|
|
|
|
"""
|
|
|
|
Called to manually configure the settings. The 'default_settings'
|
|
|
|
parameter sets where to retrieve any unspecified values from (its
|
|
|
|
argument must support attribute access (__getattr__)).
|
|
|
|
"""
|
2011-06-02 04:45:47 +08:00
|
|
|
if self._wrapped is not empty:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise RuntimeError('Settings already configured.')
|
2006-05-17 05:28:06 +08:00
|
|
|
holder = UserSettingsHolder(default_settings)
|
|
|
|
for name, value in options.items():
|
2019-03-03 08:23:18 +08:00
|
|
|
if not name.isupper():
|
|
|
|
raise TypeError('Setting %r must be uppercase.' % name)
|
2006-05-17 05:28:06 +08:00
|
|
|
setattr(holder, name, value)
|
2009-03-02 12:48:22 +08:00
|
|
|
self._wrapped = holder
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2018-12-06 08:15:33 +08:00
|
|
|
@staticmethod
|
|
|
|
def _add_script_prefix(value):
|
|
|
|
"""
|
|
|
|
Add SCRIPT_NAME prefix to relative paths.
|
|
|
|
|
|
|
|
Useful when the app is being served at a subpath and manually prefixing
|
|
|
|
subpath to STATIC_URL and MEDIA_URL in settings is inconvenient.
|
|
|
|
"""
|
2020-12-31 20:18:57 +08:00
|
|
|
# Don't apply prefix to absolute paths and URLs.
|
|
|
|
if value.startswith(('http://', 'https://', '/')):
|
2018-12-06 08:15:33 +08:00
|
|
|
return value
|
|
|
|
from django.urls import get_script_prefix
|
|
|
|
return '%s%s' % (get_script_prefix(), value)
|
|
|
|
|
2011-06-02 04:45:47 +08:00
|
|
|
@property
|
2008-07-06 20:55:24 +08:00
|
|
|
def configured(self):
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Return True if the settings have already been configured."""
|
2011-06-02 04:45:47 +08:00
|
|
|
return self._wrapped is not empty
|
2008-07-06 20:55:24 +08:00
|
|
|
|
2021-09-09 13:42:05 +08:00
|
|
|
@property
|
|
|
|
def USE_L10N(self):
|
|
|
|
stack = traceback.extract_stack()
|
|
|
|
# Show a warning if the setting is used outside of Django.
|
|
|
|
# Stack index: -1 this line, -2 the caller.
|
|
|
|
filename, _, _, _ = stack[-2]
|
|
|
|
if not filename.startswith(os.path.dirname(django.__file__)):
|
|
|
|
warnings.warn(
|
|
|
|
USE_L10N_DEPRECATED_MSG,
|
|
|
|
RemovedInDjango50Warning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
return self.__getattr__('USE_L10N')
|
|
|
|
|
|
|
|
# RemovedInDjango50Warning.
|
|
|
|
@property
|
|
|
|
def _USE_L10N_INTERNAL(self):
|
|
|
|
# Special hook to avoid checking a traceback in internal use on hot
|
|
|
|
# paths.
|
|
|
|
return self.__getattr__('USE_L10N')
|
|
|
|
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Settings:
|
2006-05-02 09:31:56 +08:00
|
|
|
def __init__(self, settings_module):
|
|
|
|
# update this dict from global settings (but only for ALL_CAPS settings)
|
|
|
|
for setting in dir(global_settings):
|
2013-11-26 21:07:07 +08:00
|
|
|
if setting.isupper():
|
2006-05-02 09:31:56 +08:00
|
|
|
setattr(self, setting, getattr(global_settings, setting))
|
|
|
|
|
|
|
|
# store the settings module in case someone later cares
|
|
|
|
self.SETTINGS_MODULE = settings_module
|
|
|
|
|
2014-09-17 16:43:45 +08:00
|
|
|
mod = importlib.import_module(self.SETTINGS_MODULE)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2014-11-18 05:49:01 +08:00
|
|
|
tuple_settings = (
|
2021-03-24 08:22:27 +08:00
|
|
|
'ALLOWED_HOSTS',
|
2014-11-18 05:49:01 +08:00
|
|
|
"INSTALLED_APPS",
|
|
|
|
"TEMPLATE_DIRS",
|
|
|
|
"LOCALE_PATHS",
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
self._explicit_settings = set()
|
2006-05-02 09:31:56 +08:00
|
|
|
for setting in dir(mod):
|
2013-11-26 21:07:07 +08:00
|
|
|
if setting.isupper():
|
2006-05-02 09:31:56 +08:00
|
|
|
setting_value = getattr(mod, setting)
|
2013-06-30 05:19:20 +08:00
|
|
|
|
2013-09-22 20:01:57 +08:00
|
|
|
if (setting in tuple_settings and
|
2015-01-22 00:55:57 +08:00
|
|
|
not isinstance(setting_value, (list, tuple))):
|
2021-03-24 18:44:01 +08:00
|
|
|
raise ImproperlyConfigured("The %s setting must be a list or a tuple." % setting)
|
2006-05-02 09:31:56 +08:00
|
|
|
setattr(self, setting, setting_value)
|
2014-01-20 10:45:21 +08:00
|
|
|
self._explicit_settings.add(setting)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2021-05-14 21:58:45 +08:00
|
|
|
if self.USE_TZ is False and not self.is_overridden('USE_TZ'):
|
|
|
|
warnings.warn(
|
|
|
|
'The default value of USE_TZ will change from False to True '
|
|
|
|
'in Django 5.0. Set USE_TZ to False in your project settings '
|
|
|
|
'if you want to keep the current default behavior.',
|
|
|
|
category=RemovedInDjango50Warning,
|
|
|
|
)
|
|
|
|
|
2021-09-09 21:15:44 +08:00
|
|
|
if self.is_overridden('USE_DEPRECATED_PYTZ'):
|
|
|
|
warnings.warn(USE_DEPRECATED_PYTZ_DEPRECATED_MSG, RemovedInDjango50Warning)
|
|
|
|
|
2021-08-17 21:13:13 +08:00
|
|
|
if self.is_overridden('CSRF_COOKIE_MASKED'):
|
|
|
|
warnings.warn(CSRF_COOKIE_MASKED_DEPRECATED_MSG, RemovedInDjango50Warning)
|
|
|
|
|
2010-09-27 05:36:22 +08:00
|
|
|
if hasattr(time, 'tzset') and self.TIME_ZONE:
|
2010-09-11 03:36:51 +08:00
|
|
|
# When we can, attempt to validate the timezone. If we can't find
|
|
|
|
# this file, no check happens and it's harmless.
|
2017-07-22 04:33:26 +08:00
|
|
|
zoneinfo_root = Path('/usr/share/zoneinfo')
|
|
|
|
zone_info_file = zoneinfo_root.joinpath(*self.TIME_ZONE.split('/'))
|
|
|
|
if zoneinfo_root.exists() and not zone_info_file.exists():
|
2010-09-11 03:36:51 +08:00
|
|
|
raise ValueError("Incorrect timezone setting: %s" % self.TIME_ZONE)
|
2007-02-12 08:10:09 +08:00
|
|
|
# Move the time zone info into os.environ. See ticket #2315 for why
|
|
|
|
# we don't do this unconditionally (breaks Windows).
|
|
|
|
os.environ['TZ'] = self.TIME_ZONE
|
2007-09-16 02:11:43 +08:00
|
|
|
time.tzset()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2021-09-09 13:42:05 +08:00
|
|
|
if self.is_overridden('USE_L10N'):
|
|
|
|
warnings.warn(USE_L10N_DEPRECATED_MSG, RemovedInDjango50Warning)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def is_overridden(self, setting):
|
|
|
|
return setting in self._explicit_settings
|
|
|
|
|
2015-07-28 22:37:41 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return '<%(cls)s "%(settings_module)s">' % {
|
|
|
|
'cls': self.__class__.__name__,
|
|
|
|
'settings_module': self.SETTINGS_MODULE,
|
|
|
|
}
|
|
|
|
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class UserSettingsHolder:
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Holder for user configured settings."""
|
2006-07-04 11:58:45 +08:00
|
|
|
# SETTINGS_MODULE doesn't make much sense in the manually configured
|
2006-05-17 05:28:06 +08:00
|
|
|
# (standalone) case.
|
|
|
|
SETTINGS_MODULE = None
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
def __init__(self, default_settings):
|
|
|
|
"""
|
|
|
|
Requests for configuration variables not in this class are satisfied
|
|
|
|
from the module specified in default_settings (if possible).
|
|
|
|
"""
|
2012-09-04 15:41:12 +08:00
|
|
|
self.__dict__['_deleted'] = set()
|
2014-09-06 02:06:02 +08:00
|
|
|
self.default_settings = default_settings
|
2006-05-17 05:28:06 +08:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2019-03-03 08:23:18 +08:00
|
|
|
if not name.isupper() or name in self._deleted:
|
2012-09-04 15:41:12 +08:00
|
|
|
raise AttributeError
|
2006-05-17 05:28:06 +08:00
|
|
|
return getattr(self.default_settings, name)
|
|
|
|
|
2012-09-04 15:41:12 +08:00
|
|
|
def __setattr__(self, name, value):
|
|
|
|
self._deleted.discard(name)
|
2021-09-09 13:42:05 +08:00
|
|
|
if name == 'USE_L10N':
|
|
|
|
warnings.warn(USE_L10N_DEPRECATED_MSG, RemovedInDjango50Warning)
|
2021-08-17 21:13:13 +08:00
|
|
|
if name == 'CSRF_COOKIE_MASKED':
|
|
|
|
warnings.warn(CSRF_COOKIE_MASKED_DEPRECATED_MSG, RemovedInDjango50Warning)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__setattr__(name, value)
|
2021-09-09 21:15:44 +08:00
|
|
|
if name == 'USE_DEPRECATED_PYTZ':
|
|
|
|
warnings.warn(USE_DEPRECATED_PYTZ_DEPRECATED_MSG, RemovedInDjango50Warning)
|
2012-09-04 15:41:12 +08:00
|
|
|
|
|
|
|
def __delattr__(self, name):
|
|
|
|
self._deleted.add(name)
|
2014-04-12 21:31:37 +08:00
|
|
|
if hasattr(self, name):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__delattr__(name)
|
2012-09-04 15:41:12 +08:00
|
|
|
|
2009-10-20 05:48:06 +08:00
|
|
|
def __dir__(self):
|
2016-04-23 20:39:15 +08:00
|
|
|
return sorted(
|
2018-09-28 21:57:12 +08:00
|
|
|
s for s in [*self.__dict__, *dir(self.default_settings)]
|
2016-04-23 20:39:15 +08:00
|
|
|
if s not in self._deleted
|
|
|
|
)
|
2006-05-17 05:28:06 +08:00
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
def is_overridden(self, setting):
|
2014-01-21 11:25:33 +08:00
|
|
|
deleted = (setting in self._deleted)
|
|
|
|
set_locally = (setting in self.__dict__)
|
|
|
|
set_on_default = getattr(self.default_settings, 'is_overridden', lambda s: False)(setting)
|
2017-09-14 09:20:29 +08:00
|
|
|
return deleted or set_locally or set_on_default
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2015-07-28 22:37:41 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return '<%(cls)s>' % {
|
|
|
|
'cls': self.__class__.__name__,
|
|
|
|
}
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
settings = LazySettings()
|