2012-04-29 22:03:46 +08:00
|
|
|
import os
|
2015-01-28 20:35:27 +08:00
|
|
|
import time
|
2013-05-18 19:43:51 +08:00
|
|
|
import warnings
|
2012-04-29 22:03:46 +08:00
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
from asgiref.local import Local
|
|
|
|
|
2016-05-18 21:33:53 +08:00
|
|
|
from django.apps import apps
|
2016-10-01 04:06:02 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-10-09 23:46:40 +08:00
|
|
|
from django.core.signals import setting_changed
|
2014-11-29 21:42:06 +08:00
|
|
|
from django.db import connections, router
|
|
|
|
from django.db.utils import ConnectionRouter
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.dispatch import Signal, receiver
|
2016-12-01 18:38:01 +08:00
|
|
|
from django.utils import timezone
|
2016-12-02 04:22:16 +08:00
|
|
|
from django.utils.formats import FORMAT_SETTINGS, reset_format_cache
|
2012-10-28 04:38:15 +08:00
|
|
|
from django.utils.functional import empty
|
2008-08-06 23:32:46 +08:00
|
|
|
|
2020-03-02 01:22:03 +08:00
|
|
|
template_rendered = Signal()
|
2011-05-18 20:08:53 +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)
|
2014-09-26 20:31:50 +08:00
|
|
|
COMPLEX_OVERRIDE_SETTINGS = {'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)
|
2021-12-13 19:17:12 +08:00
|
|
|
def clear_cache_handlers(*, setting, **kwargs):
|
|
|
|
if setting == 'CACHES':
|
2020-02-07 15:03:25 +08:00
|
|
|
from django.core.cache import caches, close_caches
|
|
|
|
close_caches()
|
2017-10-22 23:30:42 +08:00
|
|
|
caches._settings = caches.settings = caches.configure_settings(None)
|
|
|
|
caches._connections = Local()
|
2013-10-19 06:49:24 +08:00
|
|
|
|
|
|
|
|
2013-12-23 07:10:53 +08:00
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def update_installed_apps(*, setting, **kwargs):
|
|
|
|
if setting == 'INSTALLED_APPS':
|
2013-12-23 07:10:53 +08:00
|
|
|
# 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-11-11 04:04:04 +08:00
|
|
|
# Rebuild get_app_template_dirs cache.
|
|
|
|
from django.template.utils import get_app_template_dirs
|
|
|
|
get_app_template_dirs.cache_clear()
|
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)
|
2021-12-13 19:17:12 +08:00
|
|
|
def update_connections_time_zone(*, setting, **kwargs):
|
|
|
|
if setting == 'TIME_ZONE':
|
2012-04-29 22:03:46 +08:00
|
|
|
# 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
|
2014-11-19 04:50:03 +08:00
|
|
|
timezone.get_default_timezone.cache_clear()
|
2012-04-29 22:03:46 +08:00
|
|
|
|
|
|
|
# Reset the database connections' time zone
|
2021-12-13 19:17:12 +08:00
|
|
|
if setting in {'TIME_ZONE', 'USE_TZ'}:
|
2015-05-03 03:56:53 +08:00
|
|
|
for conn in connections.all():
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2015-05-03 03:56:53 +08:00
|
|
|
del conn.timezone
|
2017-09-07 20:16:21 +08:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
try:
|
2015-05-03 03:56:53 +08:00
|
|
|
del conn.timezone_name
|
2017-09-07 20:16:21 +08:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2016-06-06 11:46:28 +08:00
|
|
|
conn.ensure_timezone()
|
2012-03-14 07:29:15 +08:00
|
|
|
|
2012-04-29 22:03:46 +08:00
|
|
|
|
2014-11-29 21:42:06 +08:00
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def clear_routers_cache(*, setting, **kwargs):
|
|
|
|
if setting == 'DATABASE_ROUTERS':
|
2014-11-29 21:42:06 +08:00
|
|
|
router.routers = ConnectionRouter().routers
|
|
|
|
|
|
|
|
|
2014-11-14 22:48:27 +08:00
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def reset_template_engines(*, setting, **kwargs):
|
|
|
|
if setting in {
|
2014-11-11 23:32:19 +08:00
|
|
|
'TEMPLATES',
|
2015-02-15 22:42:05 +08:00
|
|
|
'DEBUG',
|
2014-11-11 23:32:19 +08:00
|
|
|
'INSTALLED_APPS',
|
2014-11-14 22:48:27 +08:00
|
|
|
}:
|
2014-11-11 23:32:19 +08:00
|
|
|
from django.template import engines
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2014-11-11 23:32:19 +08:00
|
|
|
del engines.templates
|
2017-09-07 20:16:21 +08:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2014-11-11 23:32:19 +08:00
|
|
|
engines._templates = None
|
|
|
|
engines._engines = {}
|
2014-11-14 22:48:27 +08:00
|
|
|
from django.template.engine import Engine
|
|
|
|
Engine.get_default.cache_clear()
|
2016-12-28 06:00:56 +08:00
|
|
|
from django.forms.renderers import get_default_renderer
|
|
|
|
get_default_renderer.cache_clear()
|
2014-11-14 22:48:27 +08:00
|
|
|
|
|
|
|
|
2012-08-22 03:52:25 +08:00
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def clear_serializers_cache(*, setting, **kwargs):
|
|
|
|
if setting == 'SERIALIZATION_MODULES':
|
2012-08-22 03:52:25 +08:00
|
|
|
from django.core import serializers
|
|
|
|
serializers._serializers = {}
|
|
|
|
|
|
|
|
|
2012-07-21 19:49:07 +08:00
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def language_changed(*, setting, **kwargs):
|
|
|
|
if 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
|
2019-04-12 21:15:18 +08:00
|
|
|
trans_real._active = Local()
|
2021-12-13 19:17:12 +08:00
|
|
|
if setting in {'LANGUAGES', 'LOCALE_PATHS'}:
|
2014-01-26 22:28:33 +08:00
|
|
|
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
|
|
|
|
2016-12-02 04:22:16 +08:00
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def localize_settings_changed(*, setting, **kwargs):
|
|
|
|
if setting in FORMAT_SETTINGS or setting == 'USE_THOUSAND_SEPARATOR':
|
2016-12-02 04:22:16 +08:00
|
|
|
reset_format_cache()
|
|
|
|
|
|
|
|
|
2012-10-28 04:38:15 +08:00
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def file_storage_changed(*, setting, **kwargs):
|
|
|
|
if setting == 'DEFAULT_FILE_STORAGE':
|
2012-10-28 04:38:15 +08:00
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
default_storage._wrapped = empty
|
2013-05-18 19:43:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def complex_setting_changed(*, enter, setting, **kwargs):
|
|
|
|
if enter and setting in COMPLEX_OVERRIDE_SETTINGS:
|
2013-12-20 18:04:58 +08:00
|
|
|
# Considering the current implementation of the signals framework,
|
2016-11-22 22:30:14 +08:00
|
|
|
# this stacklevel shows the line containing the override_settings call.
|
2021-12-13 19:17:12 +08:00
|
|
|
warnings.warn(
|
|
|
|
f'Overriding setting {setting} can lead to unexpected behavior.',
|
|
|
|
stacklevel=6,
|
|
|
|
)
|
2013-11-28 03:45:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def root_urlconf_changed(*, setting, **kwargs):
|
|
|
|
if setting == 'ROOT_URLCONF':
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import clear_url_caches, set_urlconf
|
2013-11-28 03:45:20 +08:00
|
|
|
clear_url_caches()
|
2014-05-04 22:29:49 +08:00
|
|
|
set_urlconf(None)
|
2015-01-25 22:53:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def static_storage_changed(*, setting, **kwargs):
|
|
|
|
if setting in {
|
2015-01-25 22:53:40 +08:00
|
|
|
'STATICFILES_STORAGE',
|
|
|
|
'STATIC_ROOT',
|
|
|
|
'STATIC_URL',
|
|
|
|
}:
|
|
|
|
from django.contrib.staticfiles.storage import staticfiles_storage
|
|
|
|
staticfiles_storage._wrapped = empty
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def static_finders_changed(*, setting, **kwargs):
|
|
|
|
if setting in {
|
2015-01-25 22:53:40 +08:00
|
|
|
'STATICFILES_DIRS',
|
|
|
|
'STATIC_ROOT',
|
|
|
|
}:
|
|
|
|
from django.contrib.staticfiles.finders import get_finder
|
|
|
|
get_finder.cache_clear()
|
2015-03-08 22:07:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def auth_password_validators_changed(*, setting, **kwargs):
|
|
|
|
if setting == 'AUTH_PASSWORD_VALIDATORS':
|
2020-07-24 14:25:47 +08:00
|
|
|
from django.contrib.auth.password_validation import (
|
|
|
|
get_default_password_validators,
|
|
|
|
)
|
2015-03-08 22:07:57 +08:00
|
|
|
get_default_password_validators.cache_clear()
|
2016-05-18 21:33:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(setting_changed)
|
2021-12-13 19:17:12 +08:00
|
|
|
def user_model_swapped(*, setting, **kwargs):
|
|
|
|
if setting == 'AUTH_USER_MODEL':
|
2016-05-18 21:33:53 +08:00
|
|
|
apps.clear_cache()
|
2016-10-01 04:06:02 +08:00
|
|
|
try:
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
UserModel = get_user_model()
|
|
|
|
except ImproperlyConfigured:
|
|
|
|
# Some tests set an invalid AUTH_USER_MODEL.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
from django.contrib.auth import backends
|
|
|
|
backends.UserModel = UserModel
|
|
|
|
|
|
|
|
from django.contrib.auth import forms
|
|
|
|
forms.UserModel = UserModel
|
|
|
|
|
|
|
|
from django.contrib.auth.handlers import modwsgi
|
|
|
|
modwsgi.UserModel = UserModel
|
|
|
|
|
|
|
|
from django.contrib.auth.management.commands import changepassword
|
|
|
|
changepassword.UserModel = UserModel
|
|
|
|
|
|
|
|
from django.contrib.auth import views
|
|
|
|
views.UserModel = UserModel
|