diff options
| author | Aymeric Augustin | 2013-11-24 12:00:24 +0100 | 
|---|---|---|
| committer | Aymeric Augustin | 2013-11-24 12:00:24 +0100 | 
| commit | db2de5b85ac1ae099b91bae402d21d6ea36ef306 (patch) | |
| tree | 40238c6a121bd5a37ee4e4519cd648ec36cfd46b | |
| parent | 03fd1cc81c02a5462aeb4dbce0bfe8a2afdef43d (diff) | |
| download | django-debug-toolbar-db2de5b85ac1ae099b91bae402d21d6ea36ef306.tar.bz2 | |
Make SHOW_TOOLBAR_CALLBACK a dotted path.
Fix #473.
| -rw-r--r-- | debug_toolbar/middleware.py | 8 | ||||
| -rw-r--r-- | debug_toolbar/settings.py | 14 | ||||
| -rw-r--r-- | docs/configuration.rst | 14 | 
3 files changed, 20 insertions, 16 deletions
| diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index e402553..b7114af 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -37,12 +37,8 @@ class DebugToolbarMiddleware(object):      """      debug_toolbars = {} -    def __init__(self): -        self.show_toolbar = dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK'] or show_toolbar -        self.insert_before = dt_settings.CONFIG['INSERT_BEFORE'] -      def process_request(self, request): -        if not self.show_toolbar(request): +        if not dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK'](request):              return          response = None          toolbar = DebugToolbar(request) @@ -82,7 +78,7 @@ class DebugToolbarMiddleware(object):                  response.get('Content-Type', '').split(';')[0] in _HTML_TYPES):              content = force_text(response.content, encoding=settings.DEFAULT_CHARSET)              try: -                insert_at = content.lower().rindex(self.insert_before.lower()) +                insert_at = content.lower().rindex(dt_settings.CONFIG['INSERT_BEFORE'].lower())              except ValueError:                  pass              else: diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index 5a58b6b..8534c2a 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -20,7 +20,7 @@ CONFIG_DEFAULTS = {      'RESULTS_STORE_SIZE': 10,      'ROOT_TAG_EXTRA_ATTRS': '',      'SHOW_COLLAPSED': False, -    'SHOW_TOOLBAR_CALLBACK': None, +    'SHOW_TOOLBAR_CALLBACK': 'debug_toolbar.middleware.show_toolbar',      # Panel options      'EXTRA_SIGNALS': [],      'ENABLE_STACKTRACES': True, @@ -36,7 +36,6 @@ CONFIG_DEFAULTS = {      'SQL_WARNING_THRESHOLD': 500,   # milliseconds  } -CONFIG = CONFIG_DEFAULTS.copy()  USER_CONFIG = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {})  # Backward-compatibility for 1.0, remove in 2.0.  _RENAMED_CONFIG = { @@ -60,7 +59,18 @@ if 'TAG' in USER_CONFIG:          "TAG was replaced by INSERT_BEFORE. Update your "          "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)      USER_CONFIG['INSERT_BEFORE'] = '</%s>' % USER_CONFIG.pop('TAG') + +CONFIG = CONFIG_DEFAULTS.copy()  CONFIG.update(USER_CONFIG) +if isinstance(CONFIG['SHOW_TOOLBAR_CALLBACK'], six.string_types): +    # Replace this with import_by_path in Django >= 1.6. +    mod_path, func_name = CONFIG['SHOW_TOOLBAR_CALLBACK'].rsplit('.', 1) +    mod = import_module(mod_path) +    CONFIG['SHOW_TOOLBAR_CALLBACK'] = getattr(mod, func_name) +else: +    warnings.warn( +        "SHOW_TOOLBAR_CALLBACK is now a dotted path. Update your " +        "DEBUG_TOOLBAR_CONFIG setting.", DeprecationWarning)  PANELS_DEFAULTS = [ diff --git a/docs/configuration.rst b/docs/configuration.rst index a276aa3..c1fe65d 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -85,15 +85,13 @@ Toolbar options  * ``SHOW_TOOLBAR_CALLBACK`` -  Default: ``None`` +  Default: 'debug_toolbar.middleware.show_toolbar' -  If set to ``None``, the debug toolbar middleware will use its built-in -  ``show_toolbar`` method for determining whether the toolbar should show or -  not. The default checks are that ``DEBUG`` must be set to ``True``, the IP -  of the request must be in ``INTERNAL_IPS``, and the request must no be an -  AJAX request. You can provide your own method for displaying the toolbar -  which contains your custom logic. This method should return ``True`` or -  ``False``. +  This is the dotted path to a function used for determining whether the +  toolbar should show or not. The default checks are that ``DEBUG`` must be +  set to ``True``, the IP of the request must be in ``INTERNAL_IPS``, and the +  request must no be an AJAX request. You can provide your own function that +  accepts a request in argument and returns ``True`` or ``False``.  Panel options  ~~~~~~~~~~~~~ | 
