Development of an internal social media platform with personalised dashboards for students
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.

signals.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import os
  2. import threading
  3. import time
  4. import warnings
  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(providing_args=["template", "context"])
  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
  23. caches._caches = threading.local()
  24. @receiver(setting_changed)
  25. def update_installed_apps(**kwargs):
  26. if kwargs['setting'] == 'INSTALLED_APPS':
  27. # Rebuild any AppDirectoriesFinder instance.
  28. from django.contrib.staticfiles.finders import get_finder
  29. get_finder.cache_clear()
  30. # Rebuild management commands cache
  31. from django.core.management import get_commands
  32. get_commands.cache_clear()
  33. # Rebuild get_app_template_dirs cache.
  34. from django.template.utils import get_app_template_dirs
  35. get_app_template_dirs.cache_clear()
  36. # Rebuild translations cache.
  37. from django.utils.translation import trans_real
  38. trans_real._translations = {}
  39. @receiver(setting_changed)
  40. def update_connections_time_zone(**kwargs):
  41. if kwargs['setting'] == 'TIME_ZONE':
  42. # Reset process time zone
  43. if hasattr(time, 'tzset'):
  44. if kwargs['value']:
  45. os.environ['TZ'] = kwargs['value']
  46. else:
  47. os.environ.pop('TZ', None)
  48. time.tzset()
  49. # Reset local time zone cache
  50. timezone.get_default_timezone.cache_clear()
  51. # Reset the database connections' time zone
  52. if kwargs['setting'] in {'TIME_ZONE', 'USE_TZ'}:
  53. for conn in connections.all():
  54. try:
  55. del conn.timezone
  56. except AttributeError:
  57. pass
  58. try:
  59. del conn.timezone_name
  60. except AttributeError:
  61. pass
  62. conn.ensure_timezone()
  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. 'DEBUG',
  72. 'FILE_CHARSET',
  73. 'INSTALLED_APPS',
  74. }:
  75. from django.template import engines
  76. try:
  77. del engines.templates
  78. except AttributeError:
  79. pass
  80. engines._templates = None
  81. engines._engines = {}
  82. from django.template.engine import Engine
  83. Engine.get_default.cache_clear()
  84. from django.forms.renderers import get_default_renderer
  85. get_default_renderer.cache_clear()
  86. @receiver(setting_changed)
  87. def clear_serializers_cache(**kwargs):
  88. if kwargs['setting'] == 'SERIALIZATION_MODULES':
  89. from django.core import serializers
  90. serializers._serializers = {}
  91. @receiver(setting_changed)
  92. def language_changed(**kwargs):
  93. if kwargs['setting'] in {'LANGUAGES', 'LANGUAGE_CODE', 'LOCALE_PATHS'}:
  94. from django.utils.translation import trans_real
  95. trans_real._default = None
  96. trans_real._active = threading.local()
  97. if kwargs['setting'] in {'LANGUAGES', 'LOCALE_PATHS'}:
  98. from django.utils.translation import trans_real
  99. trans_real._translations = {}
  100. trans_real.check_for_language.cache_clear()
  101. @receiver(setting_changed)
  102. def localize_settings_changed(**kwargs):
  103. if kwargs['setting'] in FORMAT_SETTINGS or kwargs['setting'] == 'USE_THOUSAND_SEPARATOR':
  104. reset_format_cache()
  105. @receiver(setting_changed)
  106. def file_storage_changed(**kwargs):
  107. if kwargs['setting'] == 'DEFAULT_FILE_STORAGE':
  108. from django.core.files.storage import default_storage
  109. default_storage._wrapped = empty
  110. @receiver(setting_changed)
  111. def complex_setting_changed(**kwargs):
  112. if kwargs['enter'] and kwargs['setting'] in COMPLEX_OVERRIDE_SETTINGS:
  113. # Considering the current implementation of the signals framework,
  114. # this stacklevel shows the line containing the override_settings call.
  115. warnings.warn("Overriding setting %s can lead to unexpected behavior."
  116. % kwargs['setting'], stacklevel=6)
  117. @receiver(setting_changed)
  118. def root_urlconf_changed(**kwargs):
  119. if kwargs['setting'] == 'ROOT_URLCONF':
  120. from django.urls import clear_url_caches, set_urlconf
  121. clear_url_caches()
  122. set_urlconf(None)
  123. @receiver(setting_changed)
  124. def static_storage_changed(**kwargs):
  125. if kwargs['setting'] in {
  126. 'STATICFILES_STORAGE',
  127. 'STATIC_ROOT',
  128. 'STATIC_URL',
  129. }:
  130. from django.contrib.staticfiles.storage import staticfiles_storage
  131. staticfiles_storage._wrapped = empty
  132. @receiver(setting_changed)
  133. def static_finders_changed(**kwargs):
  134. if kwargs['setting'] in {
  135. 'STATICFILES_DIRS',
  136. 'STATIC_ROOT',
  137. }:
  138. from django.contrib.staticfiles.finders import get_finder
  139. get_finder.cache_clear()
  140. @receiver(setting_changed)
  141. def auth_password_validators_changed(**kwargs):
  142. if kwargs['setting'] == 'AUTH_PASSWORD_VALIDATORS':
  143. from django.contrib.auth.password_validation import get_default_password_validators
  144. get_default_password_validators.cache_clear()
  145. @receiver(setting_changed)
  146. def user_model_swapped(**kwargs):
  147. if kwargs['setting'] == 'AUTH_USER_MODEL':
  148. apps.clear_cache()
  149. try:
  150. from django.contrib.auth import get_user_model
  151. UserModel = get_user_model()
  152. except ImproperlyConfigured:
  153. # Some tests set an invalid AUTH_USER_MODEL.
  154. pass
  155. else:
  156. from django.contrib.auth import backends
  157. backends.UserModel = UserModel
  158. from django.contrib.auth import forms
  159. forms.UserModel = UserModel
  160. from django.contrib.auth.handlers import modwsgi
  161. modwsgi.UserModel = UserModel
  162. from django.contrib.auth.management.commands import changepassword
  163. changepassword.UserModel = UserModel
  164. from django.contrib.auth import views
  165. views.UserModel = UserModel