2022-06-26 16:41:13 +02:00
# Signals
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
Wagtail's [](revision_model_ref) and [](page_model_ref) implement [Signals ](django:topics/signals ) from `django.dispatch` .
2015-10-05 14:18:25 +02:00
Signals are useful for creating side-effects from page publish/unpublish events.
2022-06-26 16:41:13 +02:00
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.
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
## `page_published`
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `Revision` when a page revision is set to `published` .
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
- `sender` - The page `class` .
- `instance` - The specific `Page` instance.
- `revision` - The `Revision` that was published.
- `kwargs` - Any other arguments passed to `page_published.send()` .
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
To listen to a signal, implement `page_published.connect(receiver, sender, **kwargs)` . Here's a simple
2015-10-05 14:18:25 +02:00
example showing how you might notify your team when something is published:
2022-06-26 16:41:13 +02:00
```python
from wagtail.signals import page_published
import requests
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
# Let everyone know when a new page is published
def send_to_slack(sender, **kwargs):
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:"
}
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
response = requests.post(url, values)
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
# Register a receiver
page_published.connect(send_to_slack)
```
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
### Receiving specific model events
2015-10-05 14:18:25 +02:00
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:
2022-06-26 16:41:13 +02:00
```python
from wagtail.signals import page_published
from mysite.models import BlogPostPage
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
# Do something clever for each model type
def receiver(sender, **kwargs):
# Do something with blog posts
pass
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
# Register listeners for each page model class
page_published.connect(receiver, sender=BlogPostPage)
```
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
Wagtail provides access to a list of registered page types through the `get_page_models()` function in `wagtail.models` .
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
Read the [Django documentation ](django:topics/signals ) for more information about specifying senders.
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
## `page_unpublished`
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `Page` when the page is unpublished.
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
- `sender` - The page `class` .
- `instance` - The specific `Page` instance.
- `kwargs` - Any other arguments passed to `page_unpublished.send()`
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
## `pre_page_move` and `post_page_move`
2015-10-05 14:18:25 +02:00
2022-06-26 16:41:13 +02:00
These signals are emitted from a `Page` immediately before and after it is moved.
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
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.
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
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.
2019-03-23 23:17:50 +01:00
The following arguments are emitted for both signals:
2022-06-26 16:41:13 +02:00
- `sender` - The page `class` .
- `instance` - The specific `Page` instance.
- `parent_page_before` - The parent page of `instance` **before** moving.
- `parent_page_after` - The parent page of `instance` **after** moving.
- `url_path_before` - The value of `instance.url_path` **before** moving.
- `url_path_after` - The value of `instance.url_path` **after** moving.
- `kwargs` - Any other arguments passed to `pre_page_move.send()` or `post_page_move.send()` .
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
### Distinguishing between a 'move' and a 'reorder'
2019-03-23 23:17:50 +01:00
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.
2022-06-26 16:41:13 +02:00
The best way to distinguish between a 'move' and 'reorder' is to compare the `url_path_before` and `url_path_after` values. For example:
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
```python
from wagtail.signals import pre_page_move
from wagtail.contrib.frontend_cache.utils import purge_page_from_cache
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
# Clear a page's old URLs from the cache when it moves to a different section
def clear_page_url_from_cache_on_move(sender, **kwargs):
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
if kwargs['url_path_before'] == kwargs['url_path_after']:
# No URLs are changing :) nothing to do here!
return
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
# The page is moving to a new section (possibly even a new site)
# so clear old URL(s) from the cache
purge_page_from_cache(kwargs['instance'])
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
# Register a receiver
pre_page_move.connect(clear_old_page_urls_from_cache)
```
2019-03-23 23:17:50 +01:00
2022-06-26 16:41:13 +02:00
## `page_slug_changed`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `Page` when a change to its slug is published.
2022-01-07 14:10:53 +01:00
The following arguments are emitted by this signal:
2022-06-26 16:41:13 +02:00
- `sender` - The page `class` .
- `instance` - The updated (and saved), specific `Page` instance.
- `instance_before` - A copy of the specific `Page` instance from **before** the changes were saved.
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## workflow_submitted
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `WorkflowState` when a page is submitted to a workflow.
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `WorkflowState`
- `instance` - The specific `WorkflowState` instance.
- `user` - The user who submitted the workflow
- `kwargs` - Any other arguments passed to `workflow_submitted.send()`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## workflow_rejected
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `WorkflowState` when a page is rejected from a workflow.
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `WorkflowState`
- `instance` - The specific `WorkflowState` instance.
- `user` - The user who rejected the workflow
- `kwargs` - Any other arguments passed to `workflow_rejected.send()`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## workflow_approved
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `WorkflowState` when a page's workflow completes successfully
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `WorkflowState`
- `instance` - The specific `WorkflowState` instance.
- `user` - The user who last approved the workflow
- `kwargs` - Any other arguments passed to `workflow_approved.send()`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## workflow_cancelled
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `WorkflowState` when a page's workflow is cancelled
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `WorkflowState`
- `instance` - The specific `WorkflowState` instance.
- `user` - The user who cancelled the workflow
- `kwargs` - Any other arguments passed to `workflow_cancelled.send()`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## task_submitted
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `TaskState` when a page is submitted to a task.
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `TaskState`
- `instance` - The specific `TaskState` instance.
- `user` - The user who submitted the page to the task
- `kwargs` - Any other arguments passed to `task_submitted.send()`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## task_rejected
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `TaskState` when a page is rejected from a task.
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `TaskState`
- `instance` - The specific `TaskState` instance.
- `user` - The user who rejected the task
- `kwargs` - Any other arguments passed to `task_rejected.send()`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## task_approved
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `TaskState` when a page's task is approved
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `TaskState`
- `instance` - The specific `TaskState` instance.
- `user` - The user who approved the task
- `kwargs` - Any other arguments passed to `task_approved.send()`
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
## task_cancelled
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
This signal is emitted from a `TaskState` when a page's task is cancelled.
2020-02-24 16:05:32 +01:00
2022-06-26 16:41:13 +02:00
- `sender` - `TaskState`
- `instance` - The specific `TaskState` instance.
- `user` - The user who cancelled the task
- `kwargs` - Any other arguments passed to `task_cancelled.send()`