Browse Source

Use separate results template for PageReportView

While in theory it should work, we haven't actually added tests to ensure
the tables framework can be used in ReportView.
Sage Abdullah 10 months ago
parent
commit
e3dfcc099c

+ 36 - 15
docs/extending/adding_reports.md

@@ -46,12 +46,29 @@ class UnpublishedChangesReportView(PageReportView):
 
 (string)
 
-The template used to render your report. For ``ReportView``, this defaults to ``"wagtailadmin/reports/base_report.html"``,
-which provides an empty report page layout; for ``PageReportView``, this defaults to
-``"wagtailadmin/reports/base_page_report.html"`` which provides a listing based on the explorer views,
+The template used to render your report view.
+For ``ReportView``, this defaults to ``"wagtailadmin/reports/base_report.html"``;
+for ``PageReportView``, this defaults to ``"wagtailadmin/reports/base_page_report.html"``.
+Note that this template only provides the skeleton of the view, not the listing table itself.
+The listing table should be implemented in a separate template specified by ``results_template_name`` (see below), to then be rendered via ``{% include %}``.
+Unless you want to customize the overall view, you will rarely need to change this template.
+To customize the listing, change the ``results_template_name`` instead.
+
+.. attribute:: results_template_name
+
+(string)
+
+The template used to render the listing table.
+For ``ReportView``, this defaults to ``"wagtailadmin/reports/base_report_results.html"``,
+which provides support for using the ``wagtail.admin.ui.tables`` framework.
+For ``PageReportView``, this defaults to ``"wagtailadmin/reports/base_page_report_results.html"``,
+which provides a default table layout based on the explorer views,
 displaying action buttons, as well as the title, time of the last update, status, and specific type of any pages.
 In this example, we'll change this to a new template in a later section.
 
+.. versionadded:: 6.2
+   The ``results_template_name`` attribute was added to support the use of the ``wagtail.admin.ui.tables`` framework.
+
 .. attribute:: title
 
 (string)
@@ -115,20 +132,24 @@ preprocessing, set the preprocessing_function to ``None``.
 
 ```
 
-## Customising templates
+## Customizing templates
+
+For this example \"pages with unpublished changes\" report, we'll add an extra column to the listing template, showing the last publication date for each page. To do this, we'll extend two templates: `wagtailadmin/reports/base_page_report_results.html`, and `wagtailadmin/reports/listing/_list_page_report.html`.
 
-For this example \"pages with unpublished changes\" report, we'll add an extra column to the listing template, showing the last publication date for each page. To do this, we'll extend two templates: `wagtailadmin/reports/base_page_report.html`, and `wagtailadmin/reports/listing/_list_page_report.html`.
+```{versionchanged} 6.2
+Extending `wagtailadmin/reports/base_page_report.html` was changed in favor of extending `wagtailadmin/reports/base_page_report_results.html`. The `listing` and `no_results` blocks were renamed to `results` and `no_results_message`, respectively.
+```
 
 ```html+django
-{# <project>/templates/reports/unpublished_changes_report.html #}
+{# <project>/templates/reports/unpublished_changes_report_results.html #}
 
-{% extends 'wagtailadmin/reports/base_page_report.html' %}
+{% extends 'wagtailadmin/reports/base_page_report_results.html' %}
 
-{% block listing %}
+{% block results %}
     {% include 'reports/include/_list_unpublished_changes.html' %}
 {% endblock %}
 
-{% block no_results %}
+{% block no_results_message %}
     <p>No pages with unpublished changes.</p>
 {% endblock %}
 ```
@@ -149,7 +170,7 @@ For this example \"pages with unpublished changes\" report, we'll add an extra c
 {% endblock %}
 ```
 
-Finally, we'll set `UnpublishedChangesReportView.template_name` to this new template: `'reports/unpublished_changes_report.html'`.
+Finally, we'll set `UnpublishedChangesReportView.results_template_name` to this new template: `'reports/unpublished_changes_report_results.html'`.
 
 ## Adding a menu item and admin URL
 
@@ -203,7 +224,7 @@ from wagtail.models import Page
 class UnpublishedChangesReportView(PageReportView):
 
     header_icon = 'doc-empty-inverse'
-    template_name = 'reports/unpublished_changes_report.html'
+    results_template_name = 'reports/unpublished_changes_report_results.html'
     title = "Pages with unpublished changes"
 
     list_export = PageReportView.list_export + ['last_published_at']
@@ -240,15 +261,15 @@ def register_unpublished_changes_report_url():
 ```
 
 ```html+django
-{# <project>/templates/reports/unpublished_changes_report.html #}
+{# <project>/templates/reports/unpublished_changes_report_results.html #}
 
-{% extends 'wagtailadmin/reports/base_page_report.html' %}
+{% extends 'wagtailadmin/reports/base_page_report_results.html' %}
 
-{% block listing %}
+{% block results %}
     {% include 'reports/include/_list_unpublished_changes.html' %}
 {% endblock %}
 
-{% block no_results %}
+{% block no_results_message %}
     <p>No pages with unpublished changes.</p>
 {% endblock %}
 ```

+ 3 - 3
wagtail/admin/templates/wagtailadmin/reports/aging_pages.html → wagtail/admin/templates/wagtailadmin/reports/aging_pages_results.html

@@ -1,7 +1,7 @@
-{% extends 'wagtailadmin/reports/base_page_report.html' %}
+{% extends 'wagtailadmin/reports/base_page_report_results.html' %}
 {% load i18n wagtailadmin_tags %}
 
-{% block listing %}
+{% block results %}
     <table class="listing">
         <thead>
             <tr>
@@ -49,6 +49,6 @@
     </table>
 {% endblock %}
 
-{% block no_results %}
+{% block no_results_message %}
     <p>{% trans "No pages found." %}</p>
 {% endblock %}

+ 3 - 15
wagtail/admin/templates/wagtailadmin/reports/base_page_report.html

@@ -1,19 +1,7 @@
 {% extends 'wagtailadmin/reports/base_report.html' %}
-{% load i18n wagtailadmin_tags %}
+{% load i18n %}
 
 {% block results %}
-    {% with page_obj as pages %}
-        <div id="page-results">
-            {% if pages %}
-                {% block listing %}
-                    {% include "wagtailadmin/reports/listing/_list_page_report.html" %}
-                {% endblock %}
-                {% paginate pages base_url=request.path %}
-            {% else %}
-                {% block no_results %}
-                    <p>{% trans "No pages match this report's criteria." %}</p>
-                {% endblock %}
-            {% endif %}
-        </div>
-    {% endwith %}
+    {# Rename the paginator `page_obj` to `pages` for backwards-compatibility #}
+    {% include view.results_template_name|default:"wagtailadmin/reports/base_page_report_results.html" with pages=page_obj %}
 {% endblock %}

+ 10 - 0
wagtail/admin/templates/wagtailadmin/reports/base_page_report_results.html

@@ -0,0 +1,10 @@
+{% extends 'wagtailadmin/reports/base_report_results.html' %}
+{% load i18n %}
+
+{% block results %}
+    {% include "wagtailadmin/reports/listing/_list_page_report.html" %}
+{% endblock %}
+
+{% block no_results_message %}
+    <p>{% trans "No pages match this report's criteria." %}</p>
+{% endblock %}

+ 3 - 3
wagtail/admin/templates/wagtailadmin/reports/locked_pages.html → wagtail/admin/templates/wagtailadmin/reports/locked_pages_results.html

@@ -1,9 +1,9 @@
-{% extends 'wagtailadmin/reports/base_page_report.html' %}
+{% extends 'wagtailadmin/reports/base_page_report_results.html' %}
 {% load i18n %}
-{% block listing %}
+{% block results %}
     {% include "wagtailadmin/reports/listing/_list_unlock.html" %}
 {% endblock %}
 
-{% block no_results %}
+{% block no_results_message %}
     <p>{% trans "No locked pages found." %}</p>
 {% endblock %}

+ 20 - 4
wagtail/admin/tests/test_reports_views.py

@@ -43,7 +43,11 @@ class TestLockedPagesView(WagtailTestUtils, TestCase):
     def test_simple(self):
         response = self.get()
         self.assertEqual(response.status_code, 200)
-        self.assertTemplateUsed(response, "wagtailadmin/reports/locked_pages.html")
+        self.assertTemplateUsed(response, "wagtailadmin/reports/base_page_report.html")
+        self.assertTemplateUsed(
+            response,
+            "wagtailadmin/reports/locked_pages_results.html",
+        )
 
         # Initially there should be no locked pages
         self.assertContains(response, "No locked pages found.")
@@ -79,7 +83,11 @@ class TestLockedPagesView(WagtailTestUtils, TestCase):
         # Now the listing should contain our locked page
         response = self.get()
         self.assertEqual(response.status_code, 200)
-        self.assertTemplateUsed(response, "wagtailadmin/reports/locked_pages.html")
+        self.assertTemplateUsed(response, "wagtailadmin/reports/base_page_report.html")
+        self.assertTemplateUsed(
+            response,
+            "wagtailadmin/reports/locked_pages_results.html",
+        )
         self.assertNotContains(response, "No locked pages found.")
         self.assertContains(response, "First locked page")
         self.assertContains(response, "Second locked page")
@@ -119,7 +127,11 @@ class TestLockedPagesView(WagtailTestUtils, TestCase):
         response = self.get()
 
         self.assertEqual(response.status_code, 200)
-        self.assertTemplateUsed(response, "wagtailadmin/reports/locked_pages.html")
+        self.assertTemplateUsed(response, "wagtailadmin/reports/base_page_report.html")
+        self.assertTemplateUsed(
+            response,
+            "wagtailadmin/reports/locked_pages_results.html",
+        )
         self.assertContains(response, "No locked pages found.")
 
     def test_get_with_no_permissions(self):
@@ -538,7 +550,11 @@ class TestAgingPagesView(WagtailTestUtils, TestCase):
     def test_simple(self):
         response = self.get()
         self.assertEqual(response.status_code, 200)
-        self.assertTemplateUsed(response, "wagtailadmin/reports/aging_pages.html")
+        self.assertTemplateUsed(response, "wagtailadmin/reports/base_page_report.html")
+        self.assertTemplateUsed(
+            response,
+            "wagtailadmin/reports/aging_pages_results.html",
+        )
 
     def test_displays_only_published_pages(self):
         response = self.get()

+ 1 - 1
wagtail/admin/views/reports/aging_pages.py

@@ -29,7 +29,7 @@ class AgingPagesReportFilterSet(WagtailFilterSet):
 
 
 class AgingPagesView(PageReportView):
-    template_name = "wagtailadmin/reports/aging_pages.html"
+    results_template_name = "wagtailadmin/reports/aging_pages_results.html"
     title = _("Aging pages")
     header_icon = "time"
     filterset_class = AgingPagesReportFilterSet

+ 1 - 0
wagtail/admin/views/reports/base.py

@@ -48,6 +48,7 @@ class ReportView(SpreadsheetExportMixin, BaseListingView):
 
 class PageReportView(ReportView):
     template_name = "wagtailadmin/reports/base_page_report.html"
+    results_template_name = "wagtailadmin/reports/base_page_report_results.html"
     export_headings = {
         "latest_revision_created_at": _("Updated"),
         "status_string": _("Status"),

+ 1 - 1
wagtail/admin/views/reports/locked_pages.py

@@ -34,7 +34,7 @@ class LockedPagesReportFilterSet(WagtailFilterSet):
 
 
 class LockedPagesView(PageReportView):
-    template_name = "wagtailadmin/reports/locked_pages.html"
+    results_template_name = "wagtailadmin/reports/locked_pages_results.html"
     title = _("Locked pages")
     header_icon = "lock"
     list_export = PageReportView.list_export + [