diff options
| -rw-r--r-- | debug_toolbar/panels/request_vars.py | 10 | ||||
| -rw-r--r-- | debug_toolbar/panels/sql.py | 2 | ||||
| -rw-r--r-- | debug_toolbar/panels/template.py | 48 | ||||
| -rw-r--r-- | debug_toolbar/tests/__init__.py | 1 | ||||
| -rw-r--r-- | debug_toolbar/utils/__init__.py | 24 | ||||
| -rw-r--r-- | example/example.db | bin | 55296 -> 55296 bytes | |||
| -rw-r--r-- | runtests.py (renamed from debug_toolbar/runtests.py) | 19 | ||||
| -rw-r--r-- | setup.py | 4 | ||||
| -rw-r--r-- | tests/__init__.py (renamed from debug_toolbar/tests/templates/404.html) | 0 | ||||
| -rw-r--r-- | tests/models.py | 0 | ||||
| -rw-r--r-- | tests/templates/404.html | 0 | ||||
| -rw-r--r-- | tests/tests.py (renamed from debug_toolbar/tests/tests.py) | 32 | ||||
| -rw-r--r-- | tests/urls.py (renamed from debug_toolbar/tests/urls.py) | 2 | ||||
| -rw-r--r-- | tests/views.py (renamed from debug_toolbar/tests/views.py) | 0 |
14 files changed, 90 insertions, 52 deletions
diff --git a/debug_toolbar/panels/request_vars.py b/debug_toolbar/panels/request_vars.py index 13e563a..6e7dd03 100644 --- a/debug_toolbar/panels/request_vars.py +++ b/debug_toolbar/panels/request_vars.py @@ -1,6 +1,8 @@ from django.template.loader import render_to_string from django.utils.translation import ugettext_lazy as _ + from debug_toolbar.panels import DebugPanel +from debug_toolbar.utils import get_name_from_obj class RequestVarsDebugPanel(DebugPanel): """ @@ -36,17 +38,15 @@ class RequestVarsDebugPanel(DebugPanel): context = self.context.copy() if self.view_func is not None: - module = self.view_func.__module__ - name = getattr(self.view_func, '__name__', None) or self.view_func.__class__.__name__ - view_func = '%s.%s' % (module, name) + name = get_name_from_obj(self.view_func) else: - view_func = '<no view>' + name = '<no view>' context.update({ 'get': [(k, self.request.GET.getlist(k)) for k in self.request.GET], 'post': [(k, self.request.POST.getlist(k)) for k in self.request.POST], 'cookies': [(k, self.request.COOKIES.get(k)) for k in self.request.COOKIES], - 'view_func': view_func, + 'view_func': name, 'view_args': self.view_args, 'view_kwargs': self.view_kwargs }) diff --git a/debug_toolbar/panels/sql.py b/debug_toolbar/panels/sql.py index e8e053b..841aaac 100644 --- a/debug_toolbar/panels/sql.py +++ b/debug_toolbar/panels/sql.py @@ -194,7 +194,7 @@ class SQLDebugPanel(DebugPanel): stacktrace = [] for frame in query['stacktrace']: params = map(escape, frame[0].rsplit('/', 1) + list(frame[1:])) - stacktrace.append('<span class="path">{0}/</span><span class="file">{1}</span> in <span class="func">{3}</span>(<span class="lineno">{2}</span>)\n <span class="code">{4}</span>'.format(*params)) + stacktrace.append(u'<span class="path">{0}/</span><span class="file">{1}</span> in <span class="func">{3}</span>(<span class="lineno">{2}</span>)\n <span class="code">{4}</span>'.format(*params)) query['stacktrace'] = mark_safe('\n'.join(stacktrace)) i += 1 diff --git a/debug_toolbar/panels/template.py b/debug_toolbar/panels/template.py index 44b8b3e..0715c0a 100644 --- a/debug_toolbar/panels/template.py +++ b/debug_toolbar/panels/template.py @@ -47,6 +47,29 @@ class TemplateDebugPanel(DebugPanel): template_rendered.connect(self._store_template_info) def _store_template_info(self, sender, **kwargs): + context_data = kwargs['context'] + + context_list = [] + for context_layer in context_data.dicts: + if hasattr(context_layer, 'items'): + for key, value in context_layer.items(): + # Replace any request elements - they have a large + # unicode representation and the request data is + # already made available from the Request Vars panel. + if isinstance(value, http.HttpRequest): + context_layer[key] = '<<request>>' + # Replace the debugging sql_queries element. The SQL + # data is already made available from the SQL panel. + elif key == 'sql_queries' and isinstance(value, list): + context_layer[key] = '<<sql_queries>>' + # Replace LANGUAGES, which is available in i18n context processor + elif key == 'LANGUAGES' and isinstance(value, tuple): + context_layer[key] = '<<languages>>' + try: + context_list.append(pformat(context_layer)) + except UnicodeEncodeError: + pass + kwargs['context'] = context_list self.templates.append(kwargs) def nav_title(self): @@ -78,6 +101,8 @@ class TemplateDebugPanel(DebugPanel): # Skip templates that we are generating through the debug toolbar. if template.name and template.name.startswith('debug_toolbar/'): continue + if not hasattr(template, 'origin'): + continue if template.origin and template.origin.name: template.origin_name = template.origin.name else: @@ -85,28 +110,7 @@ class TemplateDebugPanel(DebugPanel): info['template'] = template # Clean up context for better readability if getattr(settings, 'DEBUG_TOOLBAR_CONFIG', {}).get('SHOW_TEMPLATE_CONTEXT', True): - context_data = template_data.get('context', None) - - context_list = [] - for context_layer in context_data.dicts: - if hasattr(context_layer, 'items'): - for key, value in context_layer.items(): - # Replace any request elements - they have a large - # unicode representation and the request data is - # already made available from the Request Vars panel. - if isinstance(value, http.HttpRequest): - context_layer[key] = '<<request>>' - # Replace the debugging sql_queries element. The SQL - # data is already made available from the SQL panel. - elif key == 'sql_queries' and isinstance(value, list): - context_layer[key] = '<<sql_queries>>' - # Replace LANGUAGES, which is available in i18n context processor - elif key == 'LANGUAGES' and isinstance(value, tuple): - context_layer[key] = '<<languages>>' - try: - context_list.append(pformat(context_layer)) - except UnicodeEncodeError: - pass + context_list = template_data.get('context', []) info['context'] = '\n'.join(context_list) template_context.append(info) diff --git a/debug_toolbar/tests/__init__.py b/debug_toolbar/tests/__init__.py deleted file mode 100644 index f853b10..0000000 --- a/debug_toolbar/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from tests import *
\ No newline at end of file diff --git a/debug_toolbar/utils/__init__.py b/debug_toolbar/utils/__init__.py index 2ce38db..c4dc160 100644 --- a/debug_toolbar/utils/__init__.py +++ b/debug_toolbar/utils/__init__.py @@ -47,7 +47,7 @@ def get_template_info(source, context_lines=3): line = 0 upto = 0 source_lines = [] - before = during = after = "" + # before = during = after = "" origin, (start, end) = source template_source = origin.reload() @@ -55,9 +55,9 @@ def get_template_info(source, context_lines=3): for num, next in enumerate(linebreak_iter(template_source)): if start >= upto and end <= next: line = num - before = template_source[upto:start] - during = template_source[start:end] - after = template_source[end:next] + # before = template_source[upto:start] + # during = template_source[start:end] + # after = template_source[end:next] source_lines.append((num, template_source[upto:next])) upto = next @@ -75,4 +75,18 @@ def get_template_info(source, context_lines=3): return { 'name': origin.name, 'context': context, - }
\ No newline at end of file + } + +def get_name_from_obj(obj): + if hasattr(obj, '__name__'): + name = obj.__name__ + elif hasattr(obj, '__class__') and hasattr(obj.__class__, '__name__'): + name = obj.__class__.__name__ + else: + name = '<unknown>' + + if hasattr(obj, '__module__'): + module = obj.__module__ + name = '%s.%s' % (module, name) + + return name
\ No newline at end of file diff --git a/example/example.db b/example/example.db Binary files differindex 1339622..e0048d8 100644 --- a/example/example.db +++ b/example/example.db diff --git a/debug_toolbar/runtests.py b/runtests.py index f16882a..b8abc5d 100644 --- a/debug_toolbar/runtests.py +++ b/runtests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import sys from os.path import dirname, abspath +from optparse import OptionParser from django.conf import settings @@ -19,29 +20,31 @@ if not settings.configured: 'debug_toolbar', - 'debug_toolbar.tests', + 'tests', ], ROOT_URLCONF='', DEBUG=False, SITE_ID=1, ) - import djcelery - djcelery.setup_loader() from django.test.simple import run_tests -def runtests(*test_args): +def runtests(*test_args, **kwargs): if 'south' in settings.INSTALLED_APPS: from south.management.commands import patch_for_test_db_setup patch_for_test_db_setup() if not test_args: - test_args = ['debug_toolbar'] + test_args = ['tests'] parent = dirname(abspath(__file__)) sys.path.insert(0, parent) - failures = run_tests(test_args, verbosity=1, interactive=True) + failures = run_tests(test_args, verbosity=kwargs.get('verbosity', 1), interactive=kwargs.get('interactive', False), failfast=kwargs.get('failfast')) sys.exit(failures) - if __name__ == '__main__': - runtests(*sys.argv[1:])
\ No newline at end of file + parser = OptionParser() + parser.add_option('--failfast', action='store_true', default=False, dest='failfast') + + (options, args) = parser.parse_args() + + runtests(failfast=options.failfast, *args)
\ No newline at end of file @@ -11,12 +11,12 @@ setup( url='https://github.com/django-debug-toolbar/django-debug-toolbar', download_url='https://github.com/django-debug-toolbar/django-debug-toolbar/downloads', license='BSD', - packages=find_packages(exclude=['ez_setup']), + packages=find_packages(exclude=('ez_setup', 'tests', 'example')), tests_require=[ 'django>=1.1,<1.4', 'dingus', ], - test_suite='debug_toolbar.runtests.runtests', + test_suite='runtests.runtests', include_package_data=True, zip_safe=False, # because we're including media that Django needs classifiers=[ diff --git a/debug_toolbar/tests/templates/404.html b/tests/__init__.py index e69de29..e69de29 100644 --- a/debug_toolbar/tests/templates/404.html +++ b/tests/__init__.py diff --git a/tests/models.py b/tests/models.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/models.py diff --git a/tests/templates/404.html b/tests/templates/404.html new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/templates/404.html diff --git a/debug_toolbar/tests/tests.py b/tests/tests.py index 9ff332e..4936775 100644 --- a/debug_toolbar/tests/tests.py +++ b/tests/tests.py @@ -2,10 +2,12 @@ from debug_toolbar.middleware import DebugToolbarMiddleware from debug_toolbar.panels.sql import SQLDebugPanel from debug_toolbar.panels.request_vars import RequestVarsDebugPanel from debug_toolbar.toolbar.loader import DebugToolbar +from debug_toolbar.utils import get_name_from_obj from debug_toolbar.utils.tracking import pre_dispatch, post_dispatch, callbacks from django.conf import settings from django.contrib.auth.models import User +from django.http import HttpResponse from django.test import TestCase from dingus import Dingus @@ -44,7 +46,7 @@ class BaseTestCase(TestCase): self.toolbar = toolbar class DebugToolbarTestCase(BaseTestCase): - urls = 'debug_toolbar.tests.urls' + urls = 'tests.urls' def test_middleware(self): resp = self.client.get('/execute_sql/') @@ -87,7 +89,7 @@ class DebugToolbarTestCase(BaseTestCase): def test_request_urlconf_string(self): request = self.request - request.urlconf = 'debug_toolbar.tests.urls' + request.urlconf = 'tests.urls' request.META = {'REMOTE_ADDR': '127.0.0.1'} middleware = DebugToolbarMiddleware() @@ -98,12 +100,12 @@ class DebugToolbarTestCase(BaseTestCase): self.assertTrue(hasattr(request.urlconf.urlpatterns[0], '_callback_str')) self.assertEquals(request.urlconf.urlpatterns[0]._callback_str, 'debug_toolbar.views.debug_media') - self.assertEquals(request.urlconf.urlpatterns[-1].urlconf_name.__name__, 'debug_toolbar.tests.urls') + self.assertEquals(request.urlconf.urlpatterns[-1].urlconf_name.__name__, 'tests.urls') def test_request_urlconf_string_per_request(self): request = self.request - request.urlconf = 'debug_toolbar.tests.urls' + request.urlconf = 'tests.urls' request.META = {'REMOTE_ADDR': '127.0.0.1'} middleware = DebugToolbarMiddleware() @@ -121,7 +123,7 @@ class DebugToolbarTestCase(BaseTestCase): def test_request_urlconf_module(self): request = self.request - request.urlconf = __import__('debug_toolbar.tests.urls').tests.urls + request.urlconf = __import__('tests.urls').urls request.META = {'REMOTE_ADDR': '127.0.0.1'} middleware = DebugToolbarMiddleware() @@ -132,7 +134,7 @@ class DebugToolbarTestCase(BaseTestCase): self.assertTrue(hasattr(request.urlconf.urlpatterns[0], '_callback_str')) self.assertEquals(request.urlconf.urlpatterns[0]._callback_str, 'debug_toolbar.views.debug_media') - self.assertEquals(request.urlconf.urlpatterns[-1].urlconf_name.__name__, 'debug_toolbar.tests.urls') + self.assertEquals(request.urlconf.urlpatterns[-1].urlconf_name.__name__, 'tests.urls') def test_with_process_view(self): request = self.request @@ -145,7 +147,7 @@ class DebugToolbarTestCase(BaseTestCase): panel.process_request(request) panel.process_view(request, _test_view, [], {}) content = panel.content() - self.assertIn('debug_toolbar.tests.tests._test_view', content) + self.assertIn('tests.tests._test_view', content) def test_without_process_view(self): request = self.request @@ -156,6 +158,22 @@ class DebugToolbarTestCase(BaseTestCase): content = panel.content() self.assertIn('<no view>', content) +class DebugToolbarNameFromObjectTest(BaseTestCase): + def test_func(self): + def x(): + return 1 + res = get_name_from_obj(x) + self.assertEquals(res, 'tests.tests.x') + + def test_lambda(self): + res = get_name_from_obj(lambda:1) + self.assertEquals(res, 'tests.tests.<lambda>') + + def test_class(self): + class A: pass + res = get_name_from_obj(A) + self.assertEquals(res, 'tests.tests.A') + class SQLPanelTestCase(BaseTestCase): def test_recording(self): panel = self.toolbar.get_panel(SQLDebugPanel) diff --git a/debug_toolbar/tests/urls.py b/tests/urls.py index 7c99b03..d1cb07f 100644 --- a/debug_toolbar/tests/urls.py +++ b/tests/urls.py @@ -10,5 +10,5 @@ from django.contrib import admin admin.autodiscover() urlpatterns = patterns('', - url(r'^execute_sql/$', 'debug_toolbar.tests.views.execute_sql'), + url(r'^execute_sql/$', 'tests.views.execute_sql'), ) diff --git a/debug_toolbar/tests/views.py b/tests/views.py index f989dcd..f989dcd 100644 --- a/debug_toolbar/tests/views.py +++ b/tests/views.py |
