2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Settings and configuration for Django.
|
|
|
|
|
|
|
|
Values will be read from the module specified by the DJANGO_SETTINGS_MODULE environment
|
|
|
|
variable, and then from django.conf.global_settings; see the global settings file for
|
|
|
|
a list of all possible variables.
|
|
|
|
"""
|
|
|
|
|
2012-11-17 08:50:50 +08:00
|
|
|
import logging
|
2006-05-02 09:31:56 +08:00
|
|
|
import os
|
2013-03-25 12:34:46 +08:00
|
|
|
import sys
|
2007-02-12 08:10:09 +08:00
|
|
|
import time # Needed for Windows
|
2011-01-02 09:33:11 +08:00
|
|
|
import warnings
|
2009-03-02 12:48:22 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import global_settings
|
2012-03-31 19:44:57 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-06-02 04:45:47 +08:00
|
|
|
from django.utils.functional import LazyObject, empty
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils import importlib
|
2013-02-03 05:58:02 +08:00
|
|
|
from django.utils.module_loading import import_by_path
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
ENVIRONMENT_VARIABLE = "DJANGO_SETTINGS_MODULE"
|
|
|
|
|
2010-10-04 23:12:39 +08:00
|
|
|
|
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
|
|
|
|
is used the first time we need any settings at all, if the user has not
|
|
|
|
previously configured the settings manually.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
settings_module = os.environ[ENVIRONMENT_VARIABLE]
|
|
|
|
if not settings_module: # If it's set but is an empty string.
|
|
|
|
raise KeyError
|
|
|
|
except KeyError:
|
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-25 04:11:42 +08:00
|
|
|
self._configure_logging()
|
2012-09-09 04:12:18 +08:00
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
if self._wrapped is empty:
|
|
|
|
self._setup(name)
|
|
|
|
return getattr(self._wrapped, name)
|
|
|
|
|
2012-09-25 04:11:42 +08:00
|
|
|
def _configure_logging(self):
|
|
|
|
"""
|
|
|
|
Setup logging from LOGGING_CONFIG and LOGGING settings.
|
|
|
|
"""
|
2013-03-25 12:34:46 +08:00
|
|
|
if not sys.warnoptions:
|
|
|
|
try:
|
|
|
|
# Route warnings through python logging
|
|
|
|
logging.captureWarnings(True)
|
|
|
|
# Allow DeprecationWarnings through the warnings filters
|
|
|
|
warnings.simplefilter("default", DeprecationWarning)
|
|
|
|
except AttributeError:
|
|
|
|
# No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
|
|
|
|
pass
|
2012-11-17 08:50:50 +08:00
|
|
|
|
2012-09-25 04:11:42 +08:00
|
|
|
if self.LOGGING_CONFIG:
|
2012-09-25 04:30:38 +08:00
|
|
|
from django.utils.log import DEFAULT_LOGGING
|
2012-09-25 04:11:42 +08:00
|
|
|
# First find the logging configuration function ...
|
2013-02-03 05:58:02 +08:00
|
|
|
logging_config_func = import_by_path(self.LOGGING_CONFIG)
|
2012-09-25 04:11:42 +08:00
|
|
|
|
2012-09-25 04:30:38 +08:00
|
|
|
logging_config_func(DEFAULT_LOGGING)
|
2012-09-25 04:11:42 +08:00
|
|
|
|
2012-12-25 05:32:41 +08:00
|
|
|
# ... then invoke it with the logging settings
|
2012-09-25 04:30:38 +08:00
|
|
|
if self.LOGGING:
|
|
|
|
logging_config_func(self.LOGGING)
|
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():
|
|
|
|
setattr(holder, name, value)
|
2009-03-02 12:48:22 +08:00
|
|
|
self._wrapped = holder
|
2012-11-10 19:05:58 +08:00
|
|
|
self._configure_logging()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2011-06-02 04:45:47 +08:00
|
|
|
@property
|
2008-07-06 20:55:24 +08:00
|
|
|
def configured(self):
|
|
|
|
"""
|
|
|
|
Returns 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
|
|
|
|
2011-01-02 09:33:11 +08:00
|
|
|
|
|
|
|
class BaseSettings(object):
|
|
|
|
"""
|
|
|
|
Common logic for settings whether set by a module or by the user.
|
|
|
|
"""
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
if name in ("MEDIA_URL", "STATIC_URL") and value and not value.endswith('/'):
|
2012-03-31 21:01:59 +08:00
|
|
|
raise ImproperlyConfigured("If set, %s must end with a slash" % name)
|
2012-07-20 20:22:00 +08:00
|
|
|
elif name == "ALLOWED_INCLUDE_ROOTS" and isinstance(value, six.string_types):
|
2012-02-22 08:42:19 +08:00
|
|
|
raise ValueError("The ALLOWED_INCLUDE_ROOTS setting must be set "
|
|
|
|
"to a tuple, not a string.")
|
2011-01-02 09:33:11 +08:00
|
|
|
object.__setattr__(self, name, value)
|
|
|
|
|
|
|
|
|
|
|
|
class Settings(BaseSettings):
|
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):
|
|
|
|
if setting == setting.upper():
|
|
|
|
setattr(self, setting, getattr(global_settings, setting))
|
|
|
|
|
|
|
|
# store the settings module in case someone later cares
|
|
|
|
self.SETTINGS_MODULE = settings_module
|
|
|
|
|
|
|
|
try:
|
2009-03-19 00:55:59 +08:00
|
|
|
mod = importlib.import_module(self.SETTINGS_MODULE)
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e:
|
2013-05-26 02:25:29 +08:00
|
|
|
raise ImportError(
|
|
|
|
"Could not import settings '%s' (Is it on sys.path? Is there an import error in the settings file?): %s"
|
|
|
|
% (self.SETTINGS_MODULE, e)
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Settings that should be converted into tuples if they're mistakenly entered
|
|
|
|
# as strings.
|
|
|
|
tuple_settings = ("INSTALLED_APPS", "TEMPLATE_DIRS")
|
|
|
|
|
|
|
|
for setting in dir(mod):
|
|
|
|
if setting == setting.upper():
|
|
|
|
setting_value = getattr(mod, setting)
|
2012-02-22 08:42:19 +08:00
|
|
|
if setting in tuple_settings and \
|
2012-07-20 20:22:00 +08:00
|
|
|
isinstance(setting_value, six.string_types):
|
2012-06-30 21:06:42 +08:00
|
|
|
warnings.warn("The %s setting must be a tuple. Please fix your "
|
|
|
|
"settings, as auto-correction is now deprecated." % setting,
|
2012-12-26 04:07:27 +08:00
|
|
|
DeprecationWarning, stacklevel=2)
|
2006-05-02 09:31:56 +08:00
|
|
|
setting_value = (setting_value,) # In case the user forgot the comma.
|
|
|
|
setattr(self, setting, setting_value)
|
|
|
|
|
2012-03-02 05:38:45 +08:00
|
|
|
if not self.SECRET_KEY:
|
2012-03-31 19:44:57 +08:00
|
|
|
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
|
2012-03-02 05:38:45 +08:00
|
|
|
|
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.
|
|
|
|
zoneinfo_root = '/usr/share/zoneinfo'
|
|
|
|
if (os.path.exists(zoneinfo_root) and not
|
|
|
|
os.path.exists(os.path.join(zoneinfo_root, *(self.TIME_ZONE.split('/'))))):
|
|
|
|
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
|
|
|
|
2011-01-02 09:33:11 +08:00
|
|
|
|
|
|
|
class UserSettingsHolder(BaseSettings):
|
2006-05-17 05:28:06 +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()
|
2006-05-17 05:28:06 +08:00
|
|
|
self.default_settings = default_settings
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
2012-09-04 15:41:12 +08:00
|
|
|
if name in self._deleted:
|
|
|
|
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)
|
|
|
|
return super(UserSettingsHolder, self).__setattr__(name, value)
|
|
|
|
|
|
|
|
def __delattr__(self, name):
|
|
|
|
self._deleted.add(name)
|
|
|
|
return super(UserSettingsHolder, self).__delattr__(name)
|
|
|
|
|
2009-10-20 05:48:06 +08:00
|
|
|
def __dir__(self):
|
2012-08-08 22:33:15 +08:00
|
|
|
return list(self.__dict__) + dir(self.default_settings)
|
2006-05-17 05:28:06 +08:00
|
|
|
|
|
|
|
settings = LazySettings()
|