From 114ebb6ae8933ba98da75d2ed088438d80216eea Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 4 Oct 2008 19:01:41 -0400 Subject: [PATCH 1/8] changed stuff about the sql formatter, it still looks weird, not sure why --- debug_toolbar/panels/sql.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index 22d65a75f..dd36eb85c 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -68,12 +68,12 @@ def content(self): return render_to_string('debug_toolbar/panels/sql.html', context) def reformat_sql(sql): - sql = sql.replace('`,`', '`, `') + sql = sql.replace(',', ', ') sql = sql.replace('SELECT ', 'SELECT\n\t') - sql = sql.replace('` FROM ', '`\nFROM\n\t') - sql = sql.replace('` WHERE ', '`\nWHERE\n\t') - sql = sql.replace('` INNER JOIN ', '`\nINNER JOIN\n\t') - sql = sql.replace('` OUTER JOIN ', '`\nOUTER JOIN\n\t') + sql = sql.replace(' FROM ', '\nFROM\n\t') + sql = sql.replace(' WHERE ', '\nWHERE\n\t') + sql = sql.replace(' INNER JOIN ', '\nINNER JOIN\n\t') + sql = sql.replace(' OUTER JOIN ', '\nOUTER JOIN\n\t') sql = sql.replace(' ORDER BY ', '\nORDER BY\n\t') # Use Pygments to highlight SQL if it's available try: From e545bd348fbed7d547a55c42db62894e300eb20a Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 5 Oct 2008 17:52:09 -0400 Subject: [PATCH 2/8] fix for gis I think --- debug_toolbar/panels/sql.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index dd36eb85c..6b3f918e2 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -5,6 +5,7 @@ from django.db.backends import util from django.template.loader import render_to_string from django.utils import simplejson +from django.utils.encoding import force_unicode from django.utils.hashcompat import sha_constructor class DatabaseStatTracker(util.CursorDebugWrapper): @@ -28,7 +29,7 @@ def execute(self, sql, params=()): 'sql': self.db.ops.last_executed_query(self.cursor, sql, params), 'time': stop - start, 'raw_sql': sql, - 'params': _params, + 'params': _params and [force_unicode(x) for x in _params] or _params, 'hash': sha_constructor(settings.SECRET_KEY + sql + _params).hexdigest(), }) util.CursorDebugWrapper = DatabaseStatTracker From 04e1d2e242aa752c63c63066a38064438f9d2e97 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 5 Oct 2008 18:02:17 -0400 Subject: [PATCH 3/8] better fix --- debug_toolbar/panels/sql.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index 6b3f918e2..7584bd745 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -21,7 +21,7 @@ def execute(self, sql, params=()): stop = time.time() _params = None try: - _params = simplejson.dumps(params) + _params = simplejson.dumps([force_unicode(x) for x in params]) except TypeError: pass # object not JSON serializable # We keep `sql` to maintain backwards compatibility @@ -29,7 +29,7 @@ def execute(self, sql, params=()): 'sql': self.db.ops.last_executed_query(self.cursor, sql, params), 'time': stop - start, 'raw_sql': sql, - 'params': _params and [force_unicode(x) for x in _params] or _params, + 'params': _params, 'hash': sha_constructor(settings.SECRET_KEY + sql + _params).hexdigest(), }) util.CursorDebugWrapper = DatabaseStatTracker From 0368d75afbe578951bcded286a3122c5302e3e4d Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 6 Oct 2008 00:29:33 -0400 Subject: [PATCH 4/8] warn the user if they have the middleware in the wrong order --- debug_toolbar/middleware.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index 9dd995494..11ff4dd57 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -44,6 +44,11 @@ def show_toolbar(self, request): return True def process_request(self, request): + if not hasattr(request, 'user'): + import warnings + warnings.warn("You should place the debug_toolbar middleware after \ + the AuthenticationMiddleware, if you aren't using django's auth\ + app you can safely ignore this message.") if self.override_url: debug_toolbar.urls.urlpatterns += self.original_pattern self.override_url = False From ea00f59e286edb8d2376a1d00175ef77ebfa89de Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 12 Jun 2009 11:14:37 -0500 Subject: [PATCH 5/8] only conditionally include the Django 1.1 signal for backwards compatibility --- debug_toolbar/panels/signals.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/debug_toolbar/panels/signals.py b/debug_toolbar/panels/signals.py index 0a75282ae..7fe382e6e 100644 --- a/debug_toolbar/panels/signals.py +++ b/debug_toolbar/panels/signals.py @@ -3,12 +3,16 @@ from django.conf import settings from django.core.signals import request_started, request_finished, \ got_request_exception -from django.db.backends.signals import connection_created from django.db.models.signals import class_prepared, pre_init, post_init, \ pre_save, post_save, pre_delete, post_delete, post_syncdb from django.dispatch.dispatcher import WEAKREF_TYPES from django.template.loader import render_to_string +try: + from django.db.backends.signals import connection_created +except ImportError: + connection_created = None + from debug_toolbar.panels import DebugPanel class SignalDebugPanel(DebugPanel): @@ -56,6 +60,8 @@ def content(self): keys.sort() for name in keys: signal = self.signals[name] + if signal is None: + continue receivers = [] for (receiverkey, r_senderkey), receiver in signal.receivers: if isinstance(receiver, WEAKREF_TYPES): From 52dbdd5031b1d025b6553f60006a712b759ca0c5 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 16 Jun 2009 14:37:44 -0500 Subject: [PATCH 6/8] provide an option to see stuff indjango in the sql stack trace --- README.rst | 18 +++++++++++------- debug_toolbar/panels/sql.py | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index c80e005a2..60aa8d017 100644 --- a/README.rst +++ b/README.rst @@ -3,7 +3,7 @@ Django Debug Toolbar ==================== The Django Debug Toolbar is a configurable set of panels that display various -debug information about the current request/response and when clicked, display +debug information about the current request/response and when clicked, display more details about the panel's content. Currently, the following panels have been written and are working: @@ -32,8 +32,8 @@ Installation Tying into middleware allows each panel to be instantiated on request and rendering to happen on response. - The order of MIDDLEWARE_CLASSES is important: the Debug Toolbar middleware - must come after any other middleware that encodes the response's content + The order of MIDDLEWARE_CLASSES is important: the Debug Toolbar middleware + must come after any other middleware that encodes the response's content (such as GZipMiddleware). Note: The debug toolbar will only display itself if the mimetype of the @@ -60,9 +60,9 @@ Configuration The debug toolbar has two settings that can be set in `settings.py`: -#. Optional: Add a tuple called `DEBUG_TOOLBAR_PANELS` to your ``settings.py`` - file that specifies the full Python path to the panel that you want included - in the Toolbar. This setting looks very much like the `MIDDLEWARE_CLASSES` +#. Optional: Add a tuple called `DEBUG_TOOLBAR_PANELS` to your ``settings.py`` + file that specifies the full Python path to the panel that you want included + in the Toolbar. This setting looks very much like the `MIDDLEWARE_CLASSES` setting. For example:: DEBUG_TOOLBAR_PANELS = ( @@ -101,15 +101,19 @@ The debug toolbar has two settings that can be set in `settings.py`: * `EXTRA_SIGNALS`: An array of custom signals that might be in your project, defined as the python path to the signal. + * `HIDE_DJANGO_SQL`: If set to True (the default) then code in Django itself + won't be show in SQL stacktraces. + Example configuration:: def custom_show_toolbar(request): return True # Always show toolbar, for example purposes only. - + DEBUG_TOOLBAR_CONFIG = { 'INTERCEPT_REDIRECTS': False, 'SHOW_TOOLBAR_CALLBACK': custom_show_toolbar, 'EXTRA_SIGNALS': ['myproject.signals.MySignal'], + 'HIDE_DJANGO_SQL': False, } TODOs and BUGS diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index d3ac7f308..c8d50d9c9 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -2,6 +2,7 @@ import SocketServer import time import traceback + import django from django.conf import settings from django.db import connection @@ -10,6 +11,7 @@ from django.utils import simplejson from django.utils.encoding import force_unicode from django.utils.hashcompat import sha_constructor + from debug_toolbar.panels import DebugPanel # Figure out some paths @@ -26,7 +28,7 @@ def tidy_stacktrace(strace): trace = [] for s in strace[:-1]: s_path = os.path.realpath(s[0]) - if django_path in s_path and not 'django/contrib' in s_path: + if getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}).get('HIDE_DJANGO_SQL', True) and django_path in s_path and not 'django/contrib' in s_path: continue if socketserver_path in s_path: continue From d42b562aef1964e476d1fd559463d4c4196a971c Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 10 Aug 2009 11:52:01 -0500 Subject: [PATCH 7/8] Show the number of rendered templates in the toolbar. --- debug_toolbar/panels/template.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/debug_toolbar/panels/template.py b/debug_toolbar/panels/template.py index 7dc7b06cb..911952faa 100644 --- a/debug_toolbar/panels/template.py +++ b/debug_toolbar/panels/template.py @@ -36,13 +36,15 @@ class TemplateDebugPanel(DebugPanel): def __init__(self): self.templates = [] - template_rendered.connect(self._storeTemplateInfo) + template_rendered.connect(self._store_template_info) - def _storeTemplateInfo(self, sender, **kwargs): + def _store_template_info(self, sender, **kwargs): self.templates.append(kwargs) def title(self): - return 'Templates' + num_templates = len([t for t in self.templates + if not t['template'].name.startswith('debug_toolbar/')]) + return 'Templates (%s rendered)' % num_templates def url(self): return '' From 89a0b38316e1822bf22a4f013a6bb12f9db585b8 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 11 Aug 2009 13:33:03 -0500 Subject: [PATCH 8/8] allow for handling the pathological case of an insanely large template context --- README.rst | 5 +++++ debug_toolbar/panels/template.py | 19 ++++++++++--------- .../debug_toolbar/panels/templates.html | 10 ++++++---- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/README.rst b/README.rst index 60aa8d017..79b224a95 100644 --- a/README.rst +++ b/README.rst @@ -104,6 +104,11 @@ The debug toolbar has two settings that can be set in `settings.py`: * `HIDE_DJANGO_SQL`: If set to True (the default) then code in Django itself won't be show in SQL stacktraces. + * `SHOW_TEMPLATE_CONTEXT`: If set to True (the default) then a template's + context will be included with it in the Template debug panel. Turning this + off is useful when you have large template contexts, or you have template + contexts with lazy datastructures that you don't want to be evaluated. + Example configuration:: def custom_show_toolbar(request): diff --git a/debug_toolbar/panels/template.py b/debug_toolbar/panels/template.py index ad6dd0449..e99b9c582 100644 --- a/debug_toolbar/panels/template.py +++ b/debug_toolbar/panels/template.py @@ -73,15 +73,16 @@ def content(self): t.origin_name = 'No origin' info['template'] = t # Clean up context for better readability - c = d.get('context', None) - - d_list = [] - for _d in c.dicts: - try: - d_list.append(pformat(d)) - except UnicodeEncodeError: - pass - info['context'] = '\n'.join(d_list) + if getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}).get('SHOW_TEMPLATE_CONTEXT', True): + c = d.get('context', None) + + d_list = [] + for _d in c.dicts: + try: + d_list.append(pformat(d)) + except UnicodeEncodeError: + pass + info['context'] = '\n'.join(d_list) template_context.append(info) context = { 'templates': template_context, diff --git a/debug_toolbar/templates/debug_toolbar/panels/templates.html b/debug_toolbar/templates/debug_toolbar/panels/templates.html index 6c918b12c..e54c95135 100644 --- a/debug_toolbar/templates/debug_toolbar/panels/templates.html +++ b/debug_toolbar/templates/debug_toolbar/panels/templates.html @@ -14,10 +14,12 @@

Template{{ templates|length|pluralize }}

{% for template in templates %}
{{ template.template.name|addslashes }}
{{ template.template.origin_name|addslashes }}
-
- - -
+ {% if template.context %} +
+ + +
+ {% endif %} {% endfor %} {% else %}