Ensure tags menu item works when the list page doesn't exist

This commit is contained in:
Jake Howard 2022-07-20 08:41:40 +01:00
parent 85afe6c41b
commit 675d3f38b9
Signed by: jake
GPG key ID: 57AFB45680EDD477

View file

@ -1,3 +1,4 @@
from django.http.request import HttpRequest
from django.urls import reverse
from wagtail import hooks
from wagtail.admin.menu import MenuItem
@ -5,15 +6,28 @@ from wagtail.admin.menu import MenuItem
from .models import BlogPostTagListPage
class BlogPostTagsMenuItem(MenuItem):
url: str
def __init__(self) -> None:
super().__init__("Blog post tags", url="", icon_name="tag")
def is_shown(self, request: HttpRequest) -> bool:
if not self.url:
blog_post_tag_list_id = (
BlogPostTagListPage.objects.live() # type:ignore[attr-defined]
.values_list("id", flat=True)
.first()
)
self.url = (
reverse("wagtailadmin_explore", args=[blog_post_tag_list_id])
if blog_post_tag_list_id
else ""
)
return bool(self.url)
@hooks.register("register_admin_menu_item")
def register_blog_post_tags_menu_item() -> MenuItem:
blog_post_tag_list_id = (
BlogPostTagListPage.objects.live() # type:ignore[attr-defined]
.values_list("id", flat=True)
.get()
)
return MenuItem(
"Blog post tags",
reverse("wagtailadmin_explore", args=[blog_post_tag_list_id]),
icon_name="tag",
)
return BlogPostTagsMenuItem()