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)
|
2013-12-28 21:24:14 +08:00
|
|
|
self.unresolved_references = {}
|
2013-11-05 12:11:51 +08:00
|
|
|
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:
|
2013-12-28 21:24:14 +08:00
|
|
|
app_label, model_name = sender.split('.')
|
2013-11-05 12:11:51 +08:00
|
|
|
except ValueError:
|
|
|
|
raise ValueError(
|
|
|
|
"Specified sender must either be a model or a "
|
|
|
|
"model name of the 'app_label.ModelName' form."
|
|
|
|
)
|
2013-12-28 21:55:54 +08:00
|
|
|
try:
|
|
|
|
sender = apps.get_registered_model(app_label, model_name)
|
|
|
|
except LookupError:
|
2013-12-28 21:24:14 +08:00
|
|
|
ref = (app_label, model_name)
|
|
|
|
refs = self.unresolved_references.setdefault(ref, [])
|
|
|
|
refs.append((receiver, weak, dispatch_uid))
|
2013-11-05 12:11:51 +08:00
|
|
|
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)
|
|
|
|
|
2014-09-04 20:15:09 +08:00
|
|
|
m2m_changed = ModelSignal(
|
|
|
|
providing_args=["action", "instance", "reverse", "model", "pk_set", "using"],
|
|
|
|
use_caching=True,
|
|
|
|
)
|
2008-08-06 23:32:46 +08:00
|
|
|
|
2016-05-13 23:58:54 +08:00
|
|
|
pre_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "using", "apps", "plan"])
|
|
|
|
post_migrate = Signal(providing_args=["app_config", "verbosity", "interactive", "using", "apps", "plan"])
|