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
|
|
|
|
2020-03-02 01:22:03 +08:00
|
|
|
class_prepared = Signal()
|
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
|
|
|
|
2020-03-02 01:22:03 +08:00
|
|
|
pre_init = ModelSignal(use_caching=True)
|
|
|
|
post_init = ModelSignal(use_caching=True)
|
2013-11-05 12:11:51 +08:00
|
|
|
|
2020-03-02 01:22:03 +08:00
|
|
|
pre_save = ModelSignal(use_caching=True)
|
|
|
|
post_save = ModelSignal(use_caching=True)
|
2013-11-05 12:11:51 +08:00
|
|
|
|
2020-03-02 01:22:03 +08:00
|
|
|
pre_delete = ModelSignal(use_caching=True)
|
|
|
|
post_delete = ModelSignal(use_caching=True)
|
2013-11-05 12:11:51 +08:00
|
|
|
|
2020-03-02 01:22:03 +08:00
|
|
|
m2m_changed = ModelSignal(use_caching=True)
|
2008-08-06 23:32:46 +08:00
|
|
|
|
2020-03-02 01:22:03 +08:00
|
|
|
pre_migrate = Signal()
|
|
|
|
post_migrate = Signal()
|