1- from collections import OrderedDict
1+ import json
2+ from collections import OrderedDict , defaultdict
23
4+ from django .core .serializers .json import DjangoJSONEncoder
35from django .utils .module_loading import import_string
46
57from debug_toolbar import settings as dt_settings
68
79
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+
830class BaseStore :
931 config = dt_settings .get_config ().copy ()
1032
@@ -24,9 +46,14 @@ def set(cls, store_id, toolbar):
2446 def delete (cls , store_id ):
2547 raise NotImplementedError
2648
49+ @classmethod
50+ def record_stats (cls , store_id , panel_id , stats ):
51+ raise NotImplementedError
52+
2753
2854class MemoryStore (BaseStore ):
2955 _store = OrderedDict ()
56+ _stats = defaultdict (dict )
3057
3158 @classmethod
3259 def get (cls , store_id ):
@@ -46,5 +73,17 @@ def set(cls, store_id, toolbar):
4673 def delete (cls , store_id ):
4774 del cls ._store [store_id ]
4875
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+
4988
5089store = import_string (dt_settings .get_config ()["TOOLBAR_STORE_CLASS" ])
0 commit comments