0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

[refactor] Use the cleaned form data instead of directly accessing request.POST

This commit is contained in:
Shohan 2021-07-22 00:11:15 +05:30 committed by Matt Westcott
parent fc280f92a3
commit d6c787fc51
3 changed files with 23 additions and 22 deletions

View File

@ -1,5 +1,4 @@
from django import forms
from django.shortcuts import get_object_or_404
from django.template.response import TemplateResponse
from django.utils.translation import gettext_lazy as _
from django.utils.translation import ngettext
@ -32,6 +31,13 @@ class MoveBulkAction(PageBulkAction):
aria_label = "Move pages"
template_name = "wagtailadmin/pages/bulk_actions/confirm_bulk_move.html"
action_priority = 10
form_class = MoveForm
destination = Page.get_first_root_node()
def get_form_kwargs(self):
ctx = super().get_form_kwargs()
ctx['destination'] = self.destination
return ctx
def check_perm(self, page):
return page.permissions_for_user(self.request.user).can_move()
@ -51,19 +57,12 @@ class MoveBulkAction(PageBulkAction):
context['child_pages'] = context['page'].get_descendants().count()
return context
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
destination = kwargs.get('destination', Page.get_first_root_node())
context['form'] = MoveForm(destination=destination)
return context
def prepare_action(self, pages):
request = self.request
move_applicable = request.POST.dict().get("move_applicable", False)
move_applicable = self.cleaned_form.cleaned_data['move_applicable']
if move_applicable:
return
destination_page_id = request.POST.dict()['chooser']
destination = get_object_or_404(Page, id=destination_page_id)
destination = self.cleaned_form.cleaned_data['chooser']
pages_without_destination_access = []
pages_with_duplicate_slugs = []
for page in pages:
@ -72,6 +71,8 @@ class MoveBulkAction(PageBulkAction):
if not Page._slug_is_available(page.slug, destination, page=page):
pages_with_duplicate_slugs.append(page)
if pages_without_destination_access or pages_with_duplicate_slugs:
# this will be picked up by the form
self.destination = destination
return TemplateResponse(request, self.template_name, {
'pages_without_destination_access': [
{'page': page, 'can_edit': page.permissions_for_user(self.request.user).can_edit()}
@ -82,12 +83,11 @@ class MoveBulkAction(PageBulkAction):
for page in pages_with_duplicate_slugs
],
'destination': destination,
**self.get_context_data(destination=destination)
**self.get_context_data()
})
def execute_action(cls, pages):
destination_page_id = cls.request.POST.dict().pop("chooser")
destination = get_object_or_404(Page, id=destination_page_id)
destination = cls.cleaned_form.cleaned_data['chooser']
for page in pages:
if not page.permissions_for_user(cls.request.user).can_move_to(destination):
@ -97,9 +97,6 @@ class MoveBulkAction(PageBulkAction):
page.move(destination, pos='last-child', user=cls.request.user)
cls.num_parent_objects += 1
def post(self, request):
return super().post(request)
@hooks.register('register_page_bulk_action')
def move(request):

View File

@ -26,12 +26,13 @@ class PublishBulkAction(PageBulkAction):
return context
def execute_action(cls, pages):
include_descendants = cls.cleaned_form.cleaned_data['include_descendants']
for page in pages:
revision = page.save_revision(user=cls.request.user)
revision.publish(user=cls.request.user)
cls.num_parent_objects += 1
if cls.include_descendants:
if include_descendants:
for draft_descendant_page in page.get_descendants().not_live().defer_streamfields().specific():
if draft_descendant_page.permissions_for_user(cls.request.user).can_publish():
revision = draft_descendant_page.save_revision(user=cls.request.user)
@ -39,8 +40,9 @@ class PublishBulkAction(PageBulkAction):
cls.num_child_objects += 1
def get_success_message(self):
include_descendants = self.cleaned_form.cleaned_data['include_descendants']
if self.num_parent_objects == 1:
if self.include_descendants:
if include_descendants:
if self.num_child_objects == 0:
success_message = _("1 page has been published")
else:
@ -54,7 +56,7 @@ class PublishBulkAction(PageBulkAction):
else:
success_message = _("1 page has been published")
else:
if self.include_descendants:
if include_descendants:
if self.num_child_objects == 0:
success_message = _("%(num_parent_objects)d pages have been published") % {'num_parent_objects': self.num_parent_objects}
else:

View File

@ -27,19 +27,21 @@ class UnpublishBulkAction(PageBulkAction):
return context
def execute_action(cls, pages):
include_descendants = cls.cleaned_form.cleaned_data['include_descendants']
for page in pages:
page.unpublish(user=cls.request.user)
cls.num_parent_objects += 1
if cls.include_descendants:
if include_descendants:
for live_descendant_page in page.get_descendants().live().defer_streamfields().specific():
if cls.check_perm(live_descendant_page):
live_descendant_page.unpublish()
cls.num_child_objects += 1
def get_success_message(self):
include_descendants = self.cleaned_form.cleaned_data['include_descendants']
if self.num_parent_objects == 1:
if self.include_descendants:
if include_descendants:
if self.num_child_objects == 0:
success_message = _("1 page has been unpublished")
else:
@ -53,7 +55,7 @@ class UnpublishBulkAction(PageBulkAction):
else:
success_message = _("1 page has been unpublished")
else:
if self.include_descendants:
if include_descendants:
if self.num_child_objects == 0:
success_message = _("%(num_parent_objects)d pages have been unpublished") % {'num_parent_objects': self.num_parent_objects}
else: