0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Use Lock objects in page action menus

Removes about 20 queries
This commit is contained in:
Karl Hobley 2022-07-28 15:59:52 +00:00 committed by Matt Westcott
parent 315d9a938a
commit 40c1227edc
4 changed files with 30 additions and 17 deletions

View File

@ -44,11 +44,10 @@ class ActionMenuItem(Component):
'user_page_permissions' = a UserPagePermissionsProxy for the current user, to test permissions against
may also contain:
'user_page_permissions_tester' = a PagePermissionTester for the current user and page
'lock' = a Lock object if the page is locked, otherwise None
'locked_for_user' = True if the lock prevents the current user from editing the page
"""
return (
context["view"] == "create"
or not self.get_user_page_permissions_tester(context).page_locked()
)
return context["view"] == "create" or not context["locked_for_user"]
def get_context_data(self, parent_context):
"""Defines context for the template, overridable to use more data"""
@ -86,7 +85,7 @@ class PublishMenuItem(ActionMenuItem):
)
else: # view == 'edit' or 'revisions_revert'
perms_tester = self.get_user_page_permissions_tester(context)
return not perms_tester.page_locked() and perms_tester.can_publish()
return not context["locked_for_user"] and perms_tester.can_publish()
def get_context_data(self, parent_context):
context = super().get_context_data(parent_context)
@ -110,7 +109,7 @@ class SubmitForModerationMenuItem(ActionMenuItem):
perms_tester = self.get_user_page_permissions_tester(context)
return (
perms_tester.can_submit_for_moderation()
and not perms_tester.page_locked()
and not context["locked_for_user"]
)
# context == revisions_revert
return False
@ -154,8 +153,7 @@ class WorkflowMenuItem(ActionMenuItem):
def is_shown(self, context):
if context["view"] == "edit":
perms_tester = self.get_user_page_permissions_tester(context)
return not perms_tester.page_locked()
return not context["locked_for_user"]
class RestartWorkflowMenuItem(ActionMenuItem):
@ -172,7 +170,7 @@ class RestartWorkflowMenuItem(ActionMenuItem):
perms_tester = self.get_user_page_permissions_tester(context)
return (
perms_tester.can_submit_for_moderation()
and not perms_tester.page_locked()
and not context["locked_for_user"]
and workflow_state
and workflow_state.user_can_cancel(context["request"].user)
)
@ -203,7 +201,7 @@ class UnpublishMenuItem(ActionMenuItem):
def is_shown(self, context):
if context["view"] == "edit":
perms_tester = self.get_user_page_permissions_tester(context)
return not perms_tester.page_locked() and perms_tester.can_unpublish()
return not context["locked_for_user"] and perms_tester.can_unpublish()
def get_url(self, context):
return reverse("wagtailadmin_pages:unpublish", args=(context["page"].id,))
@ -226,10 +224,7 @@ class PageLockedMenuItem(ActionMenuItem):
template_name = "wagtailadmin/pages/action_menu/page_locked.html"
def is_shown(self, context):
return (
"page" in context
and self.get_user_page_permissions_tester(context).page_locked()
)
return "page" in context and context["locked_for_user"]
def get_context_data(self, parent_context):
context = super().get_context_data(parent_context)

View File

@ -328,7 +328,11 @@ class CreateView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View):
request=self.request, instance=self.page, form=self.form
)
action_menu = PageActionMenu(
self.request, view="create", parent_page=self.parent_page
self.request,
view="create",
parent_page=self.parent_page,
lock=None,
locked_for_user=False,
)
side_panels = PageSidePanels(
self.request,

View File

@ -815,7 +815,13 @@ class EditView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View):
bound_panel = self.edit_handler.get_bound_panel(
instance=self.page, request=self.request, form=self.form
)
action_menu = PageActionMenu(self.request, view="edit", page=self.page)
action_menu = PageActionMenu(
self.request,
view="edit",
page=self.page,
lock=self.lock,
locked_for_user=self.locked_for_user,
)
side_panels = PageSidePanels(
self.request,
self.page_for_status,

View File

@ -43,7 +43,15 @@ def revisions_revert(request, page_id, revision_id):
instance=revision_page, request=request, form=form
)
action_menu = PageActionMenu(request, view="revisions_revert", page=page)
lock = page.get_lock()
action_menu = PageActionMenu(
request,
view="revisions_revert",
page=page,
lock=lock,
locked_for_user=lock is not None and lock.for_user(request.user),
)
side_panels = PageSidePanels(
request,
page,