From 5aa60e672221c357066c26d464a529633e0387ee Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 26 Nov 2019 14:25:52 +0000 Subject: [PATCH] Add user_has_lock to PagePermissionTester --- wagtail/admin/views/pages.py | 12 ++++-------- wagtail/core/models.py | 5 ++++- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/wagtail/admin/views/pages.py b/wagtail/admin/views/pages.py index d0eb5aeef6..c30ca7fa3d 100644 --- a/wagtail/admin/views/pages.py +++ b/wagtail/admin/views/pages.py @@ -343,12 +343,9 @@ def edit(request, page_id): edit_handler = edit_handler.bind_to(instance=page, request=request) form_class = edit_handler.get_form_class() - user_has_lock = page.locked_by_id == request.user.id - page_locked = page_perms.page_locked() - - if user_has_lock: + if page_perms.user_has_lock(): messages.warning(request, format_html(_("Page '{}' was locked by you on {}."), page.get_admin_display_title(), page.locked_at.strftime("%d %b %Y %H:%M")), extra_tags='lock') - if page_locked: + if page_perms.page_locked(): locked_by_message = "" if page.locked_by: locked_by_message = format_html(" by {}", str(page.locked_by)) @@ -362,7 +359,7 @@ def edit(request, page_id): form = form_class(request.POST, request.FILES, instance=page, parent_page=parent) - if form.is_valid() and not page_locked: + if form.is_valid() and not page_perms.page_locked(): page = form.save(commit=False) is_publishing = bool(request.POST.get('action-publish')) and page_perms.can_publish() @@ -506,7 +503,7 @@ def edit(request, page_id): target_url += '?next=%s' % urlquote(next_url) return redirect(target_url) else: - if page_locked: + if page_perms.page_locked(): messages.error(request, _("The page could not be saved as it is locked")) else: messages.validation_error( @@ -556,7 +553,6 @@ def edit(request, page_id): 'form': form, 'next': next_url, 'has_unsaved_changes': has_unsaved_changes, - 'user_has_lock': user_has_lock, }) diff --git a/wagtail/core/models.py b/wagtail/core/models.py index c6e7dafbfd..9cbacb794d 100644 --- a/wagtail/core/models.py +++ b/wagtail/core/models.py @@ -1845,6 +1845,9 @@ class PagePermissionTester: if self.page.path.startswith(perm.page.path) ) + def user_has_lock(self): + return self.page.locked_by_id == self.user.pk + def page_locked(self): if not self.page.locked: # Page is not locked @@ -1855,7 +1858,7 @@ class PagePermissionTester: return True else: # Locked only if the current user was not the one who locked the page - return self.page.locked_by_id is not self.user.pk + return not self.user_has_lock() def can_add_subpage(self): if not self.user.is_active: