2012-04-29 22:03:46 +08:00
|
|
|
import os
|
|
|
|
import time
|
2013-10-19 06:49:24 +08:00
|
|
|
import threading
|
2013-05-18 19:43:51 +08:00
|
|
|
import warnings
|
2012-04-29 22:03:46 +08:00
|
|
|
|
2012-03-13 14:59:04 +08:00
|
|
|
from django.conf import settings
|
2012-03-14 07:29:15 +08:00
|
|
|
from django.db import connections
|
2012-04-09 21:24:57 +08:00
|
|
|
from django.dispatch import receiver, Signal
|
2012-04-29 22:03:46 +08:00
|
|
|
from django.utils import timezone
|
2012-10-28 04:38:15 +08:00
|
|
|
from django.utils.functional import empty
|
2008-08-06 23:32:46 +08:00
|
|
|
|
|
|
|
template_rendered = Signal(providing_args=["template", "context"])
|
2011-05-18 20:08:53 +08:00
|
|
|
|
2013-05-18 19:43:51 +08:00
|
|
|
setting_changed = Signal(providing_args=["setting", "value", "enter"])
|
2012-03-13 14:59:04 +08:00
|
|
|
|
2012-07-21 19:49:07 +08:00
|
|
|
# Most setting_changed receivers are supposed to be added below,
|
|
|
|
# except for cases where the receiver is related to a contrib app.
|
|
|
|
|
2013-05-18 19:43:51 +08:00
|
|
|
# Settings that may not work well when using 'override_settings' (#19031)
|
2013-12-23 07:10:53 +08:00
|
|
|
COMPLEX_OVERRIDE_SETTINGS = set(['DATABASES'])
|
2013-05-18 19:43:51 +08:00
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
2013-10-19 06:49:24 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def clear_cache_handlers(**kwargs):
|
|
|
|
if kwargs['setting'] == 'CACHES':
|
|
|
|
from django.core.cache import caches
|
|
|
|
caches._caches = threading.local()
|
|
|
|
|
|
|
|
|
2013-12-23 07:10:53 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def update_installed_apps(**kwargs):
|
|
|
|
if kwargs['setting'] == 'INSTALLED_APPS':
|
|
|
|
# Rebuild any AppDirectoriesFinder instance.
|
|
|
|
from django.contrib.staticfiles.finders import get_finder
|
|
|
|
get_finder.cache_clear()
|
2014-01-02 01:01:06 +08:00
|
|
|
# Rebuild management commands cache
|
|
|
|
from django.core.management import get_commands
|
|
|
|
get_commands.cache_clear()
|
2014-01-26 21:17:47 +08:00
|
|
|
# Rebuild templatetags module cache.
|
|
|
|
from django.template import base as mod
|
|
|
|
mod.templatetags_modules = []
|
|
|
|
# Rebuild app_template_dirs cache.
|
|
|
|
from django.template.loaders import app_directories as mod
|
|
|
|
mod.app_template_dirs = mod.calculate_app_template_dirs()
|
2014-01-26 22:28:33 +08:00
|
|
|
# Rebuild translations cache.
|
|
|
|
from django.utils.translation import trans_real
|
|
|
|
trans_real._translations = {}
|
2013-12-23 07:10:53 +08:00
|
|
|
|
|
|
|
|
2012-04-09 21:24:57 +08:00
|
|
|
@receiver(setting_changed)
|
2012-03-14 07:29:15 +08:00
|
|
|
def update_connections_time_zone(**kwargs):
|
2012-04-29 22:03:46 +08:00
|
|
|
if kwargs['setting'] == 'TIME_ZONE':
|
|
|
|
# Reset process time zone
|
|
|
|
if hasattr(time, 'tzset'):
|
|
|
|
if kwargs['value']:
|
|
|
|
os.environ['TZ'] = kwargs['value']
|
|
|
|
else:
|
|
|
|
os.environ.pop('TZ', None)
|
|
|
|
time.tzset()
|
|
|
|
|
|
|
|
# Reset local time zone cache
|
|
|
|
timezone._localtime = None
|
|
|
|
|
|
|
|
# Reset the database connections' time zone
|
2012-03-14 07:29:15 +08:00
|
|
|
if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC':
|
|
|
|
USE_TZ, TIME_ZONE = kwargs['value'], settings.TIME_ZONE
|
|
|
|
elif kwargs['setting'] == 'TIME_ZONE' and not settings.USE_TZ:
|
|
|
|
USE_TZ, TIME_ZONE = settings.USE_TZ, kwargs['value']
|
2012-04-29 22:03:46 +08:00
|
|
|
else:
|
|
|
|
# no need to change the database connnections' time zones
|
2012-03-14 07:29:15 +08:00
|
|
|
return
|
|
|
|
tz = 'UTC' if USE_TZ else TIME_ZONE
|
|
|
|
for conn in connections.all():
|
2012-04-29 22:03:46 +08:00
|
|
|
conn.settings_dict['TIME_ZONE'] = tz
|
2012-03-14 07:29:15 +08:00
|
|
|
tz_sql = conn.ops.set_time_zone_sql()
|
|
|
|
if tz_sql:
|
|
|
|
conn.cursor().execute(tz_sql, [tz])
|
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
2012-04-09 21:24:57 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def clear_context_processors_cache(**kwargs):
|
|
|
|
if kwargs['setting'] == 'TEMPLATE_CONTEXT_PROCESSORS':
|
2012-08-22 03:52:25 +08:00
|
|
|
from django.template import context
|
2012-04-09 21:24:57 +08:00
|
|
|
context._standard_context_processors = None
|
2012-07-21 19:49:07 +08:00
|
|
|
|
|
|
|
|
2012-09-16 03:39:08 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def clear_template_loaders_cache(**kwargs):
|
|
|
|
if kwargs['setting'] == 'TEMPLATE_LOADERS':
|
|
|
|
from django.template import loader
|
|
|
|
loader.template_source_loaders = None
|
|
|
|
|
|
|
|
|
2012-08-22 03:52:25 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def clear_serializers_cache(**kwargs):
|
|
|
|
if kwargs['setting'] == 'SERIALIZATION_MODULES':
|
|
|
|
from django.core import serializers
|
|
|
|
serializers._serializers = {}
|
|
|
|
|
|
|
|
|
2012-07-21 19:49:07 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def language_changed(**kwargs):
|
2014-01-26 22:28:33 +08:00
|
|
|
if kwargs['setting'] in {'LANGUAGES', 'LANGUAGE_CODE', 'LOCALE_PATHS'}:
|
2012-07-21 19:49:07 +08:00
|
|
|
from django.utils.translation import trans_real
|
|
|
|
trans_real._default = None
|
2014-01-26 22:28:33 +08:00
|
|
|
trans_real._active = threading.local()
|
|
|
|
if kwargs['setting'] in {'LANGUAGES', 'LOCALE_PATHS'}:
|
|
|
|
from django.utils.translation import trans_real
|
|
|
|
trans_real._translations = {}
|
|
|
|
trans_real.check_for_language.cache_clear()
|
2012-10-28 04:38:15 +08:00
|
|
|
|
2013-05-18 19:43:51 +08:00
|
|
|
|
2012-10-28 04:38:15 +08:00
|
|
|
@receiver(setting_changed)
|
|
|
|
def file_storage_changed(**kwargs):
|
|
|
|
if kwargs['setting'] in ('MEDIA_ROOT', 'DEFAULT_FILE_STORAGE'):
|
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
default_storage._wrapped = empty
|
2013-05-18 19:43:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
|
|
|
def complex_setting_changed(**kwargs):
|
|
|
|
if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS:
|
2013-12-20 18:04:58 +08:00
|
|
|
# Considering the current implementation of the signals framework,
|
|
|
|
# stacklevel=5 shows the line containing the override_settings call.
|
|
|
|
warnings.warn("Overriding setting %s can lead to unexpected behaviour."
|
|
|
|
% kwargs['setting'], stacklevel=5)
|
2013-11-28 03:45:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
|
|
|
def root_urlconf_changed(**kwargs):
|
|
|
|
if kwargs['setting'] == 'ROOT_URLCONF':
|
|
|
|
from django.core.urlresolvers import clear_url_caches
|
|
|
|
clear_url_caches()
|