Skip to content

Commit db2de5b

Browse files
committed
Make SHOW_TOOLBAR_CALLBACK a dotted path.
Fix #473.
1 parent 03fd1cc commit db2de5b

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

debug_toolbar/middleware.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,8 @@ class DebugToolbarMiddleware(object):
3737
"""
3838
debug_toolbars = {}
3939

40-
def __init__(self):
41-
self.show_toolbar = dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK'] or show_toolbar
42-
self.insert_before = dt_settings.CONFIG['INSERT_BEFORE']
43-
4440
def process_request(self, request):
45-
if not self.show_toolbar(request):
41+
if not dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK'](request):
4642
return
4743
response = None
4844
toolbar = DebugToolbar(request)
@@ -82,7 +78,7 @@ def process_response(self, request, response):
8278
response.get('Content-Type', '').split(';')[0] in _HTML_TYPES):
8379
content = force_text(response.content, encoding=settings.DEFAULT_CHARSET)
8480
try:
85-
insert_at = content.lower().rindex(self.insert_before.lower())
81+
insert_at = content.lower().rindex(dt_settings.CONFIG['INSERT_BEFORE'].lower())
8682
except ValueError:
8783
pass
8884
else:

debug_toolbar/settings.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
'RESULTS_STORE_SIZE': 10,
2121
'ROOT_TAG_EXTRA_ATTRS': '',
2222
'SHOW_COLLAPSED': False,
23-
'SHOW_TOOLBAR_CALLBACK': None,
23+
'SHOW_TOOLBAR_CALLBACK': 'debug_toolbar.middleware.show_toolbar',
2424
# Panel options
2525
'EXTRA_SIGNALS': [],
2626
'ENABLE_STACKTRACES': True,
@@ -36,7 +36,6 @@
3636
'SQL_WARNING_THRESHOLD': 500, # milliseconds
3737
}
3838

39-
CONFIG = CONFIG_DEFAULTS.copy()
4039
USER_CONFIG = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {})
4140
# Backward-compatibility for 1.0, remove in 2.0.
4241
_RENAMED_CONFIG = {
@@ -60,7 +59,18 @@
6059
"TAG was replaced by INSERT_BEFORE. Update your "
6160
"DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)
6261
USER_CONFIG['INSERT_BEFORE'] = '</%s>' % USER_CONFIG.pop('TAG')
62+
63+
CONFIG = CONFIG_DEFAULTS.copy()
6364
CONFIG.update(USER_CONFIG)
65+
if isinstance(CONFIG['SHOW_TOOLBAR_CALLBACK'], six.string_types):
66+
# Replace this with import_by_path in Django >= 1.6.
67+
mod_path, func_name = CONFIG['SHOW_TOOLBAR_CALLBACK'].rsplit('.', 1)
68+
mod = import_module(mod_path)
69+
CONFIG['SHOW_TOOLBAR_CALLBACK'] = getattr(mod, func_name)
70+
else:
71+
warnings.warn(
72+
"SHOW_TOOLBAR_CALLBACK is now a dotted path. Update your "
73+
"DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)
6474

6575

6676
PANELS_DEFAULTS = [

docs/configuration.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,13 @@ Toolbar options
8585

8686
* ``SHOW_TOOLBAR_CALLBACK``
8787

88-
Default: ``None``
88+
Default: 'debug_toolbar.middleware.show_toolbar'
8989

90-
If set to ``None``, the debug toolbar middleware will use its built-in
91-
``show_toolbar`` method for determining whether the toolbar should show or
92-
not. The default checks are that ``DEBUG`` must be set to ``True``, the IP
93-
of the request must be in ``INTERNAL_IPS``, and the request must no be an
94-
AJAX request. You can provide your own method for displaying the toolbar
95-
which contains your custom logic. This method should return ``True`` or
96-
``False``.
90+
This is the dotted path to a function used for determining whether the
91+
toolbar should show or not. The default checks are that ``DEBUG`` must be
92+
set to ``True``, the IP of the request must be in ``INTERNAL_IPS``, and the
93+
request must no be an AJAX request. You can provide your own function that
94+
accepts a request in argument and returns ``True`` or ``False``.
9795

9896
Panel options
9997
~~~~~~~~~~~~~

0 commit comments

Comments
 (0)