signals.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. .. _signals:
  2. Signals
  3. =======
  4. Wagtail's :ref:`page-revision-model-ref` and :ref:`page-model-ref` implement
  5. `Signals <https://docs.djangoproject.com/en/1.8/topics/signals/>`__ from ``django.dispatch``.
  6. Signals are useful for creating side-effects from page publish/unpublish events.
  7. Primarily, these are used by the :doc:`Frontend Cache <./contrib/frontendcache>` contrib module
  8. and the :doc:`./contrib/api/index`. You could also use signals to send
  9. publish notifications to a messaging service, or ``POST`` messages to another
  10. app that's consuming the API, such as a static site generator.
  11. page_published
  12. --------------
  13. This signal is emitted from a ``PageRevision`` when a revision is set to `published`.
  14. :sender: The page ``class``
  15. :instance: The specific ``Page`` instance.
  16. :revision: The ``PageRevision`` that was published
  17. :kwargs: Any other arguments passed to ``page_published.send()``.
  18. To listen to a signal, implement ``page_published.connect(receiver, sender, **kwargs)``. Here's a simple
  19. example showing how you might notify your team when something is published:
  20. .. code-block:: python
  21. from wagtail.wagtailcore.signals import page_published
  22. import urllib
  23. import urllib2
  24. # Let everyone know when a new page is published
  25. def send_to_slack(sender, **kwargs):
  26. instance = kwargs['instance']
  27. url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
  28. values = {
  29. "text" : "%s was published by %s " % (instance.title, instance.owner.username),
  30. "channel": "#publish-notifications",
  31. "username": "the squid of content",
  32. "icon_emoji": ":octopus:"
  33. }
  34. data = urllib.urlencode(values)
  35. req = urllib2.Request(url, data)
  36. response = urllib2.urlopen(req)
  37. # Register a receiver
  38. page_published.connect(send_to_slack)
  39. Receiving specific model events
  40. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  41. Sometimes you're not interested in receiving signals for every model, or you want
  42. to handle signals for specific models in different ways. For instance, you may
  43. wish to do something when a new blog post is published:
  44. .. code-block:: python
  45. from wagtail.wagtailcore.signals import page_published
  46. from mysite.models import BlogPostPage
  47. # Do something clever for each model type
  48. def receiver(sender, **kwargs):
  49. # Do something with blog posts
  50. pass
  51. # Register listeners for each page model class
  52. page_published.connect(receiver, sender=BlogPostPage)
  53. Wagtail provides access to a list of registered page types through the ``get_page_models()`` function in ``wagtail.wagtailcore.models``.
  54. Read the `Django documentation <https://docs.djangoproject.com/en/1.8/topics/signals/#connecting-to-specific-signals>`__ for more information about specifying senders.
  55. page_unpublished
  56. ----------------
  57. This signal is emitted from a ``Page`` when the page is unpublished.
  58. :sender: The page ``class``
  59. :instance: The specific ``Page`` instance.
  60. :kwargs: Any other arguments passed to ``page_unpublished.send()``