1
1
"""
2
2
Debug Panel middleware
3
3
"""
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
8
4
import threading
9
5
import time
10
6
7
+ from django .core .urlresolvers import reverse , resolve , Resolver404
8
+ from django .conf import settings
11
9
from debug_panel .cache import cache
10
+ import debug_toolbar .middleware
12
11
13
12
# the urls patterns that concern only the debug_panel application
14
13
import debug_panel .urls
15
14
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
16
26
17
- class DebugPanelMiddleware (DebugToolbarMiddleware ):
27
+
28
+ class DebugPanelMiddleware (debug_toolbar .middleware .DebugToolbarMiddleware ):
18
29
"""
19
30
Middleware to set up Debug Panel on incoming request and render toolbar
20
31
on outgoing response.
21
32
"""
22
33
23
-
24
34
def process_request (self , request ):
25
35
"""
26
36
Try to match the request with an URL from debug_panel application.
@@ -41,42 +51,20 @@ def process_request(self, request):
41
51
42
52
def process_response (self , request , response ):
43
53
"""
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.
50
55
51
56
The data stored in the cache are then reachable from an URL that is appened
52
57
to the HTTP response header under the 'X-debug-data-url' key.
53
58
"""
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
+
82
70
return response
0 commit comments