signals.rst 3.0 KB

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