Skip to content

Commit b431d64

Browse files
committed
Moved view function name extraction into debug_toolbar.utils.get_name_from_obj
1 parent 7504141 commit b431d64

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

debug_toolbar/panels/request_vars.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.template.loader import render_to_string
22
from django.utils.translation import ugettext_lazy as _
3+
34
from debug_toolbar.panels import DebugPanel
5+
from debug_toolbar.utils import get_name_from_obj
46

57
class RequestVarsDebugPanel(DebugPanel):
68
"""
@@ -36,17 +38,15 @@ def content(self):
3638
context = self.context.copy()
3739

3840
if self.view_func is not None:
39-
module = self.view_func.__module__
40-
name = getattr(self.view_func, '__name__', None) or getattr(self.view_func.__class__,'__name__','<unknown>')
41-
view_func = '%s.%s' % (module, name)
41+
name = get_name_from_obj(self.view_func)
4242
else:
43-
view_func = '<no view>'
43+
name = '<no view>'
4444

4545
context.update({
4646
'get': [(k, self.request.GET.getlist(k)) for k in self.request.GET],
4747
'post': [(k, self.request.POST.getlist(k)) for k in self.request.POST],
4848
'cookies': [(k, self.request.COOKIES.get(k)) for k in self.request.COOKIES],
49-
'view_func': view_func,
49+
'view_func': name,
5050
'view_args': self.view_args,
5151
'view_kwargs': self.view_kwargs
5252
})

debug_toolbar/utils/__init__.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ def get_template_info(source, context_lines=3):
4747
line = 0
4848
upto = 0
4949
source_lines = []
50-
before = during = after = ""
50+
# before = during = after = ""
5151

5252
origin, (start, end) = source
5353
template_source = origin.reload()
5454

5555
for num, next in enumerate(linebreak_iter(template_source)):
5656
if start >= upto and end <= next:
5757
line = num
58-
before = template_source[upto:start]
59-
during = template_source[start:end]
60-
after = template_source[end:next]
58+
# before = template_source[upto:start]
59+
# during = template_source[start:end]
60+
# after = template_source[end:next]
6161
source_lines.append((num, template_source[upto:next]))
6262
upto = next
6363

@@ -75,4 +75,18 @@ def get_template_info(source, context_lines=3):
7575
return {
7676
'name': origin.name,
7777
'context': context,
78-
}
78+
}
79+
80+
def get_name_from_obj(obj):
81+
if hasattr(obj, '__name__'):
82+
name = obj.__name__
83+
elif hasattr(obj, '__class__') and hasattr(obj.__class__, '__name__'):
84+
name = obj.__class__.__name__
85+
else:
86+
name = '<unknown>'
87+
88+
if hasattr(obj, '__module__'):
89+
module = obj.__module__
90+
name = '%s.%s' % (module, name)
91+
92+
return name

tests/tests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
from debug_toolbar.panels.sql import SQLDebugPanel
33
from debug_toolbar.panels.request_vars import RequestVarsDebugPanel
44
from debug_toolbar.toolbar.loader import DebugToolbar
5+
from debug_toolbar.utils import get_name_from_obj
56
from debug_toolbar.utils.tracking import pre_dispatch, post_dispatch, callbacks
67

78
from django.conf import settings
89
from django.contrib.auth.models import User
10+
from django.http import HttpResponse
911
from django.test import TestCase
1012

1113
from dingus import Dingus
@@ -145,7 +147,7 @@ def _test_view(request):
145147
panel.process_request(request)
146148
panel.process_view(request, _test_view, [], {})
147149
content = panel.content()
148-
self.assertIn('debug_toolbar.tests.tests._test_view', content)
150+
self.assertIn('tests.tests._test_view', content)
149151

150152
def test_without_process_view(self):
151153
request = self.request
@@ -156,6 +158,22 @@ def test_without_process_view(self):
156158
content = panel.content()
157159
self.assertIn('&lt;no view&gt;', content)
158160

161+
class DebugToolbarNameFromObjectTest(BaseTestCase):
162+
def test_func(self):
163+
def x():
164+
return 1
165+
res = get_name_from_obj(x)
166+
self.assertEquals(res, 'tests.tests.x')
167+
168+
def test_lambda(self):
169+
res = get_name_from_obj(lambda:1)
170+
self.assertEquals(res, 'tests.tests.<lambda>')
171+
172+
def test_class(self):
173+
class A: pass
174+
res = get_name_from_obj(A)
175+
self.assertEquals(res, 'tests.tests.A')
176+
159177
class SQLPanelTestCase(BaseTestCase):
160178
def test_recording(self):
161179
panel = self.toolbar.get_panel(SQLDebugPanel)

0 commit comments

Comments
 (0)