Coverage for /Users/davegaeddert/Developer/dropseed/plain/plain-models/plain/models/signals.py: 65%

1 from functools import partial

2

3 from plain.models.utils import make_model_tuple

4 from plain.signals.dispatch import Signal

5

6 class_prepared = Signal()

7

8

9 class ModelSignal(Signal):

10 """

11 Signal subclass that allows the sender to be lazily specified as a string

12 of the `package_label.ModelName` form.

13 """

14

15 def _lazy_method(self, method, packages, receiver, sender, **kwargs):

16 from plain.models.options import Options

17

18 # This partial takes a single optional argument named "sender".

19 partial_method = partial(method, receiver, **kwargs)

20 if isinstance(sender, str):

21 packages = packages or Options.default_packages

22 packages.lazy_model_operation(partial_method, make_model_tuple(sender))

23 else:

24 return partial_method(sender)

25

26 def connect(

27 self, receiver, sender=None, weak=True, dispatch_uid=None, packages=None

28 ):

29 self._lazy_method(

30 super().connect,

31 packages,

32 receiver,

33 sender,

34 weak=weak,

35 dispatch_uid=dispatch_uid,

36 )

37

38 def disconnect(self, receiver=None, sender=None, dispatch_uid=None, packages=None):

39 return self._lazy_method(

40 super().disconnect, packages, receiver, sender, dispatch_uid=dispatch_uid

41 )

42

43

44 pre_init = ModelSignal(use_caching=True)

45 post_init = ModelSignal(use_caching=True)

46

47 pre_save = ModelSignal(use_caching=True)

48 post_save = ModelSignal(use_caching=True)

49

50 pre_delete = ModelSignal(use_caching=True)

51 post_delete = ModelSignal(use_caching=True)

52

53 m2m_changed = ModelSignal(use_caching=True)