10
10
from django .conf import settings
11
11
from django .utils import six
12
12
from django .utils .encoding import force_text
13
+ from django .utils .functional import cached_property
13
14
from django .utils .module_loading import import_string
14
15
15
16
from debug_toolbar import settings as dt_settings
16
17
from debug_toolbar .toolbar import DebugToolbar
17
18
19
+ try :
20
+ from django .utils .deprecation import MiddlewareMixin
21
+ except ImportError : # Django < 1.10
22
+ # Works perfectly for everyone using MIDDLEWARE_CLASSES
23
+ MiddlewareMixin = object
24
+
25
+
18
26
_HTML_TYPES = ('text/html' , 'application/xhtml+xml' )
19
27
20
28
@@ -31,21 +39,22 @@ def show_toolbar(request):
31
39
return bool (settings .DEBUG )
32
40
33
41
34
- class DebugToolbarMiddleware (object ):
42
+ class DebugToolbarMiddleware (MiddlewareMixin ):
35
43
"""
36
44
Middleware to set up Debug Toolbar on incoming request and render toolbar
37
45
on outgoing response.
38
46
"""
39
47
debug_toolbars = {}
40
48
41
- def __init__ (self ):
49
+ @cached_property
50
+ def show_toolbar (self ):
42
51
# If SHOW_TOOLBAR_CALLBACK is a string, which is the recommended
43
52
# setup, resolve it to the corresponding callable.
44
53
func_or_path = dt_settings .get_config ()['SHOW_TOOLBAR_CALLBACK' ]
45
54
if isinstance (func_or_path , six .string_types ):
46
- self . show_toolbar = import_string (func_or_path )
55
+ return import_string (func_or_path )
47
56
else :
48
- self . show_toolbar = func_or_path
57
+ return func_or_path
49
58
50
59
def process_request (self , request ):
51
60
# Decide whether the toolbar is active for this request.
0 commit comments