Handle old construct_wagtail_userbar signature in ChecksSidePanel

pull/12712/head
Matt Westcott 2024-12-18 00:20:10 +00:00 zatwierdzone przez Matt Westcott
rodzic 268174b781
commit 01e7291486
4 zmienionych plików z 68 dodań i 21 usunięć

Wyświetl plik

@ -1,23 +1,17 @@
from warnings import warn
from django import template
from django.template.loader import render_to_string
from django.utils import translation
from wagtail import hooks
from wagtail.admin.userbar import (
AccessibilityItem,
AddPageItem,
AdminItem,
EditPageItem,
ExplorePageItem,
)
from wagtail.coreutils import (
accepts_kwarg,
apply_userbar_hooks,
)
from wagtail.models import PAGE_TEMPLATE_VAR, Page, Revision
from wagtail.users.models import UserProfile
from wagtail.utils.deprecation import RemovedInWagtail70Warning
register = template.Library()
@ -95,16 +89,7 @@ def wagtailuserbar(context, position="bottom-right"):
AccessibilityItem(),
]
for fn in hooks.get_hooks("construct_wagtail_userbar"):
if accepts_kwarg(fn, "page"):
fn(request, items, page)
else:
warn(
"`construct_wagtail_userbar` hook functions should accept a `page` argument in third position -"
f" {fn.__module__}.{fn.__name__} needs to be updated",
category=RemovedInWagtail70Warning,
)
fn(request, items)
apply_userbar_hooks(request, items, page)
# Render the items
rendered_items = [item.render(request) for item in items]

Wyświetl plik

@ -492,6 +492,51 @@ class TestUserbarInPageServe(WagtailTestUtils, TestCase):
self.assertTrue(kwargs.get("called"))
class TestUserbarHooksForChecksPanel(WagtailTestUtils, TestCase):
def setUp(self):
self.user = self.login()
self.homepage = Page.objects.get(id=2).specific
def test_construct_wagtail_userbar_hook_passes_page(self):
kwargs = {}
def construct_wagtail_userbar(request, items, page):
kwargs["called"] = True
return items
with hooks.register_temporarily(
"construct_wagtail_userbar",
construct_wagtail_userbar,
):
response = self.client.get(
reverse("wagtailadmin_pages:edit", args=(self.homepage.id,))
)
self.assertEqual(response.status_code, 200)
self.assertTrue(kwargs.get("called"))
def test_deprecated_construct_wagtail_userbar_hook_without_page(self):
kwargs = {}
def construct_wagtail_userbar(request, items):
kwargs["called"] = True
return items
with self.assertWarnsMessage(
RemovedInWagtail70Warning,
"`construct_wagtail_userbar` hook functions should accept a `page` argument in third position",
), hooks.register_temporarily(
"construct_wagtail_userbar",
construct_wagtail_userbar,
):
response = self.client.get(
reverse("wagtailadmin_pages:edit", args=(self.homepage.id,))
)
self.assertEqual(response.status_code, 200)
self.assertTrue(kwargs.get("called"))
class TestUserbarAddLink(WagtailTestUtils, TestCase):
fixtures = ["test.json"]

Wyświetl plik

@ -5,9 +5,8 @@ from django.urls import reverse
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy, ngettext
from wagtail import hooks
from wagtail.admin.ui.components import Component
from wagtail.admin.userbar import AccessibilityItem
from wagtail.admin.userbar import AccessibilityItem, apply_userbar_hooks
from wagtail.models import DraftStateMixin, LockableMixin, Page, ReferenceIndex
from wagtail.utils.deprecation import RemovedInWagtail70Warning
@ -330,8 +329,7 @@ class ChecksSidePanel(BaseSidePanel):
# Retrieve the Axe configuration from the userbar.
userbar_items = [AccessibilityItem()]
page = self.object if issubclass(self.model, Page) else None
for fn in hooks.get_hooks("construct_wagtail_userbar"):
fn(self.request, userbar_items, page)
apply_userbar_hooks(self.request, userbar_items, page)
for item in userbar_items:
if isinstance(item, AccessibilityItem):

Wyświetl plik

@ -1,6 +1,12 @@
from warnings import warn
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _
from wagtail import hooks
from wagtail.coreutils import accepts_kwarg
from wagtail.utils.deprecation import RemovedInWagtail70Warning
class BaseItem:
template = "wagtailadmin/userbar/item_base.html"
@ -297,3 +303,16 @@ class EditPageItem(BaseItem):
return ""
return super().render(request)
def apply_userbar_hooks(request, items, page):
for fn in hooks.get_hooks("construct_wagtail_userbar"):
if accepts_kwarg(fn, "page"):
fn(request, items, page)
else:
warn(
"`construct_wagtail_userbar` hook functions should accept a `page` argument in third position -"
f" {fn.__module__}.{fn.__name__} needs to be updated",
category=RemovedInWagtail70Warning,
)
fn(request, items)