|
5 | 5 |
|
6 | 6 | from django.conf import settings
|
7 | 7 | from django.utils import six
|
| 8 | +from django.utils.lru_cache import lru_cache |
8 | 9 | from django.utils.module_loading import import_string
|
9 | 10 |
|
10 | 11 | # Always import this module as follows:
|
|
39 | 40 | 'SQL_WARNING_THRESHOLD': 500, # milliseconds
|
40 | 41 | }
|
41 | 42 |
|
42 |
| -USER_CONFIG = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) |
43 |
| -# Backward-compatibility for 1.0, remove in 2.0. |
44 |
| -_RENAMED_CONFIG = { |
45 |
| - 'RESULTS_STORE_SIZE': 'RESULTS_CACHE_SIZE', |
46 |
| - 'ROOT_TAG_ATTRS': 'ROOT_TAG_EXTRA_ATTRS', |
47 |
| - 'HIDDEN_STACKTRACE_MODULES': 'HIDE_IN_STACKTRACES' |
48 |
| -} |
49 |
| -for old_name, new_name in _RENAMED_CONFIG.items(): |
50 |
| - if old_name in USER_CONFIG: |
| 43 | + |
| 44 | +@lru_cache() |
| 45 | +def get_config(): |
| 46 | + USER_CONFIG = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) |
| 47 | + |
| 48 | + # Backward-compatibility for 1.0, remove in 2.0. |
| 49 | + _RENAMED_CONFIG = { |
| 50 | + 'RESULTS_STORE_SIZE': 'RESULTS_CACHE_SIZE', |
| 51 | + 'ROOT_TAG_ATTRS': 'ROOT_TAG_EXTRA_ATTRS', |
| 52 | + 'HIDDEN_STACKTRACE_MODULES': 'HIDE_IN_STACKTRACES' |
| 53 | + } |
| 54 | + for old_name, new_name in _RENAMED_CONFIG.items(): |
| 55 | + if old_name in USER_CONFIG: |
| 56 | + warnings.warn( |
| 57 | + "%r was renamed to %r. Update your DEBUG_TOOLBAR_CONFIG " |
| 58 | + "setting." % (old_name, new_name), DeprecationWarning) |
| 59 | + USER_CONFIG[new_name] = USER_CONFIG.pop(old_name) |
| 60 | + |
| 61 | + if 'HIDE_DJANGO_SQL' in USER_CONFIG: |
| 62 | + warnings.warn( |
| 63 | + "HIDE_DJANGO_SQL was removed. Update your " |
| 64 | + "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning) |
| 65 | + USER_CONFIG.pop('HIDE_DJANGO_SQL') |
| 66 | + |
| 67 | + if 'TAG' in USER_CONFIG: |
51 | 68 | warnings.warn(
|
52 |
| - "%r was renamed to %r. Update your DEBUG_TOOLBAR_CONFIG " |
53 |
| - "setting." % (old_name, new_name), DeprecationWarning) |
54 |
| - USER_CONFIG[new_name] = USER_CONFIG.pop(old_name) |
55 |
| -if 'HIDE_DJANGO_SQL' in USER_CONFIG: |
56 |
| - warnings.warn( |
57 |
| - "HIDE_DJANGO_SQL was removed. Update your " |
58 |
| - "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning) |
59 |
| - USER_CONFIG.pop('HIDE_DJANGO_SQL') |
60 |
| -if 'TAG' in USER_CONFIG: |
61 |
| - warnings.warn( |
62 |
| - "TAG was replaced by INSERT_BEFORE. Update your " |
63 |
| - "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning) |
64 |
| - USER_CONFIG['INSERT_BEFORE'] = '</%s>' % USER_CONFIG.pop('TAG') |
65 |
| - |
66 |
| -CONFIG = CONFIG_DEFAULTS.copy() |
67 |
| -CONFIG.update(USER_CONFIG) |
| 69 | + "TAG was replaced by INSERT_BEFORE. Update your " |
| 70 | + "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning) |
| 71 | + USER_CONFIG['INSERT_BEFORE'] = '</%s>' % USER_CONFIG.pop('TAG') |
| 72 | + |
| 73 | + CONFIG = CONFIG_DEFAULTS.copy() |
| 74 | + CONFIG.update(USER_CONFIG) |
| 75 | + |
| 76 | + if 'INTERCEPT_REDIRECTS' in USER_CONFIG: |
| 77 | + warnings.warn( |
| 78 | + "INTERCEPT_REDIRECTS is deprecated. Please use the " |
| 79 | + "DISABLE_PANELS config in the " |
| 80 | + "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning) |
| 81 | + if USER_CONFIG['INTERCEPT_REDIRECTS']: |
| 82 | + if 'debug_toolbar.panels.redirects.RedirectsPanel' \ |
| 83 | + in CONFIG['DISABLE_PANELS']: |
| 84 | + # RedirectsPanel should be enabled |
| 85 | + try: |
| 86 | + CONFIG['DISABLE_PANELS'].remove( |
| 87 | + 'debug_toolbar.panels.redirects.RedirectsPanel' |
| 88 | + ) |
| 89 | + except KeyError: |
| 90 | + # We wanted to remove it, but it didn't exist. This is fine |
| 91 | + pass |
| 92 | + elif 'debug_toolbar.panels.redirects.RedirectsPanel' \ |
| 93 | + not in CONFIG['DISABLE_PANELS']: |
| 94 | + # RedirectsPanel should be disabled |
| 95 | + CONFIG['DISABLE_PANELS'].add( |
| 96 | + 'debug_toolbar.panels.redirects.RedirectsPanel' |
| 97 | + ) |
| 98 | + |
| 99 | + return CONFIG |
68 | 100 |
|
69 | 101 |
|
70 | 102 | PANELS_DEFAULTS = [
|
|
82 | 114 | 'debug_toolbar.panels.redirects.RedirectsPanel',
|
83 | 115 | ]
|
84 | 116 |
|
85 |
| -try: |
86 |
| - PANELS = list(settings.DEBUG_TOOLBAR_PANELS) |
87 |
| -except AttributeError: |
88 |
| - PANELS = PANELS_DEFAULTS |
89 |
| -else: |
90 |
| - # Backward-compatibility for 1.0, remove in 2.0. |
91 |
| - _RENAMED_PANELS = { |
92 |
| - 'debug_toolbar.panels.version.VersionDebugPanel': |
93 |
| - 'debug_toolbar.panels.versions.VersionsPanel', |
94 |
| - 'debug_toolbar.panels.timer.TimerDebugPanel': |
95 |
| - 'debug_toolbar.panels.timer.TimerPanel', |
96 |
| - 'debug_toolbar.panels.settings_vars.SettingsDebugPanel': |
97 |
| - 'debug_toolbar.panels.settings.SettingsPanel', |
98 |
| - 'debug_toolbar.panels.headers.HeaderDebugPanel': |
99 |
| - 'debug_toolbar.panels.headers.HeadersPanel', |
100 |
| - 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel': |
101 |
| - 'debug_toolbar.panels.request.RequestPanel', |
102 |
| - 'debug_toolbar.panels.sql.SQLDebugPanel': |
103 |
| - 'debug_toolbar.panels.sql.SQLPanel', |
104 |
| - 'debug_toolbar.panels.template.TemplateDebugPanel': |
105 |
| - 'debug_toolbar.panels.templates.TemplatesPanel', |
106 |
| - 'debug_toolbar.panels.cache.CacheDebugPanel': |
107 |
| - 'debug_toolbar.panels.cache.CachePanel', |
108 |
| - 'debug_toolbar.panels.signals.SignalDebugPanel': |
109 |
| - 'debug_toolbar.panels.signals.SignalsPanel', |
110 |
| - 'debug_toolbar.panels.logger.LoggingDebugPanel': |
111 |
| - 'debug_toolbar.panels.logging.LoggingPanel', |
112 |
| - 'debug_toolbar.panels.redirects.InterceptRedirectsDebugPanel': |
113 |
| - 'debug_toolbar.panels.redirects.RedirectsPanel', |
114 |
| - 'debug_toolbar.panels.profiling.ProfilingDebugPanel': |
115 |
| - 'debug_toolbar.panels.profiling.ProfilingPanel', |
116 |
| - } |
117 |
| - for index, old_panel in enumerate(PANELS): |
118 |
| - new_panel = _RENAMED_PANELS.get(old_panel) |
119 |
| - if new_panel is not None: |
120 |
| - warnings.warn( |
121 |
| - "%r was renamed to %r. Update your DEBUG_TOOLBAR_PANELS " |
122 |
| - "setting." % (old_panel, new_panel), DeprecationWarning) |
123 |
| - PANELS[index] = new_panel |
124 |
| - |
125 |
| - |
126 |
| -if 'INTERCEPT_REDIRECTS' in USER_CONFIG: |
127 |
| - warnings.warn( |
128 |
| - "INTERCEPT_REDIRECTS is deprecated. Please use the " |
129 |
| - "DISABLE_PANELS config in the " |
130 |
| - "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning) |
131 |
| - if USER_CONFIG['INTERCEPT_REDIRECTS']: |
132 |
| - if 'debug_toolbar.panels.redirects.RedirectsPanel' \ |
133 |
| - in CONFIG['DISABLE_PANELS']: |
134 |
| - # RedirectsPanel should be enabled |
135 |
| - try: |
136 |
| - CONFIG['DISABLE_PANELS'].remove( |
137 |
| - 'debug_toolbar.panels.redirects.RedirectsPanel' |
138 |
| - ) |
139 |
| - except KeyError: |
140 |
| - # We wanted to remove it, but it didn't exist. This is fine |
141 |
| - pass |
142 |
| - elif 'debug_toolbar.panels.redirects.RedirectsPanel' \ |
143 |
| - not in CONFIG['DISABLE_PANELS']: |
144 |
| - # RedirectsPanel should be disabled |
145 |
| - CONFIG['DISABLE_PANELS'].add( |
146 |
| - 'debug_toolbar.panels.redirects.RedirectsPanel' |
147 |
| - ) |
148 |
| - |
149 |
| -PATCH_SETTINGS = getattr(settings, 'DEBUG_TOOLBAR_PATCH_SETTINGS', settings.DEBUG) |
| 117 | + |
| 118 | +@lru_cache() |
| 119 | +def get_panels(): |
| 120 | + try: |
| 121 | + PANELS = list(settings.DEBUG_TOOLBAR_PANELS) |
| 122 | + except AttributeError: |
| 123 | + PANELS = PANELS_DEFAULTS |
| 124 | + else: |
| 125 | + # Backward-compatibility for 1.0, remove in 2.0. |
| 126 | + _RENAMED_PANELS = { |
| 127 | + 'debug_toolbar.panels.version.VersionDebugPanel': |
| 128 | + 'debug_toolbar.panels.versions.VersionsPanel', |
| 129 | + 'debug_toolbar.panels.timer.TimerDebugPanel': |
| 130 | + 'debug_toolbar.panels.timer.TimerPanel', |
| 131 | + 'debug_toolbar.panels.settings_vars.SettingsDebugPanel': |
| 132 | + 'debug_toolbar.panels.settings.SettingsPanel', |
| 133 | + 'debug_toolbar.panels.headers.HeaderDebugPanel': |
| 134 | + 'debug_toolbar.panels.headers.HeadersPanel', |
| 135 | + 'debug_toolbar.panels.request_vars.RequestVarsDebugPanel': |
| 136 | + 'debug_toolbar.panels.request.RequestPanel', |
| 137 | + 'debug_toolbar.panels.sql.SQLDebugPanel': |
| 138 | + 'debug_toolbar.panels.sql.SQLPanel', |
| 139 | + 'debug_toolbar.panels.template.TemplateDebugPanel': |
| 140 | + 'debug_toolbar.panels.templates.TemplatesPanel', |
| 141 | + 'debug_toolbar.panels.cache.CacheDebugPanel': |
| 142 | + 'debug_toolbar.panels.cache.CachePanel', |
| 143 | + 'debug_toolbar.panels.signals.SignalDebugPanel': |
| 144 | + 'debug_toolbar.panels.signals.SignalsPanel', |
| 145 | + 'debug_toolbar.panels.logger.LoggingDebugPanel': |
| 146 | + 'debug_toolbar.panels.logging.LoggingPanel', |
| 147 | + 'debug_toolbar.panels.redirects.InterceptRedirectsDebugPanel': |
| 148 | + 'debug_toolbar.panels.redirects.RedirectsPanel', |
| 149 | + 'debug_toolbar.panels.profiling.ProfilingDebugPanel': |
| 150 | + 'debug_toolbar.panels.profiling.ProfilingPanel', |
| 151 | + } |
| 152 | + for index, old_panel in enumerate(PANELS): |
| 153 | + new_panel = _RENAMED_PANELS.get(old_panel) |
| 154 | + if new_panel is not None: |
| 155 | + warnings.warn( |
| 156 | + "%r was renamed to %r. Update your DEBUG_TOOLBAR_PANELS " |
| 157 | + "setting." % (old_panel, new_panel), DeprecationWarning) |
| 158 | + PANELS[index] = new_panel |
| 159 | + return PANELS |
| 160 | + |
| 161 | + |
| 162 | +@lru_cache() |
| 163 | +def get_patch_settings(): |
| 164 | + return getattr(settings, 'DEBUG_TOOLBAR_PATCH_SETTINGS', settings.DEBUG) |
150 | 165 |
|
151 | 166 |
|
152 | 167 | # The following functions can monkey-patch settings automatically. Several
|
|
0 commit comments