From 92510e7f1376dd955146c402d85a3122e06ac1fc Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Thu, 8 Jun 2023 16:25:07 +0100 Subject: [PATCH] Cache explorable root page on user object via PagePermissionPolicy --- wagtail/admin/tests/pages/test_edit_page.py | 2 +- wagtail/admin/wagtail_hooks.py | 2 +- wagtail/images/tests/test_admin_views.py | 6 +++--- wagtail/permission_policies/pages.py | 6 ++++++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/wagtail/admin/tests/pages/test_edit_page.py b/wagtail/admin/tests/pages/test_edit_page.py index 18b2aa984b..c67a537c22 100644 --- a/wagtail/admin/tests/pages/test_edit_page.py +++ b/wagtail/admin/tests/pages/test_edit_page.py @@ -1876,7 +1876,7 @@ class TestPageEdit(WagtailTestUtils, TestCase): # as when running it within the full test suite self.client.get(reverse("wagtailadmin_pages:edit", args=(self.event_page.id,))) - with self.assertNumQueries(39): + with self.assertNumQueries(35): self.client.get( reverse("wagtailadmin_pages:edit", args=(self.event_page.id,)) ) diff --git a/wagtail/admin/wagtail_hooks.py b/wagtail/admin/wagtail_hooks.py index a11498fe2b..62e22b30a4 100644 --- a/wagtail/admin/wagtail_hooks.py +++ b/wagtail/admin/wagtail_hooks.py @@ -918,7 +918,7 @@ class WorkflowReportMenuItem(MenuItem): class SiteHistoryReportMenuItem(MenuItem): def is_shown(self, request): - return UserPagePermissionsProxy(request.user).explorable_pages().exists() + return get_explorable_root_page(request.user) is not None class AgingPagesReportMenuItem(MenuItem): diff --git a/wagtail/images/tests/test_admin_views.py b/wagtail/images/tests/test_admin_views.py index febf9d5a57..75ae72d485 100644 --- a/wagtail/images/tests/test_admin_views.py +++ b/wagtail/images/tests/test_admin_views.py @@ -316,7 +316,7 @@ class TestImageIndexView(WagtailTestUtils, TestCase): self.get() # Initial number of queries. - with self.assertNumQueries(14): + with self.assertNumQueries(12): self.get() # Add 5 images. @@ -326,11 +326,11 @@ class TestImageIndexView(WagtailTestUtils, TestCase): file=get_test_image_file(size=(1, 1)), ) - with self.assertNumQueries(34): + with self.assertNumQueries(32): # The renditions needed don't exist yet. We have 20 = 5 * 4 additional queries. self.get() - with self.assertNumQueries(14): + with self.assertNumQueries(12): # No extra additional queries since renditions exist and are saved in # the prefetched objects cache. self.get() diff --git a/wagtail/permission_policies/pages.py b/wagtail/permission_policies/pages.py index f8cbd6172b..9ec152e199 100644 --- a/wagtail/permission_policies/pages.py +++ b/wagtail/permission_policies/pages.py @@ -7,6 +7,7 @@ from wagtail.permission_policies.base import BasePermissionPolicy class PagePermissionPolicy(BasePermissionPolicy): permission_cache_name = "_page_permission_cache" + _explorable_root_instance_cache_name = "_explorable_root_page_cache" def __init__(self, model=Page): super().__init__(model=model) @@ -163,6 +164,10 @@ class PagePermissionPolicy(BasePermissionPolicy): ] def explorable_root_instance(self, user): + # This method is used all around the admin via get_explorable_root_page, + # so cache the result on the user for the duration of the request + if hasattr(user, self._explorable_root_instance_cache_name): + return getattr(user, self._explorable_root_instance_cache_name) pages = self.instances_with_direct_explore_permission(user) try: root_page = Page.objects.first_common_ancestor_of( @@ -170,6 +175,7 @@ class PagePermissionPolicy(BasePermissionPolicy): ) except Page.DoesNotExist: root_page = None + setattr(user, self._explorable_root_instance_cache_name, root_page) return root_page def explorable_instances(self, user):