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