Changed signatures of setting_changed signal receivers.

This commit is contained in:
Adam Johnson 2021-12-13 11:17:12 +00:00 committed by Mariusz Felisiak
parent 4328970780
commit b0d16d0129
4 changed files with 42 additions and 40 deletions

View File

@ -102,8 +102,8 @@ def get_hashers_by_algorithm():
@receiver(setting_changed) @receiver(setting_changed)
def reset_hashers(**kwargs): def reset_hashers(*, setting, **kwargs):
if kwargs['setting'] == 'PASSWORD_HASHERS': if setting == 'PASSWORD_HASHERS':
get_hashers.cache_clear() get_hashers.cache_clear()
get_hashers_by_algorithm.cache_clear() get_hashers_by_algorithm.cache_clear()

View File

@ -24,8 +24,8 @@ COMPLEX_OVERRIDE_SETTINGS = {'DATABASES'}
@receiver(setting_changed) @receiver(setting_changed)
def clear_cache_handlers(**kwargs): def clear_cache_handlers(*, setting, **kwargs):
if kwargs['setting'] == 'CACHES': if setting == 'CACHES':
from django.core.cache import caches, close_caches from django.core.cache import caches, close_caches
close_caches() close_caches()
caches._settings = caches.settings = caches.configure_settings(None) caches._settings = caches.settings = caches.configure_settings(None)
@ -33,8 +33,8 @@ def clear_cache_handlers(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def update_installed_apps(**kwargs): def update_installed_apps(*, setting, **kwargs):
if kwargs['setting'] == 'INSTALLED_APPS': if setting == 'INSTALLED_APPS':
# Rebuild any AppDirectoriesFinder instance. # Rebuild any AppDirectoriesFinder instance.
from django.contrib.staticfiles.finders import get_finder from django.contrib.staticfiles.finders import get_finder
get_finder.cache_clear() get_finder.cache_clear()
@ -50,8 +50,8 @@ def update_installed_apps(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def update_connections_time_zone(**kwargs): def update_connections_time_zone(*, setting, **kwargs):
if kwargs['setting'] == 'TIME_ZONE': if setting == 'TIME_ZONE':
# Reset process time zone # Reset process time zone
if hasattr(time, 'tzset'): if hasattr(time, 'tzset'):
if kwargs['value']: if kwargs['value']:
@ -64,7 +64,7 @@ def update_connections_time_zone(**kwargs):
timezone.get_default_timezone.cache_clear() timezone.get_default_timezone.cache_clear()
# Reset the database connections' time zone # Reset the database connections' time zone
if kwargs['setting'] in {'TIME_ZONE', 'USE_TZ'}: if setting in {'TIME_ZONE', 'USE_TZ'}:
for conn in connections.all(): for conn in connections.all():
try: try:
del conn.timezone del conn.timezone
@ -78,14 +78,14 @@ def update_connections_time_zone(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def clear_routers_cache(**kwargs): def clear_routers_cache(*, setting, **kwargs):
if kwargs['setting'] == 'DATABASE_ROUTERS': if setting == 'DATABASE_ROUTERS':
router.routers = ConnectionRouter().routers router.routers = ConnectionRouter().routers
@receiver(setting_changed) @receiver(setting_changed)
def reset_template_engines(**kwargs): def reset_template_engines(*, setting, **kwargs):
if kwargs['setting'] in { if setting in {
'TEMPLATES', 'TEMPLATES',
'DEBUG', 'DEBUG',
'INSTALLED_APPS', 'INSTALLED_APPS',
@ -104,57 +104,59 @@ def reset_template_engines(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def clear_serializers_cache(**kwargs): def clear_serializers_cache(*, setting, **kwargs):
if kwargs['setting'] == 'SERIALIZATION_MODULES': if setting == 'SERIALIZATION_MODULES':
from django.core import serializers from django.core import serializers
serializers._serializers = {} serializers._serializers = {}
@receiver(setting_changed) @receiver(setting_changed)
def language_changed(**kwargs): def language_changed(*, setting, **kwargs):
if kwargs['setting'] in {'LANGUAGES', 'LANGUAGE_CODE', 'LOCALE_PATHS'}: if setting in {'LANGUAGES', 'LANGUAGE_CODE', 'LOCALE_PATHS'}:
from django.utils.translation import trans_real from django.utils.translation import trans_real
trans_real._default = None trans_real._default = None
trans_real._active = Local() trans_real._active = Local()
if kwargs['setting'] in {'LANGUAGES', 'LOCALE_PATHS'}: if setting in {'LANGUAGES', 'LOCALE_PATHS'}:
from django.utils.translation import trans_real from django.utils.translation import trans_real
trans_real._translations = {} trans_real._translations = {}
trans_real.check_for_language.cache_clear() trans_real.check_for_language.cache_clear()
@receiver(setting_changed) @receiver(setting_changed)
def localize_settings_changed(**kwargs): def localize_settings_changed(*, setting, **kwargs):
if kwargs['setting'] in FORMAT_SETTINGS or kwargs['setting'] == 'USE_THOUSAND_SEPARATOR': if setting in FORMAT_SETTINGS or setting == 'USE_THOUSAND_SEPARATOR':
reset_format_cache() reset_format_cache()
@receiver(setting_changed) @receiver(setting_changed)
def file_storage_changed(**kwargs): def file_storage_changed(*, setting, **kwargs):
if kwargs['setting'] == 'DEFAULT_FILE_STORAGE': if setting == 'DEFAULT_FILE_STORAGE':
from django.core.files.storage import default_storage from django.core.files.storage import default_storage
default_storage._wrapped = empty default_storage._wrapped = empty
@receiver(setting_changed) @receiver(setting_changed)
def complex_setting_changed(**kwargs): def complex_setting_changed(*, enter, setting, **kwargs):
if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS: if enter and setting in COMPLEX_OVERRIDE_SETTINGS:
# Considering the current implementation of the signals framework, # Considering the current implementation of the signals framework,
# this stacklevel shows the line containing the override_settings call. # this stacklevel shows the line containing the override_settings call.
warnings.warn("Overriding setting %s can lead to unexpected behavior." warnings.warn(
% kwargs['setting'], stacklevel=6) f'Overriding setting {setting} can lead to unexpected behavior.',
stacklevel=6,
)
@receiver(setting_changed) @receiver(setting_changed)
def root_urlconf_changed(**kwargs): def root_urlconf_changed(*, setting, **kwargs):
if kwargs['setting'] == 'ROOT_URLCONF': if setting == 'ROOT_URLCONF':
from django.urls import clear_url_caches, set_urlconf from django.urls import clear_url_caches, set_urlconf
clear_url_caches() clear_url_caches()
set_urlconf(None) set_urlconf(None)
@receiver(setting_changed) @receiver(setting_changed)
def static_storage_changed(**kwargs): def static_storage_changed(*, setting, **kwargs):
if kwargs['setting'] in { if setting in {
'STATICFILES_STORAGE', 'STATICFILES_STORAGE',
'STATIC_ROOT', 'STATIC_ROOT',
'STATIC_URL', 'STATIC_URL',
@ -164,8 +166,8 @@ def static_storage_changed(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def static_finders_changed(**kwargs): def static_finders_changed(*, setting, **kwargs):
if kwargs['setting'] in { if setting in {
'STATICFILES_DIRS', 'STATICFILES_DIRS',
'STATIC_ROOT', 'STATIC_ROOT',
}: }:
@ -174,8 +176,8 @@ def static_finders_changed(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def auth_password_validators_changed(**kwargs): def auth_password_validators_changed(*, setting, **kwargs):
if kwargs['setting'] == 'AUTH_PASSWORD_VALIDATORS': if setting == 'AUTH_PASSWORD_VALIDATORS':
from django.contrib.auth.password_validation import ( from django.contrib.auth.password_validation import (
get_default_password_validators, get_default_password_validators,
) )
@ -183,8 +185,8 @@ def auth_password_validators_changed(**kwargs):
@receiver(setting_changed) @receiver(setting_changed)
def user_model_swapped(**kwargs): def user_model_swapped(*, setting, **kwargs):
if kwargs['setting'] == 'AUTH_USER_MODEL': if setting == 'AUTH_USER_MODEL':
apps.clear_cache() apps.clear_cache()
try: try:
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model

View File

@ -47,12 +47,12 @@ language_code_prefix_re = _lazy_re_compile(r'^/(\w+([@-]\w+){0,2})(/|$)')
@receiver(setting_changed) @receiver(setting_changed)
def reset_cache(**kwargs): def reset_cache(*, setting, **kwargs):
""" """
Reset global state when LANGUAGES setting has been changed, as some Reset global state when LANGUAGES setting has been changed, as some
languages should no longer be accepted. languages should no longer be accepted.
""" """
if kwargs['setting'] in ('LANGUAGES', 'LANGUAGE_CODE'): if setting in ('LANGUAGES', 'LANGUAGE_CODE'):
check_for_language.cache_clear() check_for_language.cache_clear()
get_languages.cache_clear() get_languages.cache_clear()
get_supported_language_variant.cache_clear() get_supported_language_variant.cache_clear()

View File

@ -504,8 +504,8 @@ different user model.
from django.dispatch import receiver from django.dispatch import receiver
@receiver(setting_changed) @receiver(setting_changed)
def user_model_swapped(**kwargs): def user_model_swapped(*, setting, **kwargs):
if kwargs['setting'] == 'AUTH_USER_MODEL': if setting == 'AUTH_USER_MODEL':
apps.clear_cache() apps.clear_cache()
from myapp import some_module from myapp import some_module
some_module.UserModel = get_user_model() some_module.UserModel = get_user_model()