Skip to content

Commit fcb1886

Browse files
authored
Merge pull request #913 from jdufresne/import-string
Use Django's import_string so as to avoid re-implementing it
2 parents a4959c6 + d71a225 commit fcb1886

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

debug_toolbar/panels/signals.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import absolute_import, unicode_literals
22

33
import weakref
4-
from importlib import import_module
54

65
from django.core.signals import (
76
got_request_exception, request_finished, request_started,
@@ -11,6 +10,7 @@
1110
class_prepared, post_delete, post_init, post_migrate, post_save,
1211
pre_delete, pre_init, pre_save,
1312
)
13+
from django.utils.module_loading import import_string
1414
from django.utils.translation import ugettext_lazy as _, ungettext
1515

1616
from debug_toolbar.panels import Panel
@@ -55,16 +55,13 @@ def nav_subtitle(self):
5555
def signals(self):
5656
signals = self.SIGNALS.copy()
5757
for signal in self.toolbar.config['EXTRA_SIGNALS']:
58-
mod_path, signal_name = signal.rsplit('.', 1)
59-
signals_mod = import_module(mod_path)
60-
signals[signal_name] = getattr(signals_mod, signal_name)
58+
signal_name = signal.rsplit('.', 1)[-1]
59+
signals[signal_name] = import_string(signal)
6160
return signals
6261

6362
def generate_stats(self, request, response):
6463
signals = []
6564
for name, signal in sorted(self.signals.items(), key=lambda x: x[0]):
66-
if signal is None:
67-
continue
6865
receivers = []
6966
for receiver in signal.receivers:
7067
receiver = receiver[1]

debug_toolbar/toolbar.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
import uuid
88
from collections import OrderedDict
9-
from importlib import import_module
109

1110
from django.apps import apps
1211
from django.conf.urls import url
1312
from django.core.exceptions import ImproperlyConfigured
1413
from django.template import TemplateSyntaxError
1514
from django.template.loader import render_to_string
15+
from django.utils.module_loading import import_string
1616

1717
from debug_toolbar import settings as dt_settings
1818

@@ -106,27 +106,10 @@ def fetch(cls, store_id):
106106
def get_panel_classes(cls):
107107
if cls._panel_classes is None:
108108
# Load panels in a temporary variable for thread safety.
109-
panel_classes = []
110-
for panel_path in dt_settings.get_panels():
111-
# This logic could be replaced with import_by_path in Django 1.6.
112-
try:
113-
panel_module, panel_classname = panel_path.rsplit('.', 1)
114-
except ValueError:
115-
raise ImproperlyConfigured(
116-
"%s isn't a debug panel module" % panel_path)
117-
try:
118-
mod = import_module(panel_module)
119-
except ImportError as e:
120-
raise ImproperlyConfigured(
121-
'Error importing debug panel %s: "%s"' %
122-
(panel_module, e))
123-
try:
124-
panel_class = getattr(mod, panel_classname)
125-
except AttributeError:
126-
raise ImproperlyConfigured(
127-
'Toolbar Panel module "%s" does not define a "%s" class' %
128-
(panel_module, panel_classname))
129-
panel_classes.append(panel_class)
109+
panel_classes = [
110+
import_string(panel_path)
111+
for panel_path in dt_settings.get_panels()
112+
]
130113
cls._panel_classes = panel_classes
131114
return cls._panel_classes
132115

0 commit comments

Comments
 (0)