Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signals.py 1.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from functools import partial
  2. from django.db.models.utils import make_model_tuple
  3. from django.dispatch import Signal
  4. class_prepared = Signal()
  5. class ModelSignal(Signal):
  6. """
  7. Signal subclass that allows the sender to be lazily specified as a string
  8. of the `app_label.ModelName` form.
  9. """
  10. def _lazy_method(self, method, apps, receiver, sender, **kwargs):
  11. from django.db.models.options import Options
  12. # This partial takes a single optional argument named "sender".
  13. partial_method = partial(method, receiver, **kwargs)
  14. if isinstance(sender, str):
  15. apps = apps or Options.default_apps
  16. apps.lazy_model_operation(partial_method, make_model_tuple(sender))
  17. else:
  18. return partial_method(sender)
  19. def connect(self, receiver, sender=None, weak=True, dispatch_uid=None, apps=None):
  20. self._lazy_method(
  21. super().connect,
  22. apps,
  23. receiver,
  24. sender,
  25. weak=weak,
  26. dispatch_uid=dispatch_uid,
  27. )
  28. def disconnect(self, receiver=None, sender=None, dispatch_uid=None, apps=None):
  29. return self._lazy_method(
  30. super().disconnect, apps, receiver, sender, dispatch_uid=dispatch_uid
  31. )
  32. pre_init = ModelSignal(use_caching=True)
  33. post_init = ModelSignal(use_caching=True)
  34. pre_save = ModelSignal(use_caching=True)
  35. post_save = ModelSignal(use_caching=True)
  36. pre_delete = ModelSignal(use_caching=True)
  37. post_delete = ModelSignal(use_caching=True)
  38. m2m_changed = ModelSignal(use_caching=True)
  39. pre_migrate = Signal()
  40. post_migrate = Signal()