@@ -39,27 +39,35 @@ class DebugToolbarMiddleware(object):
39
39
debug_toolbars = {}
40
40
41
41
def process_request (self , request ):
42
+ # Decide whether the toolbar is active for this request.
42
43
func_path = dt_settings .CONFIG ['SHOW_TOOLBAR_CALLBACK' ]
43
44
# Replace this with import_by_path in Django >= 1.6.
44
45
mod_path , func_name = func_path .rsplit ('.' , 1 )
45
46
show_toolbar = getattr (import_module (mod_path ), func_name )
46
47
if not show_toolbar (request ):
47
48
return
48
- response = None
49
+
49
50
toolbar = DebugToolbar (request )
51
+ self .__class__ .debug_toolbars [threading .current_thread ().ident ] = toolbar
52
+
53
+ # Activate instrumentation ie. monkey-patch.
50
54
for panel in toolbar .enabled_panels :
51
55
panel .enable_instrumentation ()
56
+
57
+ # Run process_request methods of panels like Django middleware.
58
+ response = None
52
59
for panel in toolbar .enabled_panels :
53
60
response = panel .process_request (request )
54
61
if response :
55
62
break
56
- self .__class__ .debug_toolbars [threading .current_thread ().ident ] = toolbar
57
63
return response
58
64
59
65
def process_view (self , request , view_func , view_args , view_kwargs ):
60
66
toolbar = self .__class__ .debug_toolbars .get (threading .current_thread ().ident )
61
67
if not toolbar :
62
68
return
69
+
70
+ # Run process_view methods of panels like Django middleware.
63
71
response = None
64
72
for panel in toolbar .enabled_panels :
65
73
response = panel .process_view (request , view_func , view_args , view_kwargs )
@@ -69,26 +77,42 @@ def process_view(self, request, view_func, view_args, view_kwargs):
69
77
70
78
def process_response (self , request , response ):
71
79
toolbar = self .__class__ .debug_toolbars .pop (threading .current_thread ().ident , None )
72
- if not toolbar or getattr ( response , 'streaming' , False ) :
80
+ if not toolbar :
73
81
return response
82
+
83
+ # Run process_response methods of panels like Django middleware.
74
84
for panel in reversed (toolbar .enabled_panels ):
75
85
new_response = panel .process_response (request , response )
76
86
if new_response :
77
87
response = new_response
88
+
89
+ # Deactivate instrumentation ie. monkey-unpatch. This must run
90
+ # regardless of the response. Keep 'return' clauses below.
91
+ # (NB: Django's model for middleware doesn't guarantee anything.)
78
92
for panel in reversed (toolbar .enabled_panels ):
79
93
panel .disable_instrumentation ()
94
+
95
+ # Check for responses where the toolbar can't be inserted.
96
+ content_encoding = response .get ('Content-Encoding' , '' )
97
+ content_type = response .get ('Content-Type' , '' ).split (';' )[0 ]
98
+ if any ((getattr (response , 'streaming' , False ),
99
+ 'gzip' in content_encoding ,
100
+ content_type not in _HTML_TYPES )):
101
+ return response
102
+
103
+ # Collapse the toolbar by default if SHOW_COLLAPSED is set.
80
104
if toolbar .config ['SHOW_COLLAPSED' ] and 'djdt' not in request .COOKIES :
81
105
response .set_cookie ('djdt' , 'hide' , 864000 )
82
- if ( 'gzip' not in response . get ( 'Content-Encoding' , '' ) and
83
- response .get ( 'Content-Type' , '' ). split ( ';' )[ 0 ] in _HTML_TYPES ):
84
- content = force_text (response .content , encoding = settings .DEFAULT_CHARSET )
85
- try :
86
- insert_at = content .lower ().rindex (dt_settings .CONFIG ['INSERT_BEFORE' ].lower ())
87
- except ValueError :
88
- pass
89
- else :
90
- toolbar_content = toolbar .render_toolbar ()
91
- response .content = content [:insert_at ] + toolbar_content + content [insert_at :]
106
+
107
+ # Insert the toolbar in the response.
108
+ content = force_text (response .content , encoding = settings .DEFAULT_CHARSET )
109
+ try :
110
+ insert_at = content .lower ().rindex (dt_settings .CONFIG ['INSERT_BEFORE' ].lower ())
111
+ except ValueError :
112
+ pass
113
+ else :
114
+ toolbar_content = toolbar .render_toolbar ()
115
+ response .content = content [:insert_at ] + toolbar_content + content [insert_at :]
92
116
if response .get ('Content-Length' , None ):
93
117
response ['Content-Length' ] = len (response .content )
94
118
return response
0 commit comments