Skip to content

Commit

Permalink
Add wagtail.reorder audit log action
Browse files Browse the repository at this point in the history
This adds a special audit message for reordering pages.

Previously, reordering pages under the same parent would show the
slighly misleading message that the page was moved to the same parent.
A special page reordered message would be more appropiate in this case.
  • Loading branch information
Stormheg committed Mar 31, 2021
1 parent 520fa23 commit 1ff679f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changelog
* Utilize `PageQuerySet.defer_streamfields()` to improve efficiency in a few key places (Andy Babic)
* Switch ``register_setting``, ``register_settings_menu_item`` to use SVG icons (Thibaud Colas)
* Add support to SVG icons for ``SearchArea`` subclasses in ``register_admin_search_area`` (Thibaud Colas)
* Add `wagtail.reorder` page audit log action (Storm Heg)
* `get_settings` template tag now supports specifying the variable name with `{% get_settings as var %}` (Samir Shah)
* Fix: StreamField required status is now consistently handled by the `blank` keyword argument (Matt Westcott)
* Fix: Show 'required' asterisks for blocks inside required StreamFields (Matt Westcott)
Expand Down
1 change: 1 addition & 0 deletions docs/advanced_topics/audit_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Action Notes
``wagtail.revert`` The page was reverted to a previous draft
``wagtail.copy`` The page was copied to a new location
``wagtail.move`` The page was moved to a new location
``wagtail.reorder`` The order of the page under it's parent was changed
``wagtail.view_restriction.create`` The page was restricted
``wagtail.view_restriction.edit`` The page restrictions were updated
``wagtail.view_restriction.delete`` The page restrictions were removed
Expand Down
1 change: 1 addition & 0 deletions docs/releases/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Other features
* Update ``PageQueryset.specific(defer=True)`` to only perform a single database query (Andy Babic)
* Switched ``register_setting``, ``register_settings_menu_item`` to use SVG icons (Thibaud Colas)
* Add support to SVG icons for ``SearchArea`` subclasses in ``register_admin_search_area`` (Thibaud Colas)
* Add specialized ``wagtail.reorder`` page audit log action. This was previously covered by the ``wagtail.move`` action (Storm Heg)
* ``get_settings`` template tag now supports specifying the variable name with ``{% get_settings as var %}`` (Samir Shah)

Bug fixes
Expand Down
9 changes: 9 additions & 0 deletions wagtail/admin/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,14 @@ def move_message(data):
except KeyError:
return _('Moved')

def reorder_message(data):
try:
return _("Reordered under '%(parent)s'") % {
'parent': data['destination']['title'],
}
except KeyError:
return _('Reordered')

def schedule_publish_message(data):
try:
if data['revision']['has_live_version']:
Expand Down Expand Up @@ -935,6 +943,7 @@ def rename_message(data):
actions.register_action('wagtail.create_alias', _('Create alias'), create_alias_message)
actions.register_action('wagtail.convert_alias', _('Convert alias into regular page'), convert_alias_message)
actions.register_action('wagtail.move', _('Move'), move_message)
actions.register_action('wagtail.reorder', _('Reorder'), reorder_message)
actions.register_action('wagtail.publish.schedule', _("Schedule publication"), schedule_publish_message)
actions.register_action('wagtail.schedule.cancel', _("Unschedule publication"), unschedule_publish_message)
actions.register_action('wagtail.view_restriction.create', _("Add view restrictions"), add_view_restriction)
Expand Down
3 changes: 2 additions & 1 deletion wagtail/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2062,7 +2062,8 @@ def move(self, target, pos=None, user=None):
# Log
PageLogEntry.objects.log_action(
instance=self,
action='wagtail.move',
# Check if page was reordered (reordering doesn't change the parent)
action='wagtail.reorder' if parent_before.id == target.id else 'wagtail.move',
user=user,
data={
'source': {
Expand Down
18 changes: 18 additions & 0 deletions wagtail/core/tests/test_audit_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ def test_page_copy(self):
['wagtail.publish', 'wagtail.copy', 'wagtail.create']
)

def test_page_reorder(self):
section_1 = self.root_page.add_child(
instance=SimplePage(title="Child 1", slug="child-1", content="hello")
)
self.root_page.add_child(
instance=SimplePage(title="Child 2", slug="child-2", content="hello")
)

user = get_user_model().objects.first()

# Reorder section 1 to be the last page under root_page.
# This should log as `wagtail.reorder` because the page was moved under the same parent page
section_1.move(self.root_page, user=user, pos="last-child")

self.assertEqual(PageLogEntry.objects.filter(action='wagtail.reorder', user=user).count(), 1)
self.assertEqual(PageLogEntry.objects.filter(action='wagtail.move', user=user).count(), 0)

def test_page_move(self):
section = self.root_page.add_child(
instance=SimplePage(title="About us", slug="about", content="hello")
Expand All @@ -193,6 +210,7 @@ def test_page_move(self):
section.move(self.home_page, user=user)

self.assertEqual(PageLogEntry.objects.filter(action='wagtail.move', user=user).count(), 1)
self.assertEqual(PageLogEntry.objects.filter(action='wagtail.reorder', user=user).count(), 0)

def test_page_delete(self):
self.home_page.add_child(
Expand Down

0 comments on commit 1ff679f

Please sign in to comment.