Show number of blog posts on admin dashboard

This commit is contained in:
Jake Howard 2022-09-15 08:52:13 +01:00
parent f3b248110e
commit 77e34cbf02
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 30 additions and 1 deletions

View file

@ -0,0 +1,8 @@
{% load wagtailcore_tags wagtailadmin_tags %}
<li>
{% icon name="edit" %}
<a href="{% pageurl list_page %}">
<span>{{ posts }}</span> Posts
</a>
</li>

View file

@ -2,8 +2,9 @@ from django.http.request import HttpRequest
from django.urls import reverse from django.urls import reverse
from wagtail import hooks from wagtail import hooks
from wagtail.admin.menu import MenuItem from wagtail.admin.menu import MenuItem
from wagtail.admin.site_summary import SummaryItem
from .models import BlogPostTagListPage from .models import BlogPostListPage, BlogPostTagListPage
class BlogPostTagsMenuItem(MenuItem): class BlogPostTagsMenuItem(MenuItem):
@ -26,6 +27,26 @@ class BlogPostTagsMenuItem(MenuItem):
return bool(self.url) return bool(self.url)
class BlogPostCountSummaryItem(SummaryItem):
template_name = "blog/blog_post_count_summary_item.html"
def is_shown(self) -> bool:
return BlogPostListPage.objects.exists()
def get_context_data(self, parent_context: dict) -> dict:
context = super().get_context_data(parent_context)
list_page = BlogPostListPage.objects.get()
context["list_page"] = list_page
context["posts"] = list_page.get_listing_pages().count()
return context
@hooks.register("register_admin_menu_item") @hooks.register("register_admin_menu_item")
def register_blog_post_tags_menu_item() -> MenuItem: def register_blog_post_tags_menu_item() -> MenuItem:
return BlogPostTagsMenuItem() return BlogPostTagsMenuItem()
@hooks.register("construct_homepage_summary_items")
def register_blog_post_count_summary_item(request: HttpRequest, panels: list) -> None:
panels.append(BlogPostCountSummaryItem(request))