You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

185 lines
5.8 KiB

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