Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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(*, setting, **kwargs):
  21. if 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(*, setting, **kwargs):
  28. if 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(*, setting, **kwargs):
  43. if 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 setting in {"TIME_ZONE", "USE_TZ"}:
  55. for conn in connections.all(initialized_only=True):
  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(*, setting, **kwargs):
  67. if setting == "DATABASE_ROUTERS":
  68. router.routers = ConnectionRouter().routers
  69. @receiver(setting_changed)
  70. def reset_template_engines(*, setting, **kwargs):
  71. if 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(*, setting, **kwargs):
  89. if setting == "SERIALIZATION_MODULES":
  90. from django.core import serializers
  91. serializers._serializers = {}
  92. @receiver(setting_changed)
  93. def language_changed(*, setting, **kwargs):
  94. if 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 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(*, setting, **kwargs):
  104. if setting in FORMAT_SETTINGS or setting == "USE_THOUSAND_SEPARATOR":
  105. reset_format_cache()
  106. @receiver(setting_changed)
  107. def file_storage_changed(*, setting, **kwargs):
  108. if 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(*, enter, setting, **kwargs):
  113. if enter and 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(
  117. f"Overriding setting {setting} can lead to unexpected behavior.",
  118. stacklevel=6,
  119. )
  120. @receiver(setting_changed)
  121. def root_urlconf_changed(*, setting, **kwargs):
  122. if setting == "ROOT_URLCONF":
  123. from django.urls import clear_url_caches, set_urlconf
  124. clear_url_caches()
  125. set_urlconf(None)
  126. @receiver(setting_changed)
  127. def static_storage_changed(*, setting, **kwargs):
  128. if setting in {
  129. "STATICFILES_STORAGE",
  130. "STATIC_ROOT",
  131. "STATIC_URL",
  132. }:
  133. from django.contrib.staticfiles.storage import staticfiles_storage
  134. staticfiles_storage._wrapped = empty
  135. @receiver(setting_changed)
  136. def static_finders_changed(*, setting, **kwargs):
  137. if setting in {
  138. "STATICFILES_DIRS",
  139. "STATIC_ROOT",
  140. }:
  141. from django.contrib.staticfiles.finders import get_finder
  142. get_finder.cache_clear()
  143. @receiver(setting_changed)
  144. def auth_password_validators_changed(*, setting, **kwargs):
  145. if setting == "AUTH_PASSWORD_VALIDATORS":
  146. from django.contrib.auth.password_validation import (
  147. get_default_password_validators,
  148. )
  149. get_default_password_validators.cache_clear()
  150. @receiver(setting_changed)
  151. def user_model_swapped(*, setting, **kwargs):
  152. if setting == "AUTH_USER_MODEL":
  153. apps.clear_cache()
  154. try:
  155. from django.contrib.auth import get_user_model
  156. UserModel = get_user_model()
  157. except ImproperlyConfigured:
  158. # Some tests set an invalid AUTH_USER_MODEL.
  159. pass
  160. else:
  161. from django.contrib.auth import backends
  162. backends.UserModel = UserModel
  163. from django.contrib.auth import forms
  164. forms.UserModel = UserModel
  165. from django.contrib.auth.handlers import modwsgi
  166. modwsgi.UserModel = UserModel
  167. from django.contrib.auth.management.commands import changepassword
  168. changepassword.UserModel = UserModel
  169. from django.contrib.auth import views
  170. views.UserModel = UserModel