Implement the page_url_path_changed signal

pull/7776/head
Andy Babic 2021-12-08 09:49:49 +00:00 zatwierdzone przez Andy Babic
rodzic 361c3d5d54
commit 2f86eda372
2 zmienionych plików z 41 dodań i 13 usunięć

Wyświetl plik

@ -52,9 +52,9 @@ from wagtail.core.forms import TaskStateCommentForm
from wagtail.core.log_actions import log
from wagtail.core.query import PageQuerySet
from wagtail.core.signals import (
page_published, page_unpublished, post_page_move, pre_page_move, pre_validate_delete,
task_approved, task_cancelled, task_rejected, task_submitted, workflow_approved,
workflow_cancelled, workflow_rejected, workflow_submitted)
page_published, page_unpublished, page_url_path_changed, post_page_move, pre_page_move,
pre_validate_delete, task_approved, task_cancelled, task_rejected, task_submitted,
workflow_approved, workflow_cancelled, workflow_rejected, workflow_submitted)
from wagtail.core.treebeard import TreebeardPathFixMixin
from wagtail.core.url_routing import RouteResult
from wagtail.core.utils import (
@ -482,8 +482,8 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
if clean:
self.full_clean()
update_descendant_url_paths = False
is_new = self.id is None
slug_changed = False
if is_new:
# we are creating a record. If we're doing things properly, this should happen
@ -499,18 +499,18 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
old_record = Page.objects.get(id=self.id)
if old_record.slug != self.slug:
self.set_url_path(self.get_parent())
update_descendant_url_paths = True
slug_changed = True
old_url_path = old_record.url_path
new_url_path = self.url_path
result = super().save(**kwargs)
if not is_new and update_descendant_url_paths:
if slug_changed:
self._update_descendant_url_paths(old_url_path, new_url_path)
# Check if this is a root page of any sites and clear the 'wagtail_site_root_paths' key if so
# Note: New translations of existing site roots are considered site roots as well, so we must
# always check if this page is a site root, even if it's new.
# always check if this page is a site root, even if it's new.
if self.is_site_root():
cache.delete('wagtail_site_root_paths')
@ -526,24 +526,35 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
self.url_path
)
log_entry = None
if log_action is not None:
# The default for log_action is False. i.e. don't log unless specifically instructed
# Page creation is a special case that we want logged by default, but allow skipping it
# explicitly by passing log_action=None
if is_new:
log(
log_entry = log(
instance=self,
action='wagtail.create',
user=user or self.owner,
content_changed=True,
)
elif log_action:
log(
log_entry = log(
instance=self,
action=log_action,
user=user
)
if slug_changed:
# Emit page_url_path_changed signal only on successful commit
transaction.on_commit(lambda: page_url_path_changed.send(
sender=self.specific_class or self.__class__,
instance=self,
url_path_before=old_url_path,
url_path_after=new_url_path,
log_entry=log_entry,
))
return result
def delete(self, *args, **kwargs):
@ -1492,6 +1503,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
old_self = Page.objects.get(id=self.id)
old_url_path = old_self.url_path
new_url_path = old_self.set_url_path(parent=parent_after)
url_path_changed = old_url_path != new_url_path
# Emit pre_page_move signal
pre_page_move.send(
@ -1515,7 +1527,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
new_self.save()
# Update descendant paths if url_path has changed
if old_url_path != new_url_path:
if url_path_changed:
new_self._update_descendant_url_paths(old_url_path, new_url_path)
# Emit post_page_move signal
@ -1529,10 +1541,10 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
)
# Log
log(
log_entry = log(
instance=self,
# Check if page was reordered (reordering doesn't change the parent)
action='wagtail.reorder' if parent_before.id == target.id else 'wagtail.move',
action='wagtail.move' if url_path_changed else 'wagtail.reorder',
user=user,
data={
'source': {
@ -1547,6 +1559,16 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
)
logger.info("Page moved: \"%s\" id=%d path=%s", self.title, self.id, new_url_path)
if url_path_changed:
# Emit page_url_path_changed signal
page_url_path_changed.send(
sender=self.specific_class or self.__class__,
instance=self,
url_path_before=old_url_path,
url_path_after=new_url_path,
log_entry=log_entry,
)
def copy(self, recursive=False, to=None, update_attrs=None, copy_revisions=True, keep_live=True, user=None,
process_child_object=None, exclude_fields=None, log_action='wagtail.copy', reset_translation_key=True):
"""
@ -1973,7 +1995,10 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase):
def get_cached_paths(self):
"""
This returns a list of paths to invalidate in a frontend cache
This returns a list of paths to invalidate in a frontend cache, or when
create redirects when the page's URL changes as a result of a slug
change or move. These values are automatically added to the page's
default URL to create full URLs.
"""
return ['/']

Wyświetl plik

@ -9,6 +9,9 @@ page_published = Signal()
# provides args: instance
page_unpublished = Signal()
# provides args: instance, url_path_before, url_path_after, log_entry
page_url_path_changed = Signal()
# provides args: instance, parent_page_before, parent_page_after, url_path_before, url_path_after
pre_page_move = Signal()