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

Cache explorable root page on user object via PagePermissionPolicy

This commit is contained in:
Sage Abdullah 2023-06-08 16:25:07 +01:00
parent bacba73b93
commit 92510e7f13
No known key found for this signature in database
GPG Key ID: EB1A33CC51CC0217
4 changed files with 11 additions and 5 deletions

View File

@ -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,))
)

View File

@ -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):

View File

@ -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()

View File

@ -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):