Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the toolbar loading mechanism to only happen when the middlewar…
…e has been added to the MIDDLEWARE_CLASSES setting.
- Loading branch information
Showing
1 changed file
with
28 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,30 @@ | ||
from django.conf import settings | ||
from django.utils.importlib import import_module | ||
|
||
from debug_toolbar.toolbar.loader import load_panel_classes | ||
from debug_toolbar.middleware import DebugToolbarMiddleware | ||
|
||
loaded = False | ||
|
||
|
||
def is_toolbar(cls): | ||
return (issubclass(cls, DebugToolbarMiddleware) or | ||
DebugToolbarMiddleware in getattr(cls, '__bases__', ())) | ||
|
||
|
||
def iter_toolbar_middlewares(): | ||
global loaded | ||
for middleware_path in settings.MIDDLEWARE_CLASSES: | ||
try: | ||
mod_path, cls_name = middleware_path.rsplit('.', 1) | ||
mod = import_module(mod_path) | ||
middleware_cls = getattr(mod, cls_name) | ||
except (AttributeError, ImportError, ValueError): | ||
continue | ||
if is_toolbar(middleware_cls) and not loaded: | ||
# we have a hit! | ||
loaded = True | ||
yield middleware_cls | ||
|
||
load_panel_classes() | ||
for middleware_cls in iter_toolbar_middlewares(): | ||
load_panel_classes() |