1
+ import contextvars
1
2
import datetime
2
3
import json
3
- from threading import local
4
4
from time import time
5
5
6
6
from django .utils .encoding import force_str
13
13
except ImportError :
14
14
PostgresJson = None
15
15
16
+ recording = contextvars .ContextVar ("debug-toolbar-recording" , default = True )
17
+
16
18
17
19
class SQLQueryTriggered (Exception ):
18
20
"""Thrown when template panel triggers a query"""
19
21
20
- pass
21
-
22
-
23
- class ThreadLocalState (local ):
24
- def __init__ (self ):
25
- self .enabled = True
26
-
27
- @property
28
- def Wrapper (self ):
29
- if self .enabled :
30
- return NormalCursorWrapper
31
- return ExceptionCursorWrapper
32
-
33
- def recording (self , v ):
34
- self .enabled = v
35
-
36
-
37
- state = ThreadLocalState ()
38
- recording = state .recording # export function
39
-
40
22
41
23
def wrap_cursor (connection , panel ):
42
24
if not hasattr (connection , "_djdt_cursor" ):
@@ -50,16 +32,22 @@ def cursor(*args, **kwargs):
50
32
# See:
51
33
# https://github.com/jazzband/django-debug-toolbar/pull/615
52
34
# https://github.com/jazzband/django-debug-toolbar/pull/896
53
- return state .Wrapper (
54
- connection ._djdt_cursor (* args , ** kwargs ), connection , panel
55
- )
35
+ if recording .get ():
36
+ wrapper = NormalCursorWrapper
37
+ else :
38
+ wrapper = ExceptionCursorWrapper
39
+ return wrapper (connection ._djdt_cursor (* args , ** kwargs ), connection , panel )
56
40
57
41
def chunked_cursor (* args , ** kwargs ):
58
42
# prevent double wrapping
59
43
# solves https://github.com/jazzband/django-debug-toolbar/issues/1239
60
44
cursor = connection ._djdt_chunked_cursor (* args , ** kwargs )
61
45
if not isinstance (cursor , BaseCursorWrapper ):
62
- return state .Wrapper (cursor , connection , panel )
46
+ if recording .get ():
47
+ wrapper = NormalCursorWrapper
48
+ else :
49
+ wrapper = ExceptionCursorWrapper
50
+ return wrapper (cursor , connection , panel )
63
51
return cursor
64
52
65
53
connection .cursor = cursor
0 commit comments