Skip to content

Commit d40dd2d

Browse files
committed
works with debug toolbar >= 1.0
1 parent 78843d6 commit d40dd2d

File tree

3 files changed

+31
-49
lines changed

3 files changed

+31
-49
lines changed

debug_panel/middleware.py

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
"""
22
Debug Panel middleware
33
"""
4-
from debug_toolbar.middleware import DebugToolbarMiddleware
5-
from django.core.urlresolvers import reverse, resolve, Resolver404
6-
from django.http import HttpResponseRedirect
7-
from django.shortcuts import render
84
import threading
95
import time
106

7+
from django.core.urlresolvers import reverse, resolve, Resolver404
8+
from django.conf import settings
119
from debug_panel.cache import cache
10+
import debug_toolbar.middleware
1211

1312
# the urls patterns that concern only the debug_panel application
1413
import debug_panel.urls
1514

15+
def show_toolbar(request):
16+
"""
17+
Default function to determine whether to show the toolbar on a given page.
18+
"""
19+
if request.META.get('REMOTE_ADDR', None) not in settings.INTERNAL_IPS:
20+
return False
21+
22+
return bool(settings.DEBUG)
23+
24+
25+
debug_toolbar.middleware.show_toolbar = show_toolbar
1626

17-
class DebugPanelMiddleware(DebugToolbarMiddleware):
27+
28+
class DebugPanelMiddleware(debug_toolbar.middleware.DebugToolbarMiddleware):
1829
"""
1930
Middleware to set up Debug Panel on incoming request and render toolbar
2031
on outgoing response.
2132
"""
2233

23-
2434
def process_request(self, request):
2535
"""
2636
Try to match the request with an URL from debug_panel application.
@@ -41,42 +51,20 @@ def process_request(self, request):
4151

4252
def process_response(self, request, response):
4353
"""
44-
Since there is no hook to intercept and change rendering of the default
45-
debug_toolbar middleware, this is mostly a copy the original debug_toolbar
46-
middleware.
47-
48-
Instead of rendering the debug_toolbar inside the response HTML, it's stored
49-
in the Django cache.
54+
Store the DebugToolbarMiddleware rendered toolbar into a cache store.
5055
5156
The data stored in the cache are then reachable from an URL that is appened
5257
to the HTTP response header under the 'X-debug-data-url' key.
5358
"""
54-
__traceback_hide__ = True
55-
ident = threading.current_thread().ident
56-
toolbar = self.__class__.debug_toolbars.get(ident)
57-
if not toolbar:
58-
return response
59-
if isinstance(response, HttpResponseRedirect):
60-
if not toolbar.config['INTERCEPT_REDIRECTS']:
61-
return response
62-
redirect_to = response.get('Location', None)
63-
if redirect_to:
64-
cookies = response.cookies
65-
response = render(
66-
request,
67-
'debug_toolbar/redirect.html',
68-
{'redirect_to': redirect_to}
69-
)
70-
response.cookies = cookies
71-
72-
for panel in toolbar.panels:
73-
panel.process_response(request, response)
74-
75-
cache_key = "%f" % time.time()
76-
cache.set(cache_key, toolbar.render_toolbar())
77-
78-
response['X-debug-data-url'] = request.build_absolute_uri(
79-
reverse('debug_data', urlconf=debug_panel.urls, kwargs={'cache_key': cache_key}))
80-
81-
del self.__class__.debug_toolbars[ident]
59+
toolbar = self.__class__.debug_toolbars.get(threading.current_thread().ident, None)
60+
61+
response = super(DebugPanelMiddleware, self).process_response(request, response)
62+
63+
if toolbar:
64+
cache_key = "%f" % time.time()
65+
cache.set(cache_key, toolbar.render_toolbar())
66+
67+
response['X-debug-data-url'] = request.build_absolute_uri(
68+
reverse('debug_data', urlconf=debug_panel.urls, kwargs={'cache_key': cache_key}))
69+
8270
return response

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name='django-debug-panel',
6-
version='0.7.2',
6+
version='0.8.0',
77
author=u'Joël Billaud',
88
author_email='jbillaud@gmail.com',
99
packages=['debug_panel'],
@@ -13,6 +13,6 @@
1313
description='django-debug-toolbar in WebKit DevTools. Works fine with background Ajax requests and non-HTML responses',
1414
long_description=open('README.rst').read(),
1515
install_requires=[
16-
"django-debug-toolbar >= 0.9.0"
16+
"django-debug-toolbar >= 1.0"
1717
],
1818
)

tests/settings.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,4 @@
4444
}
4545
}
4646

47-
48-
# Debug Toolbar configuration
49-
50-
DEBUG_TOOLBAR_CONFIG = {
51-
# Django's test client sets wsgi.multiprocess to True inappropriately
52-
'RENDER_PANELS': True,
53-
}
47+
DEBUG_TOOLBAR_PATCH_SETTINGS = True

0 commit comments

Comments
 (0)