import sys

from django.conf import settings
from django.core.signals import request_started, request_finished, \
    got_request_exception
from django.db.models.signals import class_prepared, pre_init, post_init, \
    pre_save, post_save, pre_delete, post_delete, post_syncdb
from django.dispatch.dispatcher import WEAKREF_TYPES
from django.utils.translation import ugettext_lazy as _

try:
    from django.db.backends.signals import connection_created
except ImportError:
    connection_created = None

from debug_toolbar.panels import DebugPanel

class SignalDebugPanel(DebugPanel):
    name = "Signals"
    template = 'debug_toolbar/panels/signals.html'
    has_content = True

    SIGNALS = {
        'request_started': request_started,
        'request_finished': request_finished,
        'got_request_exception': got_request_exception,
        'connection_created': connection_created,
        'class_prepared': class_prepared,
        'pre_init': pre_init,
        'post_init': post_init,
        'pre_save': pre_save,
        'post_save': post_save,
        'pre_delete': pre_delete,
        'post_delete': post_delete,
        'post_syncdb': post_syncdb,
    }

    def nav_title(self):
        return _("Signals")

    def title(self):
        return _("Signals")

    def url(self):
        return ''

    def signals(self):
        signals = self.SIGNALS.copy()
        if hasattr(settings, 'DEBUG_TOOLBAR_CONFIG'):
            extra_signals = settings.DEBUG_TOOLBAR_CONFIG.get('EXTRA_SIGNALS', [])
        else:
            extra_signals = []
        for signal in extra_signals:
            parts = signal.split('.')
            path = '.'.join(parts[:-1])
            __import__(path)
            signals[parts[-1]] = getattr(sys.modules[path], parts[-1])
        return signals
    signals = property(signals)

    def process_response(self, request, response):
        signals = []
        keys = self.signals.keys()
        keys.sort()
        for name in keys:
            signal = self.signals[name]
            if signal is None:
                continue
            receivers = []
            for (receiverkey, r_senderkey), receiver in signal.receivers:
                if isinstance(receiver, WEAKREF_TYPES):
                    receiver = receiver()
                if receiver is None:
                    continue
                if getattr(receiver, 'im_self', None) is not None:
                    text = "method %s on %s object" % (receiver.__name__, getattr(receiver.im_self, '__class__', type).__name__)
                elif getattr(receiver, 'im_class', None) is not None:
                    text = "method %s on %s" % (receiver.__name__, receiver.im_class.__name__)
                else:
                    text = "function %s" % getattr(receiver, '__name__', str(receiver))
                receivers.append(text)
            signals.append((name, signal, receivers))

        self.record_stats({'signals': signals})

@@ -1213,32 +1213,420 @@
 n _(%22Signals%22)%0A%0A
+    def nav_subtitle(self):%0A        signals = self.get_stats()%5B'signals'%5D%0A        num_receivers = sum(len(s%5B2%5D) for s in signals)%0A        num_signals = len(signals)%0A        return '%25d %25s from %25d %25s' %25 (%0A            num_receivers,%0A            (num_receivers == 1) and 'receiver' or 'receivers',%0A            num_signals,%0A            (num_signals == 1) and 'signal' or 'signals',%0A        )%0A%0A
     def title(se
@@ -2295,120 +2295,77 @@
     
-keys = self.signals.keys()%0A        keys.sort()%0A        for name in keys:%0A            signal = self.signals%5Bname%5D
+for name, signal in sorted(self.signals.items(), key=lambda x: x%5B0%5D):
 %0A   
@@ -2673,32 +2673,178 @@
         continue
+%0A%0A                receiver = getattr(receiver, '__wraps__', receiver)%0A                receiver_name = getattr(receiver, '__name__', str(receiver))
 %0A               
@@ -2927,54 +2927,18 @@
  = %22
-method %25s on %25s object%22 %25 (receiver.__name__, 
+%25s.%25s%22 %25 (
 geta
@@ -2978,32 +2978,47 @@
 , type).__name__
+, receiver_name
 )%0A              
@@ -3105,21 +3105,11 @@
  = %22
-method %25s on 
+%25s.
 %25s%22 
@@ -3120,16 +3120,25 @@
 eceiver.
+im_class.
 __name__
@@ -3147,34 +3147,21 @@
 receiver
-.im_class._
 _name
-__
 )%0A      
@@ -3208,39 +3208,22 @@
  = %22
-function %25s%22 %25 getattr(
+%25s%22 %25 
 receiver
 , '_
@@ -3222,36 +3222,13 @@
 iver
-, '_
 _name
-__', str(receiver))
 %0A   

Improve signals panel