diff options
| author | Aymeric Augustin | 2013-10-27 12:48:44 +0100 |
|---|---|---|
| committer | Aymeric Augustin | 2013-10-27 12:48:44 +0100 |
| commit | 9cb9796791062c9d208c0f887dc65142d42c022a (patch) | |
| tree | 40ba69bec7f39115ccfcc88c5b1cfa2baf2f86ea /debug_toolbar | |
| parent | c710140a15222af2cf31b7579c6a5fedf9a482d5 (diff) | |
| download | django-debug-toolbar-9cb9796791062c9d208c0f887dc65142d42c022a.tar.bz2 | |
Centralize access to config and default values.
Diffstat (limited to 'debug_toolbar')
| -rw-r--r-- | debug_toolbar/middleware.py | 17 | ||||
| -rw-r--r-- | debug_toolbar/panels/cache.py | 5 | ||||
| -rw-r--r-- | debug_toolbar/panels/signals.py | 8 | ||||
| -rw-r--r-- | debug_toolbar/panels/template.py | 3 | ||||
| -rw-r--r-- | debug_toolbar/toolbar/loader.py | 11 | ||||
| -rw-r--r-- | debug_toolbar/utils/__init__.py | 15 | ||||
| -rw-r--r-- | debug_toolbar/utils/settings.py | 27 | ||||
| -rw-r--r-- | debug_toolbar/utils/tracking/db.py | 13 |
8 files changed, 48 insertions, 51 deletions
diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 2c47227..76402bd 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -16,6 +16,7 @@ from django.utils import six import debug_toolbar.urls from debug_toolbar.toolbar.loader import DebugToolbar +from debug_toolbar.utils.settings import CONFIG _HTML_TYPES = ('text/html', 'application/xhtml+xml') # Handles python threading module bug - http://bugs.python.org/issue14308 @@ -60,21 +61,11 @@ class DebugToolbarMiddleware(object): def __init__(self): self._urlconfs = {} - # Set method to use to decide to show toolbar - self.show_toolbar = show_toolbar + # The method to call to decide to show the toolbar + self.show_toolbar = CONFIG['SHOW_TOOLBAR_CALLBACK'] or show_toolbar # The tag to attach the toolbar to - self.tag = '</body>' - - if hasattr(settings, 'DEBUG_TOOLBAR_CONFIG'): - show_toolbar_callback = settings.DEBUG_TOOLBAR_CONFIG.get( - 'SHOW_TOOLBAR_CALLBACK', None) - if show_toolbar_callback: - self.show_toolbar = show_toolbar_callback - - tag = settings.DEBUG_TOOLBAR_CONFIG.get('TAG', None) - if tag: - self.tag = '</' + tag + '>' + self.tag = '</%s>' % CONFIG['TAG'] def process_request(self, request): __traceback_hide__ = True # noqa diff --git a/debug_toolbar/panels/cache.py b/debug_toolbar/panels/cache.py index b3cb540..8127d55 100644 --- a/debug_toolbar/panels/cache.py +++ b/debug_toolbar/panels/cache.py @@ -16,6 +16,7 @@ from django.utils.translation import ugettext_lazy as _, ungettext from debug_toolbar.panels import DebugPanel from debug_toolbar.utils import (tidy_stacktrace, render_stacktrace, get_template_info, get_stack) +from debug_toolbar.utils.settings import CONFIG cache_called = Signal(providing_args=[ @@ -28,9 +29,7 @@ def send_signal(method): value = method(self, *args, **kwargs) t = time.time() - t - debug_toolbar_config = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) - enable_stacktraces = debug_toolbar_config.get('ENABLE_STACKTRACES', True) - if enable_stacktraces: + if CONFIG['ENABLE_STACKTRACES']: stacktrace = tidy_stacktrace(reversed(get_stack())) else: stacktrace = [] diff --git a/debug_toolbar/panels/signals.py b/debug_toolbar/panels/signals.py index f7d4239..ae6b773 100644 --- a/debug_toolbar/panels/signals.py +++ b/debug_toolbar/panels/signals.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals -from django.conf import settings from django.core.signals import ( request_started, request_finished, got_request_exception) from django.db.models.signals import ( @@ -17,6 +16,7 @@ except ImportError: connection_created = None from debug_toolbar.panels import DebugPanel +from debug_toolbar.utils.settings import CONFIG class SignalDebugPanel(DebugPanel): @@ -66,11 +66,7 @@ class SignalDebugPanel(DebugPanel): @property def signals(self): signals = self.SIGNALS.copy() - if hasattr(settings, 'DEBUG_TOOLBAR_CONFIG'): - extra_signals = settings.DEBUG_TOOLBAR_CONFIG.get('EXTRA_SIGNALS', []) - else: - extra_signals = [] - for signal in extra_signals: + for signal in CONFIG['EXTRA_SIGNALS']: mod_path, signal_name = signal.rsplit('.', 1) signals_mod = import_module(mod_path) signals[signal_name] = getattr(signals_mod, signal_name) diff --git a/debug_toolbar/panels/template.py b/debug_toolbar/panels/template.py index d8f96f2..f8cd46b 100644 --- a/debug_toolbar/panels/template.py +++ b/debug_toolbar/panels/template.py @@ -12,6 +12,7 @@ from django.utils.translation import ugettext_lazy as _ from django.db.models.query import QuerySet, RawQuerySet from debug_toolbar.panels import DebugPanel from debug_toolbar.utils.tracking.db import recording, SQLQueryTriggered +from debug_toolbar.utils.settings import CONFIG # Code taken and adapted from Simon Willison and Django Snippets: # http://www.djangosnippets.org/snippets/766/ @@ -133,7 +134,7 @@ class TemplateDebugPanel(DebugPanel): template.origin_name = 'No origin' info['template'] = template # Clean up context for better readability - if getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}).get('SHOW_TEMPLATE_CONTEXT', True): + if CONFIG['SHOW_TEMPLATE_CONTEXT']: context_list = template_data.get('context', []) info['context'] = '\n'.join(context_list) template_context.append(info) diff --git a/debug_toolbar/toolbar/loader.py b/debug_toolbar/toolbar/loader.py index 491f66c..c8630a9 100644 --- a/debug_toolbar/toolbar/loader.py +++ b/debug_toolbar/toolbar/loader.py @@ -9,6 +9,8 @@ from django.template.loader import render_to_string from django.utils.datastructures import SortedDict from django.utils.importlib import import_module +from debug_toolbar.utils.settings import CONFIG + class DebugToolbar(object): @@ -17,17 +19,14 @@ class DebugToolbar(object): self._panels = SortedDict() base_url = self.request.META.get('SCRIPT_NAME', '') self.config = { - 'INTERCEPT_REDIRECTS': False, 'MEDIA_URL': '%s/__debug__/m/' % base_url, - 'ROOT_TAG_ATTRS': '', } - # Check if settings has a DEBUG_TOOLBAR_CONFIG and updated config - self.config.update(getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {})) + self.config.update(CONFIG) self.template_context = { 'BASE_URL': base_url, # for backwards compatibility - 'DEBUG_TOOLBAR_MEDIA_URL': self.config.get('MEDIA_URL'), + 'DEBUG_TOOLBAR_MEDIA_URL': self.config['MEDIA_URL'], 'STATIC_URL': settings.STATIC_URL, - 'TOOLBAR_ROOT_TAG_ATTRS': self.config.get('ROOT_TAG_ATTRS'), + 'TOOLBAR_ROOT_TAG_ATTRS': self.config['ROOT_TAG_ATTRS'], } self.load_panels() diff --git a/debug_toolbar/utils/__init__.py b/debug_toolbar/utils/__init__.py index 73e38e2..c421dce 100644 --- a/debug_toolbar/utils/__init__.py +++ b/debug_toolbar/utils/__init__.py @@ -14,13 +14,11 @@ from django.utils.safestring import mark_safe from django.utils import six from django.views.debug import linebreak_iter +from .settings import CONFIG # Figure out some paths django_path = os.path.realpath(os.path.dirname(django.__file__)) -config = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) -hide_django_sql = config.get('HIDE_DJANGO_SQL', True) - def get_module_path(module_name): try: @@ -37,14 +35,7 @@ def get_module_path(module_name): hidden_paths = [ get_module_path(module_name) - for module_name in config.get( - 'HIDDEN_STACKTRACE_MODULES', ( - 'socketserver' if six.PY3 else 'SocketServer', - 'threading', - 'wsgiref', - 'debug_toolbar', - ) - ) + for module_name in CONFIG['HIDDEN_STACKTRACE_MODULES'] ] @@ -68,7 +59,7 @@ def tidy_stacktrace(stack): # inspection. if '__traceback_hide__' in frame.f_locals: continue - if hide_django_sql and django_path in s_path and not 'django/contrib' in s_path: + if CONFIG['HIDE_DJANGO_SQL'] and django_path in s_path and not 'django/contrib' in s_path: continue if omit_path(s_path): continue diff --git a/debug_toolbar/utils/settings.py b/debug_toolbar/utils/settings.py new file mode 100644 index 0000000..482781b --- /dev/null +++ b/debug_toolbar/utils/settings.py @@ -0,0 +1,27 @@ +from __future__ import unicode_literals + +from django.conf import settings +from django.utils import six + + +CONFIG_DEFAULTS = { + 'INTERCEPT_REDIRECTS': False, + 'SHOW_TOOLBAR_CALLBACK': None, + 'EXTRA_SIGNALS': [], + 'HIDE_DJANGO_SQL': True, + 'SHOW_TEMPLATE_CONTEXT': True, + 'TAG': 'body', + 'ENABLE_STACKTRACES': True, + 'HIDDEN_STACKTRACE_MODULES': ( + 'socketserver' if six.PY3 else 'SocketServer', + 'threading', + 'wsgiref', + 'debug_toolbar', + ), + 'ROOT_TAG_ATTRS': '', +} + + +CONFIG = {} +CONFIG.update(CONFIG_DEFAULTS) +CONFIG.update(getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {})) diff --git a/debug_toolbar/utils/tracking/db.py b/debug_toolbar/utils/tracking/db.py index 5822270..b195f4a 100644 --- a/debug_toolbar/utils/tracking/db.py +++ b/debug_toolbar/utils/tracking/db.py @@ -12,12 +12,7 @@ from django.utils.encoding import force_text from django.utils import six from debug_toolbar.utils import tidy_stacktrace, get_template_info, get_stack - - -# TODO:This should be set in the toolbar loader as a default and panels should -# get a copy of the toolbar object with access to its config dictionary -DEBUG_TOOLBAR_CONFIG = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) -SQL_WARNING_THRESHOLD = DEBUG_TOOLBAR_CONFIG.get('SQL_WARNING_THRESHOLD', 500) +from debug_toolbar.utils.settings import CONFIG class SQLQueryTriggered(Exception): @@ -98,9 +93,7 @@ class NormalCursorWrapper(object): finally: stop_time = time() duration = (stop_time - start_time) * 1000 - debug_toolbar_config = getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}) - enable_stacktraces = debug_toolbar_config.get('ENABLE_STACKTRACES', True) - if enable_stacktraces: + if CONFIG['ENABLE_STACKTRACES']: stacktrace = tidy_stacktrace(reversed(get_stack())) else: stacktrace = [] @@ -143,7 +136,7 @@ class NormalCursorWrapper(object): 'stacktrace': stacktrace, 'start_time': start_time, 'stop_time': stop_time, - 'is_slow': (duration > SQL_WARNING_THRESHOLD), + 'is_slow': (duration > CONFIG.get('SQL_WARNING_THRESHOLD', 500)), 'is_select': sql.lower().strip().startswith('select'), 'template_info': template_info, } |
