2015-10-05 12:18:25 +00:00
.. _signals:
Signals
=======
Wagtail's `PageRevision <./pages/model_reference#pagerevision> `__ and
`Page <./pages/model_reference#page> `__ implement
`Signals <https://docs.djangoproject.com/en/1.8/topics/signals/> `__ from `` django.dispatch `` .
Signals are useful for creating side-effects from page publish/unpublish events.
Primarily, these are used by the `Frontend Cache <./contrib/frontendcache> `__ contrib module
and the `Wagtail API <./contrib/api/index> `__ . You could also 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.
page_published
--------------
This signal is emitted from a `` PageRevision `` when a revision is set to `published` .
:sender: The page ``class``
:instance: The specific ``Page`` instance.
:revision: The ``PageRevision`` that was published
:kwargs: Any other arguments passed to ``page_published.send()``.
2015-10-05 12:29:56 +00:00
To listen to a signal, implement `` page_published.connect(receiver, sender, **kwargs) `` . Here's a simple
2015-10-05 12:18:25 +00:00
example showing how you might notify your team when something is published:
.. code-block :: python
2016-01-04 23:17:48 +00:00
from wagtail.wagtailcore.signals import page_published
2015-10-05 12:18:25 +00:00
import urllib
import urllib2
# Let everyone know when a new page is published
2016-01-04 23:17:48 +00:00
def send_to_slack(sender, **kwargs):
2015-10-05 12:18:25 +00:00
instance = kwargs['instance']
url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
values = {
"text" : "%s was published by %s " % (instance.title, instance.owner.username),
"channel": "#publish-notifications",
"username": "the squid of content",
"icon_emoji": ":octopus:"
}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
# Register a receiver
page_published.connect(send_to_slack)
Receiving specific model events
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sometimes you're not interested in receiving signals for every model, or you want
to handle signals for specific models in different ways. For instance, you may
wish to do something when a new blog post is published:
.. code-block :: python
2016-01-04 23:17:48 +00:00
from wagtail.wagtailcore.signals import page_published
2015-10-05 12:18:25 +00:00
from mysite.models import BlogPostPage
# Do something clever for each model type
def receiver(model, **kwargs):
# Do something with blog posts
pass
# Register listeners for each page model class
page_published.connect(receiver, sender=BlogPostPage)
2015-11-20 14:53:40 +00:00
Wagtail provides access to a list of registered page types through the `` get_page_models() `` function in `` wagtail.wagtailcore.models `` .
2015-10-05 12:18:25 +00:00
Read the `Django documentation <https://docs.djangoproject.com/en/1.8/topics/signals/#connecting-to-specific-signals> `__ for more information about specifying senders.
page_unpublished
----------------
This signal is emitted from a `` Page `` when the page is unpublished.
:sender: The page ``class``
:instance: The specific ``Page`` instance.
2016-01-04 23:17:48 +00:00
:kwargs: Any other arguments passed to ``page_unpublished.send()``