signals.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. .. _signals:
  2. Signals
  3. =======
  4. Wagtail's :ref:`page-revision-model-ref` and :ref:`page-model-ref` implement
  5. :doc:`Signals <topics/signals>` from ``django.dispatch``.
  6. Signals are useful for creating side-effects from page publish/unpublish events.
  7. For example, you could use signals to send publish notifications to a messaging service, or ``POST`` messages to another app that's consuming the API, such as a static site generator.
  8. ``page_published``
  9. ------------------
  10. This signal is emitted from a ``PageRevision`` when a revision is set to `published`.
  11. :sender: The page ``class``.
  12. :instance: The specific ``Page`` instance.
  13. :revision: The ``PageRevision`` that was published.
  14. :kwargs: Any other arguments passed to ``page_published.send()``.
  15. To listen to a signal, implement ``page_published.connect(receiver, sender, **kwargs)``. Here's a simple
  16. example showing how you might notify your team when something is published:
  17. .. code-block:: python
  18. from wagtail.core.signals import page_published
  19. import requests
  20. # Let everyone know when a new page is published
  21. def send_to_slack(sender, **kwargs):
  22. instance = kwargs['instance']
  23. url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
  24. values = {
  25. "text" : "%s was published by %s " % (instance.title, instance.owner.username),
  26. "channel": "#publish-notifications",
  27. "username": "the squid of content",
  28. "icon_emoji": ":octopus:"
  29. }
  30. response = requests.post(url, values)
  31. # Register a receiver
  32. page_published.connect(send_to_slack)
  33. Receiving specific model events
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. Sometimes you're not interested in receiving signals for every model, or you want
  36. to handle signals for specific models in different ways. For instance, you may
  37. wish to do something when a new blog post is published:
  38. .. code-block:: python
  39. from wagtail.core.signals import page_published
  40. from mysite.models import BlogPostPage
  41. # Do something clever for each model type
  42. def receiver(sender, **kwargs):
  43. # Do something with blog posts
  44. pass
  45. # Register listeners for each page model class
  46. page_published.connect(receiver, sender=BlogPostPage)
  47. Wagtail provides access to a list of registered page types through the ``get_page_models()`` function in ``wagtail.core.models``.
  48. Read the :ref:`Django documentation <connecting-to-specific-signals>` for more information about specifying senders.
  49. ``page_unpublished``
  50. --------------------
  51. This signal is emitted from a ``Page`` when the page is unpublished.
  52. :sender: The page ``class``.
  53. :instance: The specific ``Page`` instance.
  54. :kwargs: Any other arguments passed to ``page_unpublished.send()``
  55. ``pre_page_move`` and ``post_page_move``
  56. ------------------------------------------
  57. .. versionadded:: 2.10
  58. These signals are emitted from a ``Page`` immediately before and after it is moved.
  59. Subscribe to ``pre_page_move`` if you need to know values BEFORE any database changes are applied. For example: Getting the page's previous URL, or that of its descendants.
  60. Subscribe to ``post_page_move`` if you need to know values AFTER database changes have been applied. For example: Getting the page's new URL, or that of its descendants.
  61. The following arguments are emitted for both signals:
  62. :sender: The page ``class``.
  63. :instance: The specific ``Page`` instance.
  64. :parent_page_before: The parent page of ``instance`` **before** moving.
  65. :parent_page_after: The parent page of ``instance`` **after** moving.
  66. :url_path_before: The value of ``instance.url_path`` **before** moving.
  67. :url_path_after: The value of ``instance.url_path`` **after** moving.
  68. :kwargs: Any other arguments passed to ``pre_page_move.send()`` or ``post_page_move.send()``.
  69. Distinguishing between a 'move' and a 'reorder'
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. The signal can be emitted as a result of a page being moved to a different section (a 'move'), or as a result of a page being moved to a different position within the same section (a 'reorder'). Knowing the difference between the two can be particularly useful, because only a 'move' affects a page's URL (and that of its descendants), whereas a 'reorder' only affects the natural page order; which is probably less impactful.
  72. The best way to distinguish between a 'move' and 'reorder' is to compare the ``url_path_before`` and ``url_path_after`` values. For example:
  73. .. code-block:: python
  74. from wagtail.core.signals import pre_page_move
  75. from wagtail.contrib.frontend_cache.utils import purge_page_from_cache
  76. # Clear a page's old URLs from the cache when it moves to a different section
  77. def clear_page_url_from_cache_on_move(sender, **kwargs):
  78. if kwargs['url_path_before'] == kwargs['url_path_after']:
  79. # No URLs are changing :) nothing to do here!
  80. return
  81. # The page is moving to a new section (possibly even a new site)
  82. # so clear old URL(s) from the cache
  83. purge_page_from_cache(kwargs['instance'])
  84. # Register a receiver
  85. pre_page_move.connect(clear_old_page_urls_from_cache)
  86. workflow_submitted
  87. ------------------
  88. This signal is emitted from a ``WorkflowState`` when a page is submitted to a workflow.
  89. :sender: ``WorkflowState``
  90. :instance: The specific ``WorkflowState`` instance.
  91. :user: The user who submitted the workflow
  92. :kwargs: Any other arguments passed to ``workflow_submitted.send()``
  93. workflow_rejected
  94. -----------------
  95. This signal is emitted from a ``WorkflowState`` when a page is rejected from a workflow.
  96. :sender: ``WorkflowState``
  97. :instance: The specific ``WorkflowState`` instance.
  98. :user: The user who rejected the workflow
  99. :kwargs: Any other arguments passed to ``workflow_rejected.send()``
  100. workflow_approved
  101. -----------------
  102. This signal is emitted from a ``WorkflowState`` when a page's workflow completes successfully
  103. :sender: ``WorkflowState``
  104. :instance: The specific ``WorkflowState`` instance.
  105. :user: The user who last approved the workflow
  106. :kwargs: Any other arguments passed to ``workflow_approved.send()``
  107. workflow_cancelled
  108. ------------------
  109. This signal is emitted from a ``WorkflowState`` when a page's workflow is cancelled
  110. :sender: ``WorkflowState``
  111. :instance: The specific ``WorkflowState`` instance.
  112. :user: The user who cancelled the workflow
  113. :kwargs: Any other arguments passed to ``workflow_cancelled.send()``
  114. task_submitted
  115. --------------
  116. This signal is emitted from a ``TaskState`` when a page is submitted to a task.
  117. :sender: ``TaskState``
  118. :instance: The specific ``TaskState`` instance.
  119. :user: The user who submitted the page to the task
  120. :kwargs: Any other arguments passed to ``task_submitted.send()``
  121. task_rejected
  122. -------------
  123. This signal is emitted from a ``TaskState`` when a page is rejected from a task.
  124. :sender: ``TaskState``
  125. :instance: The specific ``TaskState`` instance.
  126. :user: The user who rejected the task
  127. :kwargs: Any other arguments passed to ``task_rejected.send()``
  128. task_approved
  129. -------------
  130. This signal is emitted from a ``TaskState`` when a page's task is approved
  131. :sender: ``TaskState``
  132. :instance: The specific ``TaskState`` instance.
  133. :user: The user who approved the task
  134. :kwargs: Any other arguments passed to ``task_approved.send()``
  135. task_cancelled
  136. --------------
  137. This signal is emitted from a ``TaskState`` when a page's task is cancelled.
  138. :sender: ``TaskState``
  139. :instance: The specific ``TaskState`` instance.
  140. :user: The user who cancelled the task
  141. :kwargs: Any other arguments passed to ``task_cancelled.send()``