Skip to content

Commit

Permalink
Allow default item in action menu to be overridden with construct_pag…
Browse files Browse the repository at this point in the history
…e_action_menu hook

Fixes wagtail#5438
  • Loading branch information
rjpruitt16 authored and gasman committed Oct 9, 2019
1 parent f4cc454 commit 1123065
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 20 deletions.
21 changes: 21 additions & 0 deletions docs/reference/hooks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,33 @@ Hooks for customising the way users are directed through the process of creating

Modify the final list of action menu items on the page creation and edit views. The callable passed to this hook receives a list of ``ActionMenuItem`` objects, a request object and a context dictionary as per ``register_page_action_menu_item``, and should modify the list of menu items in-place.


.. code-block:: python
@hooks.register('construct_page_action_menu')
def remove_submit_to_moderator_option(menu_items, request, context):
menu_items[:] = [item for item in menu_items if item.name != 'action-submit']
.. _construct_page_action_menu:

Can also be used to customize default action menu button. The last item in menu_item variable is chosen as the default action. An example on changing the default to publish can be seen below.

.. code-block:: python
from wagtail.admin.action_menu import UnpublishMenuItem, DeleteMenuItem, SubmitForModerationMenuItem, SaveDraftMenuItem, PublishMenuItem
@hooks.register('construct_page_action_menu')
def make_publish_default_action(menu_items, request, context):
menu_items[:] = [
UnpublishMenuItem(order=10),
DeleteMenuItem(order=20),
SubmitForModerationMenuItem(order=30),
SaveDraftMenuItem(order=40),
PublishMenuItem(order=50),
]
.. construct_page_listing_buttons:
``construct_page_listing_buttons``
Expand Down
18 changes: 17 additions & 1 deletion wagtail/admin/action_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def render_html(self, request, parent_context):


class PublishMenuItem(ActionMenuItem):
label = "Publish"
name = 'action-publish'
template = 'wagtailadmin/pages/action_menu/publish.html'

Expand Down Expand Up @@ -121,6 +122,17 @@ def get_url(self, request, context):
return reverse('wagtailadmin_pages:delete', args=(context['page'].id,))


class SaveDraftMenuItem(ActionMenuItem):
name = 'action-save-draft'
label = _("Save Draft")
template = 'wagtailadmin/pages/action_menu/save_draft.html'

def get_context(self, request, parent_context):
context = super().get_context(request, parent_context)
context['is_revision'] = (context['view'] == 'revisions_revert')
return context


BASE_PAGE_ACTION_MENU_ITEMS = None


Expand All @@ -137,6 +149,7 @@ def _get_base_page_action_menu_items():
DeleteMenuItem(order=20),
PublishMenuItem(order=30),
SubmitForModerationMenuItem(order=40),
SaveDraftMenuItem(order=50),
]
for hook in hooks.get_hooks('register_page_action_menu_item'):
BASE_PAGE_ACTION_MENU_ITEMS.append(hook())
Expand All @@ -163,13 +176,16 @@ def __init__(self, request, **kwargs):
for hook in hooks.get_hooks('construct_page_action_menu'):
hook(self.menu_items, self.request, self.context)

self.default_item = self.menu_items.pop() if self.menu_items else SaveDraftMenuItem(order=50)

def render_html(self):
return render_to_string(self.template, {
'default_menu_item': self.default_item.render_html(self.request, self.context),
'show_menu': bool(self.menu_items),
'rendered_menu_items': [
menu_item.render_html(self.request, self.context)
for menu_item in self.menu_items
]
],
}, request=self.request)

@cached_property
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{% if show_menu %}
{{ default_menu_item }}
<div class="dropdown-toggle icon icon-arrow-up"></div>
<ul>
{% for item in rendered_menu_items %}{{ item }}{% endfor %}
{% for item in rendered_menu_items %}
<li>
{{ item }}
</li>
{% endfor %}
</ul>
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<li>
{% if url %}
<a href="{{ url }}">{{ label }}</a>
{% else %}
<input type="submit" name="{{ name }}" value="{{ label }}" class="button" />
{% endif %}
</li>
{% if url %}
<a class="button" href="{{ url }}">{{ label }}</a>
{% else %}
<input type="submit" name="{{ name }}" value="{{ label }}" class="button" />
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
{% load i18n %}
<li>
<button type="submit" name="action-publish" value="action-publish" class="button button-longrunning {% if is_revision %}warning{% endif %}" data-clicked-text="{% trans 'Publishing…' %}"><span class="icon icon-spinner"></span><em>{% if is_revision %}{% trans 'Publish this revision' %}{% else %}{% trans 'Publish' %}{% endif %}</em></button>
</li>
<button type="submit" name="action-publish" value="action-publish" class="button button-longrunning {% if is_revision %}warning{% endif %}" data-clicked-text="{% trans 'Publishing…' %}"><span class="icon icon-spinner"></span><em>{% if is_revision %}{% trans 'Publish this revision' %}{% else %}{% trans 'Publish' %}{% endif %}</em></button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% load i18n %}
<button type="submit" class="button action-save button-longrunning {% if is_revision %}warning{% endif %}" data-clicked-text="{% trans 'Saving…' %}" {% if page.locked %}disabled {% endif %}><span class="icon icon-spinner"></span><em>{% if page.locked %}{% trans 'Page locked' %}{% else %}{% if is_revision %}{% trans 'Replace current draft' %}{% else %}{% trans 'Save draft' %}{% endif %}{% endif %}</em></button>
11 changes: 5 additions & 6 deletions wagtail/admin/templates/wagtailadmin/pages/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ <h1 class="icon icon-doc-empty-inverse">
<footer role="contentinfo">
<nav aria-label="{% trans 'Actions' %}">
<ul>
<li class="actions">
<div class="dropdown dropup dropdown-button match-width">
<button type="submit" class="button action-save button-longrunning" data-clicked-text="{% trans 'Saving…' %}"><span class="icon icon-spinner"></span><em>{% trans 'Save draft' %}</em></button>
{{ action_menu.render_html }}
</div>
</li>
<li class="actions">
<div class="dropdown dropup dropdown-button match-width {% if is_revision %}warning{% endif %}">
{{ action_menu.render_html }}
</div>
</li>

<li class="preview">
{% trans 'Preview' as preview_label %}
Expand Down
2 changes: 0 additions & 2 deletions wagtail/admin/templates/wagtailadmin/pages/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ <h1 class="icon icon-doc-empty-inverse">
<ul>
<li class="actions">
<div class="dropdown dropup dropdown-button match-width {% if is_revision %}warning{% endif %}">
<button type="submit" class="button action-save button-longrunning {% if is_revision %}warning{% endif %}" data-clicked-text="{% trans 'Saving…' %}" {% if page.locked %}disabled {% endif %}><span class="icon icon-spinner"></span><em>{% if page.locked %}{% trans 'Page locked' %}{% else %}{% if is_revision %}{% trans 'Replace current draft' %}{% else %}{% trans 'Save draft' %}{% endif %}{% endif %}</em></button>

{{ action_menu.render_html }}
</div>
</li>
Expand Down
72 changes: 72 additions & 0 deletions wagtail/admin/tests/pages/test_edit_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
from django.urls import reverse
from django.utils import timezone

from wagtail.admin.action_menu import (
DeleteMenuItem, PublishMenuItem, SaveDraftMenuItem, SubmitForModerationMenuItem,
UnpublishMenuItem)
from wagtail.admin.tests.pages.timestamps import submittable_timestamp
from wagtail.core.models import Page, PageRevision, Site
from wagtail.core.signals import page_published
Expand Down Expand Up @@ -854,6 +857,75 @@ def hook_func(request, page):
# page should be edited
self.assertEqual(Page.objects.get(id=self.child_page.id).title, "I've been edited!")

def test_construct_page_action_menu_hook(self):
menu = [
UnpublishMenuItem(order=10),
DeleteMenuItem(order=20),
SubmitForModerationMenuItem(order=30),
SaveDraftMenuItem(order=40),
PublishMenuItem(order=50),
]

default_item = '<button type="submit" name="action-publish" value="action-publish" class="button button-longrunning " data-clicked-text="Publishing…"><span class="icon icon-spinner"></span><em>Publish</em></button>'
menu_items_html = '''<ul>
<li>
<a class="button" href="/admin/pages/{id}/unpublish/">Unpublish</a>
</li>
<li>
<a class="button" href="/admin/pages/{id}/delete/">Delete</a>
</li>
<li>
<input type="submit" name="action-submit" value="Submit for moderation" class="button" />
</li>
<li>
<button type="submit" class="button action-save button-longrunning " data-clicked-text="Saving…" ><span class="icon icon-spinner"></span><em>Save draft</em></button>
</li>
</ul>'''.format(id=self.single_event_page.id)

def hook_func(menu_items, request, context):
menu_items[:] = menu

with self.register_hook('construct_page_action_menu', hook_func):
response = self.client.get(reverse('wagtailadmin_pages:edit',
args=(self.single_event_page.id, )))

self.assertContains(response, default_item, html=True)
self.assertContains(response, menu_items_html, html=True)

for item in menu:
if item.template:
self.assertTemplateUsed(response, item.template)

def test_construct_page_action_menu_hook_2(self):
menu = [
DeleteMenuItem(order=20),
SubmitForModerationMenuItem(order=30),
]

default_item = '<input type="submit" name="action-submit" value="Submit for moderation" class="button" />'
menu_items_html = '''<ul>
<li>
<a class="button" href="/admin/pages/{id}/delete/">Delete</a>
</li>
</ul>'''.format(id=self.single_event_page.id)

def hook_func(menu_items, request, context):
menu_items[:] = menu

with self.register_hook('construct_page_action_menu', hook_func):
response = self.client.get(reverse('wagtailadmin_pages:edit',
args=(self.single_event_page.id, )))

self.assertContains(response, default_item, html=True)
self.assertContains(response, menu_items_html, html=True)

for item in menu:
if item.template:
self.assertTemplateUsed(response, item.template)

self.assertTemplateNotUsed(response, "wagtailadmin/pages/action_menu/save_draft.html")
self.assertTemplateNotUsed(response, "wagtailadmin/pages/action_menu/publish.html")


class TestPageEditReordering(TestCase, WagtailTestUtils):
def setUp(self):
Expand Down

0 comments on commit 1123065

Please sign in to comment.