checks.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from __future__ import unicode_literals
  2. import pprint
  3. from django import VERSION as DJANGO_VERSION
  4. from django.conf import global_settings
  5. from django.core.checks import Warning, register
  6. from mezzanine.conf import settings
  7. from mezzanine.utils.deprecation import get_middleware_setting
  8. from mezzanine.utils.sites import SITE_PERMISSION_MIDDLEWARE
  9. @register()
  10. def check_template_settings(app_configs, **kwargs):
  11. issues = []
  12. if not settings.TEMPLATES:
  13. suggested_config = _build_suggested_template_config(settings)
  14. declaration = 'TEMPLATES = '
  15. config_formatted = pprint.pformat(suggested_config)
  16. config_formatted = "\n".join(' ' * len(declaration) + line
  17. for line in config_formatted.splitlines())
  18. config_formatted = declaration + config_formatted[len(declaration):]
  19. issues.append(Warning(
  20. "Please update your settings to use the TEMPLATES setting rather "
  21. "than the deprecated individual TEMPLATE_ settings. The latter "
  22. "are unsupported and correct behaviour is not guaranteed. Here's "
  23. "a suggestion based on on your existing configuration:\n\n%s\n"
  24. % config_formatted,
  25. id="mezzanine.core.W01"
  26. ))
  27. if settings.DEBUG != settings.TEMPLATE_DEBUG:
  28. issues.append(Warning(
  29. "TEMPLATE_DEBUG and DEBUG settings have different values, "
  30. "which may not be what you want. Mezzanine used to fix this "
  31. "for you, but doesn't any more. Update your settings.py to "
  32. "use the TEMPLATES setting to have template debugging "
  33. "controlled by the DEBUG setting.",
  34. id="mezzanine.core.W02"
  35. ))
  36. else:
  37. loader_tags_built_in = any(
  38. 'mezzanine.template.loader_tags'
  39. in config.get('OPTIONS', {}).get('builtins', {})
  40. for config in settings.TEMPLATES
  41. )
  42. if not DJANGO_VERSION < (1, 9) and not loader_tags_built_in:
  43. issues.append(Warning(
  44. "You haven't included 'mezzanine.template.loader_tags' as a "
  45. "builtin in any of your template configurations. Mezzanine's "
  46. "'overextends' tag will not be available in your templates.",
  47. id="mezzanine.core.W03"
  48. ))
  49. return issues
  50. def _build_suggested_template_config(settings):
  51. suggested_templates_config = {
  52. "BACKEND": "django.template.backends.django.DjangoTemplates",
  53. "OPTIONS": {
  54. "builtins": [
  55. "mezzanine.template.loader_tags",
  56. ],
  57. },
  58. }
  59. def set_setting(name, value, unconditional=False):
  60. if value or unconditional:
  61. suggested_templates_config[name] = value
  62. def set_option(name, value):
  63. if value:
  64. suggested_templates_config["OPTIONS"][name.lower()] = value
  65. def get_debug(_):
  66. if settings.TEMPLATE_DEBUG != settings.DEBUG:
  67. return settings.TEMPLATE_DEBUG
  68. def get_default(default):
  69. def getter(name):
  70. value = getattr(settings, name)
  71. if value == getattr(global_settings, name):
  72. value = default
  73. return value
  74. return getter
  75. default_context_processors = [
  76. "django.contrib.auth.context_processors.auth",
  77. "django.contrib.messages.context_processors.messages",
  78. "django.core.context_processors.debug",
  79. "django.core.context_processors.i18n",
  80. "django.core.context_processors.static",
  81. "django.core.context_processors.media",
  82. "django.core.context_processors.request",
  83. "django.core.context_processors.tz",
  84. "mezzanine.conf.context_processors.settings",
  85. "mezzanine.pages.context_processors.page",
  86. ]
  87. def get_loaders(_):
  88. """
  89. Django's default TEMPLATES setting doesn't specify loaders, instead
  90. dynamically sets a default based on whether or not APP_DIRS is True.
  91. We check here if the existing TEMPLATE_LOADERS setting matches one
  92. of those default cases, and omit the 'loaders' option if so.
  93. """
  94. template_loaders = list(settings.TEMPLATE_LOADERS)
  95. default_loaders = list(global_settings.TEMPLATE_LOADERS)
  96. if template_loaders == default_loaders:
  97. # Equivalent to Django's default with APP_DIRS True
  98. template_loaders = None
  99. app_dirs = True
  100. elif template_loaders == default_loaders[:1]:
  101. # Equivalent to Django's default with APP_DIRS False
  102. template_loaders = None
  103. app_dirs = False
  104. else:
  105. # This project has a custom loaders setting, which we'll use.
  106. # Custom loaders are incompatible with APP_DIRS.
  107. app_dirs = False
  108. return template_loaders, app_dirs
  109. def set_loaders(name, value):
  110. template_loaders, app_dirs = value
  111. set_option(name, template_loaders)
  112. set_setting('APP_DIRS', app_dirs, unconditional=True)
  113. old_settings = [
  114. ('ALLOWED_INCLUDE_ROOTS', settings.__getattr__, set_option),
  115. ('TEMPLATE_STRING_IF_INVALID', settings.__getattr__, set_option),
  116. ('TEMPLATE_DIRS', settings.__getattr__, set_setting),
  117. ('TEMPLATE_CONTEXT_PROCESSORS',
  118. get_default(default_context_processors), set_option),
  119. ('TEMPLATE_DEBUG', get_debug, set_option),
  120. ('TEMPLATE_LOADERS', get_loaders, set_loaders),
  121. ]
  122. def convert_setting_name(old_name):
  123. return old_name.rpartition('TEMPLATE_')[2]
  124. for setting_name, getter, setter in old_settings:
  125. value = getter(setting_name)
  126. new_setting_name = convert_setting_name(setting_name)
  127. setter(new_setting_name, value)
  128. return [suggested_templates_config]
  129. @register()
  130. def check_sites_middleware(app_configs, **kwargs):
  131. if SITE_PERMISSION_MIDDLEWARE not in get_middleware_setting():
  132. return [Warning(SITE_PERMISSION_MIDDLEWARE +
  133. " missing from settings.MIDDLEWARE - per site"
  134. " permissions not applied",
  135. id="mezzanine.core.W04")]
  136. return []