Ensure tags menu item works when the list page doesn't exist
This commit is contained in:
parent
85afe6c41b
commit
675d3f38b9
1 changed files with 24 additions and 10 deletions
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue