mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Add hooks for page move actions
This commit is contained in:
parent
a3dc9ed477
commit
f34bda64ea
@ -492,6 +492,22 @@ Hooks for customising the way users are directed through the process of creating
|
||||
|
||||
Uses the same behaviour as ``before_create_page``.
|
||||
|
||||
.. _after_move_page:
|
||||
|
||||
``after_move_page``
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Do something with a ``Page`` object after it has been moved passing in the request and page object. Uses the same behaviour as ``after_create_page``.
|
||||
|
||||
|
||||
.. _before_move_page:
|
||||
|
||||
``before_move_page``
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Called at the beginning of the "move_confirm page" view passing in the request, the page object and the destination page object.
|
||||
|
||||
Uses the same behaviour as ``before_create_page``.
|
||||
|
||||
.. _register_page_action_menu_item:
|
||||
|
||||
|
@ -2563,6 +2563,47 @@ class TestPageMove(TestCase, WagtailTestUtils):
|
||||
response = self.client.get(reverse('wagtailadmin_pages:set_page_position', args=(self.test_page.id, )))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_before_move_page_hook(self):
|
||||
def hook_func(request, page, destination):
|
||||
self.assertIsInstance(request, HttpRequest)
|
||||
self.assertIsInstance(page.specific, SimplePage)
|
||||
self.assertIsInstance(destination.specific, SimplePage)
|
||||
|
||||
return HttpResponse("Overridden!")
|
||||
|
||||
with self.register_hook('before_move_page', hook_func):
|
||||
response = self.client.get(reverse('wagtailadmin_pages:move_confirm', args=(self.test_page.id, self.section_b.id)))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content, b"Overridden!")
|
||||
|
||||
def test_before_move_page_hook_post(self):
|
||||
def hook_func(request, page, destination):
|
||||
self.assertIsInstance(request, HttpRequest)
|
||||
self.assertIsInstance(page.specific, SimplePage)
|
||||
self.assertIsInstance(destination.specific, SimplePage)
|
||||
|
||||
return HttpResponse("Overridden!")
|
||||
|
||||
with self.register_hook('before_move_page', hook_func):
|
||||
response = self.client.post(reverse('wagtailadmin_pages:move_confirm', args=(self.test_page.id, self.section_b.id)))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content, b"Overridden!")
|
||||
|
||||
def test_after_move_page_hook(self):
|
||||
def hook_func(request, page):
|
||||
self.assertIsInstance(request, HttpRequest)
|
||||
self.assertIsInstance(page.specific, SimplePage)
|
||||
|
||||
return HttpResponse("Overridden!")
|
||||
|
||||
with self.register_hook('after_move_page', hook_func):
|
||||
response = self.client.post(reverse('wagtailadmin_pages:move_confirm', args=(self.test_page.id, self.section_b.id)))
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.content, b"Overridden!")
|
||||
|
||||
|
||||
class TestPageCopy(TestCase, WagtailTestUtils):
|
||||
|
||||
|
@ -757,6 +757,11 @@ def move_confirm(request, page_to_move_id, destination_id):
|
||||
if not page_to_move.permissions_for_user(request.user).can_move_to(destination):
|
||||
raise PermissionDenied
|
||||
|
||||
for fn in hooks.get_hooks('before_move_page'):
|
||||
result = fn(request, page_to_move, destination)
|
||||
if hasattr(result, 'status_code'):
|
||||
return result
|
||||
|
||||
if request.method == 'POST':
|
||||
# any invalid moves *should* be caught by the permission check above,
|
||||
# so don't bother to catch InvalidMoveToDescendant
|
||||
@ -766,6 +771,11 @@ def move_confirm(request, page_to_move_id, destination_id):
|
||||
messages.button(reverse('wagtailadmin_pages:edit', args=(page_to_move.id,)), _('Edit'))
|
||||
])
|
||||
|
||||
for fn in hooks.get_hooks('after_move_page'):
|
||||
result = fn(request, page_to_move)
|
||||
if hasattr(result, 'status_code'):
|
||||
return result
|
||||
|
||||
return redirect('wagtailadmin_explore', destination.id)
|
||||
|
||||
return render(request, 'wagtailadmin/pages/confirm_move.html', {
|
||||
|
Loading…
Reference in New Issue
Block a user