Преглед изворни кода

Show parent page on search results further down the tree

Matt Westcott пре 1 година
родитељ
комит
da90f83f94

+ 5 - 0
wagtail/admin/templates/wagtailadmin/pages/listing/_page_title_cell.html

@@ -1,4 +1,9 @@
 {% load l10n %}
 <td id="page_{{ instance.pk|unlocalize }}_title" {% if column.classname %}class="{{ column.classname }}"{% endif %} data-listing-page-title>
     {% include "wagtailadmin/pages/listing/_page_title_explore.html" with page=instance show_locale_labels=show_locale_labels %}
+    {% if parent_page %}
+        <div>
+            <a href="{% url 'wagtailadmin_explore' parent_page.id %}" class="icon icon-arrow-right">{{ parent_page.get_admin_display_title }}</a>
+        </div>
+    {% endif %}
 </td>

+ 6 - 0
wagtail/admin/tests/pages/test_explorer_view.py

@@ -422,6 +422,12 @@ class TestPageExplorer(WagtailTestUtils, TestCase):
         self.assertEqual(response.status_code, 200)
         page_ids = [page.id for page in response.context["pages"]]
         self.assertEqual(page_ids, [self.old_page.id])
+        # Results that are not immediate children of the current page should show their parent
+        self.assertContains(
+            response,
+            '<a href="/admin/pages/2/" class="icon icon-arrow-right">Welcome to your new Wagtail site!</a>',
+            html=True,
+        )
 
         # search results should not include pages outside parent_page's descendants
         response = self.client.get(

+ 1 - 0
wagtail/admin/ui/tables/pages.py

@@ -24,6 +24,7 @@ class PageTitleColumn(BaseColumn):
         context["page_perms"] = instance.permissions_for_user(
             parent_context["request"].user
         )
+        context["parent_page"] = getattr(instance, "annotated_parent_page", None)
         context["show_locale_labels"] = parent_context.get("show_locale_labels")
         context["perms"] = parent_context.get("perms")
         return context

+ 15 - 0
wagtail/admin/views/pages/listing.py

@@ -250,6 +250,21 @@ class BaseIndexView(PermissionCheckedMixin, BaseListingView):
 
         context = super().get_context_data(**kwargs)
 
+        if self.is_searching:
+            # postprocess this page of results to annotate each result with its parent page
+            parent_page_paths = {
+                page.path[: -page.steplen] for page in context["object_list"]
+            }
+            parent_pages_by_path = {
+                page.path: page
+                for page in Page.objects.filter(path__in=parent_page_paths).specific()
+            }
+            for page in context["object_list"]:
+                parent_page = parent_pages_by_path.get(page.path[: -page.steplen])
+                # add annotation if parent page is found and is not the currently viewed parent
+                if parent_page and parent_page != self.parent_page:
+                    page.annotated_parent_page = parent_page
+
         context.update(
             {
                 "parent_page": self.parent_page,