mirror of
https://github.com/wagtail/wagtail.git
synced 2024-12-01 11:41:20 +01:00
Recursive unpublish for descendant pages
This commit is contained in:
parent
f6b47235a3
commit
65ba8873d2
@ -10,8 +10,28 @@
|
||||
<form action="{% url 'wagtailadmin_pages:unpublish' page.id %}" method="POST">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" name="next" value="{{ next }}">
|
||||
<input type="submit" value="{% trans 'Yes, unpublish it' %}" class="button">
|
||||
<a href="{% if next %}{{ next }}{% else %}{% url 'wagtailadmin_explore' page.get_parent.id %}{% endif %}" class="button button-secondary">{% trans "No, don't unpublish" %}</a>
|
||||
<ul class="fields">
|
||||
{% if live_descendant_count > 0 %}
|
||||
<li>
|
||||
<div class="field boolean_field checkbox_input">
|
||||
<div class="field-content">
|
||||
<div class="input">
|
||||
<input id="id_include_descendants" name="include_descendants" type="checkbox">
|
||||
<span>{% blocktrans count counter=live_descendant_count %}
|
||||
This will also unpublish one more subpage.
|
||||
{% plural %}
|
||||
This will also unpublish {{ live_descendant_count }} more subpages.
|
||||
{% endblocktrans %}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
{% endif %}
|
||||
<li>
|
||||
<input type="submit" value="{% trans 'Yes, unpublish it' %}" class="button">
|
||||
<a href="{% if next %}{{ next }}{% else %}{% url 'wagtailadmin_explore' page.get_parent.id %}{% endif %}" class="button button-secondary">{% trans "No, don't unpublish" %}</a>
|
||||
</li>
|
||||
</ul>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
@ -2139,6 +2139,99 @@ class TestPageUnpublish(TestCase, WagtailTestUtils):
|
||||
self.assertEqual(mock_call['instance'], self.page)
|
||||
self.assertIsInstance(mock_call['instance'], self.page.specific_class)
|
||||
|
||||
def test_unpublish_descendants_view(self):
|
||||
"""
|
||||
This tests that the unpublish view responds with an unpublish confirm page that does not contain the form field 'include_descendants'
|
||||
"""
|
||||
# Get unpublish page
|
||||
response = self.client.get(reverse('wagtailadmin_pages:unpublish', args=(self.page.id, )))
|
||||
|
||||
# Check that the user recieved an unpublish confirm page
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, 'wagtailadmin/pages/confirm_unpublish.html')
|
||||
# Check the form does not contain the checkbox field include_descendants
|
||||
self.assertNotContains(response, '<input id="id_include_descendants" name="include_descendants" type="checkbox">')
|
||||
|
||||
|
||||
class TestPageUnpublishIncludingDescendants(TestCase, WagtailTestUtils):
|
||||
def setUp(self):
|
||||
self.user = self.login()
|
||||
# Find root page
|
||||
self.root_page = Page.objects.get(id=2)
|
||||
|
||||
# Create a page to unpublish
|
||||
self.test_page = self.root_page.add_child(instance=SimplePage(
|
||||
title="Hello world!",
|
||||
slug='hello-world',
|
||||
content="hello",
|
||||
live=True,
|
||||
has_unpublished_changes=False,
|
||||
))
|
||||
|
||||
# Create a couple of child pages
|
||||
self.test_child_page = self.test_page.add_child(instance=SimplePage(
|
||||
title="Child page",
|
||||
slug='child-page',
|
||||
content="hello",
|
||||
live=True,
|
||||
has_unpublished_changes=True,
|
||||
))
|
||||
|
||||
self.test_another_child_page = self.test_page.add_child(instance=SimplePage(
|
||||
title="Another Child page",
|
||||
slug='another-child-page',
|
||||
content="hello",
|
||||
live=True,
|
||||
has_unpublished_changes=True,
|
||||
))
|
||||
|
||||
def test_unpublish_descendants_view(self):
|
||||
"""
|
||||
This tests that the unpublish view responds with an unpublish confirm page that contains the form field 'include_descendants'
|
||||
"""
|
||||
# Get unpublish page
|
||||
response = self.client.get(reverse('wagtailadmin_pages:unpublish', args=(self.test_page.id, )))
|
||||
|
||||
# Check that the user recieved an unpublish confirm page
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTemplateUsed(response, 'wagtailadmin/pages/confirm_unpublish.html')
|
||||
# Check the form contains the checkbox field include_descendants
|
||||
self.assertContains(response, '<input id="id_include_descendants" name="include_descendants" type="checkbox">')
|
||||
|
||||
def test_unpublish_include_children_view_post(self):
|
||||
"""
|
||||
This posts to the unpublish view and checks that the page and its descendants were unpublished
|
||||
"""
|
||||
# Post to the unpublish page
|
||||
response = self.client.post(reverse('wagtailadmin_pages:unpublish', args=(self.test_page.id, )), {'include_descendants': 'on'})
|
||||
|
||||
# Should be redirected to explorer page
|
||||
self.assertRedirects(response, reverse('wagtailadmin_explore', args=(self.root_page.id, )))
|
||||
|
||||
# Check that the page was unpublished
|
||||
self.assertFalse(SimplePage.objects.get(id=self.test_page.id).live)
|
||||
|
||||
# Check that the descendant pages were unpiblished as well
|
||||
self.assertFalse(SimplePage.objects.get(id=self.test_child_page.id).live)
|
||||
self.assertFalse(SimplePage.objects.get(id=self.test_another_child_page.id).live)
|
||||
|
||||
def test_unpublish_not_include_children_view_post(self):
|
||||
"""
|
||||
This posts to the unpublish view and checks that the page was unpublished but its descendants were not
|
||||
"""
|
||||
# Post to the unpublish page
|
||||
response = self.client.post(reverse('wagtailadmin_pages:unpublish', args=(self.test_page.id, )), {})
|
||||
|
||||
# Should be redirected to explorer page
|
||||
self.assertRedirects(response, reverse('wagtailadmin_explore', args=(self.root_page.id, )))
|
||||
|
||||
# Check that the page was unpublished
|
||||
self.assertFalse(SimplePage.objects.get(id=self.test_page.id).live)
|
||||
|
||||
# Check that the descendant pages were not unpublished
|
||||
self.assertTrue(SimplePage.objects.get(id=self.test_child_page.id).live)
|
||||
self.assertTrue(SimplePage.objects.get(id=self.test_another_child_page.id).live)
|
||||
|
||||
|
||||
class TestApproveRejectModeration(TestCase, WagtailTestUtils):
|
||||
def setUp(self):
|
||||
|
@ -647,8 +647,18 @@ def unpublish(request, page_id):
|
||||
next_url = get_valid_next_url_from_request(request)
|
||||
|
||||
if request.method == 'POST':
|
||||
include_descendants = request.POST.get("include_descendants", False)
|
||||
|
||||
page.unpublish()
|
||||
|
||||
if include_descendants == 'on':
|
||||
live_descendant_pages = page.get_descendants().live().specific()
|
||||
for live_descendant_page in live_descendant_pages:
|
||||
if not live_descendant_page.permissions_for_user(request.user).can_unpublish():
|
||||
raise PermissionDenied
|
||||
for live_descendant_page in live_descendant_pages:
|
||||
live_descendant_page.unpublish()
|
||||
|
||||
messages.success(request, _("Page '{0}' unpublished.").format(page.title), buttons=[
|
||||
messages.button(reverse('wagtailadmin_pages:edit', args=(page.id,)), _('Edit'))
|
||||
])
|
||||
@ -660,6 +670,7 @@ def unpublish(request, page_id):
|
||||
return render(request, 'wagtailadmin/pages/confirm_unpublish.html', {
|
||||
'page': page,
|
||||
'next': next_url,
|
||||
'live_descendant_count': page.get_descendants().live().count(),
|
||||
})
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user