Skip to content

Commit

Permalink
Use fewer db queries when checking current_workflow_task
Browse files Browse the repository at this point in the history
  • Loading branch information
dest81 authored and zerolab committed Apr 19, 2021
1 parent 00aea17 commit 3bf8d12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
5 changes: 4 additions & 1 deletion wagtail/admin/action_menu.py
Expand Up @@ -38,7 +38,10 @@ def is_shown(self, request, context):
'parent_page' (if view = 'create') = the parent page of the page being created
'user_page_permissions' = a UserPagePermissionsProxy for the current user, to test permissions against
"""
return (context['view'] == 'create' or not context['user_page_permissions'].for_page(context['page']).page_locked())
return (
context['view'] == 'create'
or not self.get_user_page_permissions_tester(context).page_locked()
)

def get_context(self, request, parent_context):
"""Defines context for the template, overridable to use more data"""
Expand Down
6 changes: 3 additions & 3 deletions wagtail/admin/views/pages/workflow.py
Expand Up @@ -143,12 +143,12 @@ def confirm_workflow_cancellation(request, page_id):
@user_passes_test(user_has_any_page_permission)
def workflow_status(request, page_id):
page = get_object_or_404(Page, id=page_id)

if not page.current_workflow_state:
current_workflow_state = page.current_workflow_state
if not current_workflow_state:
raise PermissionDenied

workflow_tasks = []
workflow_state = page.current_workflow_state
workflow_state = current_workflow_state
if not workflow_state:
# Show last workflow state
workflow_state = page.workflow_states.order_by('created_at').last()
Expand Down
33 changes: 19 additions & 14 deletions wagtail/core/models.py
Expand Up @@ -2842,21 +2842,23 @@ def workflow_in_progress(self):
def current_workflow_state(self):
"""Returns the in progress or needs changes workflow state on this page, if it exists"""
try:
return WorkflowState.objects.active().get(page=self)
return WorkflowState.objects.active().select_related("current_task_state__task").get(page=self)
except WorkflowState.DoesNotExist:
return

@property
def current_workflow_task_state(self):
"""Returns (specific class of) the current task state of the workflow on this page, if it exists"""
if self.current_workflow_state and self.current_workflow_state.status == WorkflowState.STATUS_IN_PROGRESS and self.current_workflow_state.current_task_state:
return self.current_workflow_state.current_task_state.specific
current_workflow_state = self.current_workflow_state
if current_workflow_state and current_workflow_state.status == WorkflowState.STATUS_IN_PROGRESS and current_workflow_state.current_task_state:
return current_workflow_state.current_task_state.specific

@property
def current_workflow_task(self):
"""Returns (specific class of) the current task in progress on this page, if it exists"""
if self.current_workflow_task_state:
return self.current_workflow_task_state.task.specific
current_workflow_task_state = self.current_workflow_task_state
if current_workflow_task_state:
return current_workflow_task_state.task.specific

class Meta:
verbose_name = _('page')
Expand Down Expand Up @@ -3314,8 +3316,9 @@ def user_has_lock(self):
return self.page.locked_by_id == self.user.pk

def page_locked(self):
if self.page.current_workflow_task:
if self.page.current_workflow_task.page_locked_for_user(self.page, self.user):
current_workflow_task = self.page.current_workflow_task
if current_workflow_task:
if current_workflow_task.page_locked_for_user(self.page, self.user):
return True

if not self.page.locked:
Expand Down Expand Up @@ -3353,8 +3356,9 @@ def can_edit(self):
if 'add' in self.permissions and self.page.owner_id == self.user.pk:
return True

if self.page.current_workflow_task:
if self.page.current_workflow_task.user_can_access_editor(self.page, self.user):
current_workflow_task = self.page.current_workflow_task
if current_workflow_task:
if current_workflow_task.user_can_access_editor(self.page, self.user):
return True

return False
Expand Down Expand Up @@ -3428,9 +3432,9 @@ def can_unschedule(self):
def can_lock(self):
if self.user.is_superuser:
return True

if self.page.current_workflow_task:
return self.page.current_workflow_task.user_can_lock(self.page, self.user)
current_workflow_task = self.page.current_workflow_task
if current_workflow_task:
return current_workflow_task.user_can_lock(self.page, self.user)

if 'lock' in self.permissions:
return True
Expand All @@ -3444,8 +3448,9 @@ def can_unlock(self):
if self.user_has_lock():
return True

if self.page.current_workflow_task:
return self.page.current_workflow_task.user_can_unlock(self.page, self.user)
current_workflow_task = self.page.current_workflow_task
if current_workflow_task:
return current_workflow_task.user_can_unlock(self.page, self.user)

if 'unlock' in self.permissions:
return True
Expand Down

0 comments on commit 3bf8d12

Please sign in to comment.