1
- from collections import OrderedDict
1
+ import json
2
+ from collections import OrderedDict , defaultdict
2
3
4
+ from django .core .serializers .json import DjangoJSONEncoder
3
5
from django .utils .module_loading import import_string
4
6
5
7
from debug_toolbar import settings as dt_settings
6
8
7
9
10
+ class DebugToolbarJSONEncoder (DjangoJSONEncoder ):
11
+ def default (self , o ):
12
+ try :
13
+ return super ().default (o )
14
+ except TypeError :
15
+ return str (o )
16
+
17
+
18
+ def serialize (data ):
19
+ return json .dumps (data , cls = DebugToolbarJSONEncoder )
20
+
21
+
22
+ def deserialize (data ):
23
+ return json .loads (data )
24
+
25
+
26
+ # Record stats in serialized fashion.
27
+ # Remove use of fetching the toolbar as a whole from the store.
28
+
29
+
8
30
class BaseStore :
9
31
config = dt_settings .get_config ().copy ()
10
32
@@ -24,9 +46,14 @@ def set(cls, store_id, toolbar):
24
46
def delete (cls , store_id ):
25
47
raise NotImplementedError
26
48
49
+ @classmethod
50
+ def record_stats (cls , store_id , panel_id , stats ):
51
+ raise NotImplementedError
52
+
27
53
28
54
class MemoryStore (BaseStore ):
29
55
_store = OrderedDict ()
56
+ _stats = defaultdict (dict )
30
57
31
58
@classmethod
32
59
def get (cls , store_id ):
@@ -46,5 +73,17 @@ def set(cls, store_id, toolbar):
46
73
def delete (cls , store_id ):
47
74
del cls ._store [store_id ]
48
75
76
+ @classmethod
77
+ def save_panel (cls , store_id , panel_id , stats = None ):
78
+ cls ._stats [store_id ][panel_id ] = serialize (stats )
79
+
80
+ @classmethod
81
+ def panel (cls , store_id , panel_id ):
82
+ try :
83
+ data = cls ._stats [store_id ][panel_id ]
84
+ except KeyError :
85
+ data = None
86
+ return {} if data is None else deserialize (data )
87
+
49
88
50
89
store = import_string (dt_settings .get_config ()["TOOLBAR_STORE_CLASS" ])
0 commit comments