diff --git a/wagtail/admin/ui/side_panels.py b/wagtail/admin/ui/side_panels.py index f73b3075ac..08236ffbe8 100644 --- a/wagtail/admin/ui/side_panels.py +++ b/wagtail/admin/ui/side_panels.py @@ -70,6 +70,7 @@ class BaseStatusSidePanel(BaseSidePanel): scheduled_object=None, locale=None, translations=None, + usage_url=None, **kwargs, ): super().__init__(*args, **kwargs) @@ -78,6 +79,7 @@ class BaseStatusSidePanel(BaseSidePanel): self.scheduled_object = scheduled_object self.locale = locale self.translations = translations + self.usage_url = usage_url self.locking_enabled = isinstance(self.object, LockableMixin) def get_status_templates(self, context): @@ -94,9 +96,10 @@ class BaseStatusSidePanel(BaseSidePanel): "wagtailadmin/shared/side_panels/includes/status/locked.html" ) - templates.append( - "wagtailadmin/shared/side_panels/includes/status/usage.html" - ) + if self.usage_url: + templates.append( + "wagtailadmin/shared/side_panels/includes/status/usage.html" + ) return templates @@ -201,10 +204,10 @@ class BaseStatusSidePanel(BaseSidePanel): def get_usage_context(self): return { - "usage_count": ReferenceIndex.get_references_to(self.object) - .group_by_source_object() - .count(), - "usage_url": getattr(self.object, "usage_url", None), + "usage_count": ReferenceIndex.get_grouped_references_to( + self.object + ).count(), + "usage_url": self.usage_url, } def get_context_data(self, parent_context): @@ -224,6 +227,11 @@ class BaseStatusSidePanel(BaseSidePanel): class PageStatusSidePanel(BaseStatusSidePanel): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + if self.object.pk: + self.usage_url = reverse("wagtailadmin_pages:usage", args=(self.object.pk,)) + def get_status_templates(self, context): templates = super().get_status_templates(context) templates.insert( @@ -233,9 +241,6 @@ class PageStatusSidePanel(BaseStatusSidePanel): def get_usage_context(self): context = super().get_usage_context() - context["usage_url"] = reverse( - "wagtailadmin_pages:usage", args=(self.object.id,) - ) context["usage_url_text"] = ngettext( "Referenced %(count)s time", "Referenced %(count)s times", diff --git a/wagtail/snippets/side_panels.py b/wagtail/snippets/side_panels.py index 566cbaef9e..79c73757af 100644 --- a/wagtail/snippets/side_panels.py +++ b/wagtail/snippets/side_panels.py @@ -7,7 +7,6 @@ class SnippetStatusSidePanel(BaseStatusSidePanel): inherit = [ "view", "history_url", - "usage_url", "workflow_history_url", "revisions_compare_url_name", "revision_enabled", diff --git a/wagtail/snippets/views/snippets.py b/wagtail/snippets/views/snippets.py index ce1fe1cb14..f6ec880e9d 100644 --- a/wagtail/snippets/views/snippets.py +++ b/wagtail/snippets/views/snippets.py @@ -329,6 +329,7 @@ class EditView(generic.CreateEditViewOptionalFeaturesMixin, generic.EditView): else None, locale=self.locale, translations=self.translations, + usage_url=self.get_usage_url(), ) ] if self.preview_enabled and self.object.is_previewable(): @@ -352,7 +353,6 @@ class EditView(generic.CreateEditViewOptionalFeaturesMixin, generic.EditView): "model_opts": self.model._meta, "action_menu": action_menu, "side_panels": side_panels, - "usage_url": self.get_usage_url(), "revisions_compare_url_name": self.revisions_compare_url_name, "media": media, }