2013-11-05 12:11:51 +08:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2013-12-24 19:25:17 +08:00
|
|
|
from django.apps import apps
|
2008-08-06 23:32:46 +08:00
|
|
|
from django.dispatch import Signal
|
2013-11-05 12:11:51 +08:00
|
|
|
from django.utils import six
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-08-06 23:32:46 +08:00
|
|
|
class_prepared = Signal(providing_args=["class"])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
|
2013-11-05 12:11:51 +08:00
|
|
|
class ModelSignal(Signal):
|
|
|
|
"""
|
|
|
|
Signal subclass that allows the sender to be lazily specified as a string
|
|
|
|
of the `app_label.ModelName` form.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ModelSignal, self).__init__(*args, **kwargs)
|
|
|
|
self.unresolved_references = defaultdict(list)
|
|
|
|
class_prepared.connect(self._resolve_references)
|
|
|
|
|
|
|
|
def _resolve_references(self, sender, **kwargs):
|
|
|
|
opts = sender._meta
|
|
|
|
reference = (opts.app_label, opts.object_name)
|
|
|
|
try:
|
|
|
|
receivers = self.unresolved_references.pop(reference)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
for receiver, weak, dispatch_uid in receivers:
|
|
|
|
super(ModelSignal, self).connect(
|
|
|
|
receiver, sender=sender, weak=weak, dispatch_uid=dispatch_uid
|
|
|
|
)
|
|
|
|
|
|
|
|
def connect(self, receiver, sender=None, weak=True, dispatch_uid=None):
|
|
|
|
if isinstance(sender, six.string_types):
|
|
|
|
try:
|
|
|
|
app_label, object_name = sender.split('.')
|
|
|
|
except ValueError:
|
|
|
|
raise ValueError(
|
|
|
|
"Specified sender must either be a model or a "
|
|
|
|
"model name of the 'app_label.ModelName' form."
|
|
|
|
)
|
2013-12-24 19:25:17 +08:00
|
|
|
sender = apps.get_model(app_label, object_name, only_installed=False)
|
2013-11-05 12:11:51 +08:00
|
|
|
if sender is None:
|
|
|
|
reference = (app_label, object_name)
|
|
|
|
self.unresolved_references[reference].append(
|
|
|
|
(receiver, weak, dispatch_uid)
|
|
|
|
)
|
|
|
|
return
|
|
|
|
super(ModelSignal, self).connect(
|
|
|
|
receiver, sender=sender, weak=weak, dispatch_uid=dispatch_uid
|
|
|
|
)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2013-11-05 12:11:51 +08:00
|
|
|
pre_init = ModelSignal(providing_args=["instance", "args", "kwargs"], use_caching=True)
|
|
|
|
post_init = ModelSignal(providing_args=["instance"], use_caching=True)
|
|
|
|
|
|
|
|
pre_save = ModelSignal(providing_args=["instance", "raw", "using", "update_fields"],
|
|
|
|
use_caching=True)
|
|
|
|
post_save = ModelSignal(providing_args=["instance", "raw", "created", "using", "update_fields"], use_caching=True)
|
|
|
|
|
|
|
|
pre_delete = ModelSignal(providing_args=["instance", "using"], use_caching=True)
|
|
|
|
post_delete = ModelSignal(providing_args=["instance", "using"], use_caching=True)
|
|
|
|
|
|
|
|
m2m_changed = ModelSignal(providing_args=["action", "instance", "reverse", "model", "pk_set", "using"], use_caching=True)
|
2008-08-06 23:32:46 +08:00
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
pre_migrate = Signal(providing_args=["app", "create_models", "verbosity", "interactive", "db"])
|
|
|
|
pre_syncdb = pre_migrate
|
2013-08-20 11:14:21 +08:00
|
|
|
post_migrate = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive", "db"])
|
2013-07-30 18:52:52 +08:00
|
|
|
post_syncdb = post_migrate
|