2016-03-30 16:34:44 +08:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from django.db.models.utils import make_model_tuple
|
2008-08-06 23:32:46 +08:00
|
|
|
from django.dispatch import Signal
|
2013-11-05 12:11:51 +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.
|
|
|
|
"""
|
2016-05-20 16:21:20 +08:00
|
|
|
def _lazy_method(self, method, apps, receiver, sender, **kwargs):
|
2016-06-01 00:17:25 +08:00
|
|
|
from django.db.models.options import Options
|
|
|
|
|
2016-05-20 16:21:20 +08:00
|
|
|
# This partial takes a single optional argument named "sender".
|
|
|
|
partial_method = partial(method, receiver, **kwargs)
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(sender, str):
|
2016-06-01 00:17:25 +08:00
|
|
|
apps = apps or Options.default_apps
|
|
|
|
apps.lazy_model_operation(partial_method, make_model_tuple(sender))
|
|
|
|
else:
|
2016-05-20 16:21:20 +08:00
|
|
|
return partial_method(sender)
|
|
|
|
|
2016-03-30 16:34:44 +08:00
|
|
|
def connect(self, receiver, sender=None, weak=True, dispatch_uid=None, apps=None):
|
2016-06-19 08:45:38 +08:00
|
|
|
self._lazy_method(
|
2017-01-21 21:13:44 +08:00
|
|
|
super().connect, apps, receiver, sender,
|
2016-06-19 08:45:38 +08:00
|
|
|
weak=weak, dispatch_uid=dispatch_uid,
|
|
|
|
)
|
2016-05-20 16:21:20 +08:00
|
|
|
|
2016-11-07 22:46:42 +08:00
|
|
|
def disconnect(self, receiver=None, sender=None, dispatch_uid=None, apps=None):
|
2016-05-20 16:21:20 +08:00
|
|
|
return self._lazy_method(
|
2017-01-21 21:13:44 +08:00
|
|
|
super().disconnect, apps, receiver, sender, dispatch_uid=dispatch_uid
|
2016-05-20 16:21:20 +08:00
|
|
|
)
|
2013-11-05 12:11:51 +08:00
|
|
|
|
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"])
|