signals.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import os
  2. import time
  3. import warnings
  4. from asgiref.local import Local
  5. from django.apps import apps
  6. from django.core.exceptions import ImproperlyConfigured
  7. from django.core.signals import setting_changed
  8. from django.db import connections, router
  9. from django.db.utils import ConnectionRouter
  10. from django.dispatch import Signal, receiver
  11. from django.utils import timezone
  12. from django.utils.formats import FORMAT_SETTINGS, reset_format_cache
  13. from django.utils.functional import empty
  14. template_rendered = Signal()
  15. # Most setting_changed receivers are supposed to be added below,
  16. # except for cases where the receiver is related to a contrib app.
  17. # Settings that may not work well when using 'override_settings' (#19031)
  18. COMPLEX_OVERRIDE_SETTINGS = {'DATABASES'}
  19. @receiver(setting_changed)
  20. def clear_cache_handlers(**kwargs):
  21. if kwargs['setting'] == 'CACHES':
  22. from django.core.cache import caches, close_caches
  23. close_caches()
  24. caches._settings = caches.settings = caches.configure_settings(None)
  25. caches._connections = Local()
  26. @receiver(setting_changed)
  27. def update_installed_apps(**kwargs):
  28. if kwargs['setting'] == 'INSTALLED_APPS':
  29. # Rebuild any AppDirectoriesFinder instance.
  30. from django.contrib.staticfiles.finders import get_finder
  31. get_finder.cache_clear()
  32. # Rebuild management commands cache
  33. from django.core.management import get_commands
  34. get_commands.cache_clear()
  35. # Rebuild get_app_template_dirs cache.
  36. from django.template.utils import get_app_template_dirs
  37. get_app_template_dirs.cache_clear()
  38. # Rebuild translations cache.
  39. from django.utils.translation import trans_real
  40. trans_real._translations = {}
  41. @receiver(setting_changed)
  42. def update_connections_time_zone(**kwargs):
  43. if kwargs['setting'] == 'TIME_ZONE':
  44. # Reset process time zone
  45. if hasattr(time, 'tzset'):
  46. if kwargs['value']:
  47. os.environ['TZ'] = kwargs['value']
  48. else:
  49. os.environ.pop('TZ', None)
  50. time.tzset()
  51. # Reset local time zone cache
  52. timezone.get_default_timezone.cache_clear()
  53. # Reset the database connections' time zone
  54. if kwargs['setting'] in {'TIME_ZONE', 'USE_TZ'}:
  55. for conn in connections.all():
  56. try:
  57. del conn.timezone
  58. except AttributeError:
  59. pass
  60. try:
  61. del conn.timezone_name
  62. except AttributeError:
  63. pass
  64. conn.ensure_timezone()
  65. @receiver(setting_changed)
  66. def clear_routers_cache(**kwargs):
  67. if kwargs['setting'] == 'DATABASE_ROUTERS':
  68. router.routers = ConnectionRouter().routers
  69. @receiver(setting_changed)
  70. def reset_template_engines(**kwargs):
  71. if kwargs['setting'] in {
  72. 'TEMPLATES',
  73. 'DEBUG',
  74. 'INSTALLED_APPS',
  75. }:
  76. from django.template import engines
  77. try:
  78. del engines.templates
  79. except AttributeError:
  80. pass
  81. engines._templates = None
  82. engines._engines = {}
  83. from django.template.engine import Engine
  84. Engine.get_default.cache_clear()
  85. from django.forms.renderers import get_default_renderer
  86. get_default_renderer.cache_clear()
  87. @receiver(setting_changed)
  88. def clear_serializers_cache(**kwargs):
  89. if kwargs['setting'] == 'SERIALIZATION_MODULES':
  90. from django.core import serializers
  91. serializers._serializers = {}
  92. @receiver(setting_changed)
  93. def language_changed(**kwargs):
  94. if kwargs['setting'] in {'LANGUAGES', 'LANGUAGE_CODE', 'LOCALE_PATHS'}:
  95. from django.utils.translation import trans_real
  96. trans_real._default = None
  97. trans_real._active = Local()
  98. if kwargs['setting'] in {'LANGUAGES', 'LOCALE_PATHS'}:
  99. from django.utils.translation import trans_real
  100. trans_real._translations = {}
  101. trans_real.check_for_language.cache_clear()
  102. @receiver(setting_changed)
  103. def localize_settings_changed(**kwargs):
  104. if kwargs['setting'] in FORMAT_SETTINGS or kwargs['setting'] == 'USE_THOUSAND_SEPARATOR':
  105. reset_format_cache()
  106. @receiver(setting_changed)
  107. def file_storage_changed(**kwargs):
  108. if kwargs['setting'] == 'DEFAULT_FILE_STORAGE':
  109. from django.core.files.storage import default_storage
  110. default_storage._wrapped = empty
  111. @receiver(setting_changed)
  112. def complex_setting_changed(**kwargs):
  113. if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS:
  114. # Considering the current implementation of the signals framework,
  115. # this stacklevel shows the line containing the override_settings call.
  116. warnings.warn("Overriding setting %s can lead to unexpected behavior."
  117. % kwargs['setting'], stacklevel=6)
  118. @receiver(setting_changed)
  119. def root_urlconf_changed(**kwargs):
  120. if kwargs['setting'] == 'ROOT_URLCONF':
  121. from django.urls import clear_url_caches, set_urlconf
  122. clear_url_caches()
  123. set_urlconf(None)
  124. @receiver(setting_changed)
  125. def static_storage_changed(**kwargs):
  126. if kwargs['setting'] in {
  127. 'STATICFILES_STORAGE',
  128. 'STATIC_ROOT',
  129. 'STATIC_URL',
  130. }:
  131. from django.contrib.staticfiles.storage import staticfiles_storage
  132. staticfiles_storage._wrapped = empty
  133. @receiver(setting_changed)
  134. def static_finders_changed(**kwargs):
  135. if kwargs['setting'] in {
  136. 'STATICFILES_DIRS',
  137. 'STATIC_ROOT',
  138. }:
  139. from django.contrib.staticfiles.finders import get_finder
  140. get_finder.cache_clear()
  141. @receiver(setting_changed)
  142. def auth_password_validators_changed(**kwargs):
  143. if kwargs['setting'] == 'AUTH_PASSWORD_VALIDATORS':
  144. from django.contrib.auth.password_validation import (
  145. get_default_password_validators,
  146. )
  147. get_default_password_validators.cache_clear()
  148. @receiver(setting_changed)
  149. def user_model_swapped(**kwargs):
  150. if kwargs['setting'] == 'AUTH_USER_MODEL':
  151. apps.clear_cache()
  152. try:
  153. from django.contrib.auth import get_user_model
  154. UserModel = get_user_model()
  155. except ImproperlyConfigured:
  156. # Some tests set an invalid AUTH_USER_MODEL.
  157. pass
  158. else:
  159. from django.contrib.auth import backends
  160. backends.UserModel = UserModel
  161. from django.contrib.auth import forms
  162. forms.UserModel = UserModel
  163. from django.contrib.auth.handlers import modwsgi
  164. modwsgi.UserModel = UserModel
  165. from django.contrib.auth.management.commands import changepassword
  166. changepassword.UserModel = UserModel
  167. from django.contrib.auth import views
  168. views.UserModel = UserModel