From 80d15637d814e19e514a919e7e30dc0651a0ec73 Mon Sep 17 00:00:00 2001 From: Sage Abdullah Date: Mon, 6 Jun 2022 20:41:55 +0700 Subject: [PATCH] Use tables component in snippets listing view --- wagtail/admin/views/generic/models.py | 2 +- .../wagtailsnippets/snippets/list.html | 26 ++----------------- .../wagtailsnippets/snippets/results.html | 6 ++--- .../wagtailsnippets/snippets/type_index.html | 2 +- wagtail/snippets/tests/test_snippets.py | 12 ++++----- wagtail/snippets/views/snippets.py | 19 +++++++++----- 6 files changed, 26 insertions(+), 41 deletions(-) diff --git a/wagtail/admin/views/generic/models.py b/wagtail/admin/views/generic/models.py index 59e638a1bc..e12f4fe258 100644 --- a/wagtail/admin/views/generic/models.py +++ b/wagtail/admin/views/generic/models.py @@ -252,7 +252,7 @@ class IndexView( def get_edit_url(self, instance): if self.edit_url_name: - return reverse(self.edit_url_name, args=(instance.pk,)) + return reverse(self.edit_url_name, args=(quote(instance.pk),)) def get_add_url(self): if self.add_url_name: diff --git a/wagtail/snippets/templates/wagtailsnippets/snippets/list.html b/wagtail/snippets/templates/wagtailsnippets/snippets/list.html index 6fe1d60567..4a11903ee3 100644 --- a/wagtail/snippets/templates/wagtailsnippets/snippets/list.html +++ b/wagtail/snippets/templates/wagtailsnippets/snippets/list.html @@ -1,24 +1,2 @@ -{% load i18n l10n wagtailadmin_tags wagtailsnippets_admin_tags %} - - {% if can_delete_snippets %}{% endif %} - - - - {% include 'wagtailadmin/bulk_actions/select_all_checkbox_cell.html' %} - - - - - {% for snippet in items %} - - {% include "wagtailadmin/bulk_actions/listing_checkbox_cell.html" with obj_type="snippet" obj=snippet aria_labelledby_prefix="snippet_" aria_labelledby=snippet.pk|unlocalize aria_labelledby_suffix="_title" %} - - - {% endfor %} - -
{% trans "Title" %}
- -
    - {% snippet_listing_buttons snippet %} -
-
+{% load wagtailadmin_tags %} +{% component table %} diff --git a/wagtail/snippets/templates/wagtailsnippets/snippets/results.html b/wagtail/snippets/templates/wagtailsnippets/snippets/results.html index e973e32377..3d45d19354 100644 --- a/wagtail/snippets/templates/wagtailsnippets/snippets/results.html +++ b/wagtail/snippets/templates/wagtailsnippets/snippets/results.html @@ -1,8 +1,8 @@ {% load i18n %} -{% if items %} +{% if object_list %} {% if is_searching or is_filtering %}

- {% blocktrans trimmed count counter=items.paginator.count %} + {% blocktrans trimmed count counter=page_obj.paginator.count %} There is {{ counter }} match {% plural %} There are {{ counter }} matches @@ -12,7 +12,7 @@ {% include "wagtailsnippets/snippets/list.html" %} - {% include "wagtailadmin/shared/pagination_nav.html" with items=items linkurl=index_url %} + {% include "wagtailadmin/shared/pagination_nav.html" with items=page_obj linkurl=index_url %} {% else %} {% if is_searching or is_filtering %}

{% blocktrans trimmed with snippet_type_name_plural=model_opts.verbose_name_plural %}Sorry, no {{ snippet_type_name_plural }} match your query{% endblocktrans %}

diff --git a/wagtail/snippets/templates/wagtailsnippets/snippets/type_index.html b/wagtail/snippets/templates/wagtailsnippets/snippets/type_index.html index a17261ea91..278d2f9def 100644 --- a/wagtail/snippets/templates/wagtailsnippets/snippets/type_index.html +++ b/wagtail/snippets/templates/wagtailsnippets/snippets/type_index.html @@ -64,6 +64,6 @@ {% include "wagtailadmin/shared/filters.html" %} {% endif %} {% trans "Select all snippets in listing" as select_all_text %} - {% include 'wagtailadmin/bulk_actions/footer.html' with select_all_obj_text=select_all_text app_label=model_opts.app_label model_name=model_opts.model_name objects=items %} + {% include 'wagtailadmin/bulk_actions/footer.html' with select_all_obj_text=select_all_text app_label=model_opts.app_label model_name=model_opts.model_name objects=page_obj %} {% endblock %} diff --git a/wagtail/snippets/tests/test_snippets.py b/wagtail/snippets/tests/test_snippets.py index 67bf118c43..b00702dfb4 100644 --- a/wagtail/snippets/tests/test_snippets.py +++ b/wagtail/snippets/tests/test_snippets.py @@ -127,7 +127,7 @@ class TestSnippetListView(TestCase, WagtailTestUtils): Advert.objects.create(pk=i, text="advert %d" % i) response = self.get() self.assertEqual(response.status_code, 200) - self.assertEqual(response.context["items"][0].text, "advert 10") + self.assertEqual(response.context["page_obj"][0].text, "advert 10") def test_simple_pagination(self): @@ -294,7 +294,7 @@ class TestModelOrdering(TestCase, WagtailTestUtils): reverse("wagtailsnippets_tests_advertwithtabbedinterface:list") ) self.assertEqual(response.status_code, 200) - self.assertEqual(response.context["items"][0].text, "aaaadvert") + self.assertEqual(response.context["page_obj"][0].text, "aaaadvert") def test_chooser_respects_model_ordering(self): response = self.client.get( @@ -325,7 +325,7 @@ class TestSnippetListViewWithSearchableSnippet(TestCase, WagtailTestUtils): self.assertTemplateUsed(response, "wagtailsnippets/snippets/type_index.html") # All snippets should be in items - items = list(response.context["items"].object_list) + items = list(response.context["page_obj"].object_list) self.assertIn(self.snippet_a, items) self.assertIn(self.snippet_b, items) self.assertIn(self.snippet_c, items) @@ -337,7 +337,7 @@ class TestSnippetListViewWithSearchableSnippet(TestCase, WagtailTestUtils): response = self.get({"q": "Hello"}) # Just snippets with "Hello" should be in items - items = list(response.context["items"].object_list) + items = list(response.context["page_obj"].object_list) self.assertIn(self.snippet_a, items) self.assertNotIn(self.snippet_b, items) self.assertIn(self.snippet_c, items) @@ -346,7 +346,7 @@ class TestSnippetListViewWithSearchableSnippet(TestCase, WagtailTestUtils): response = self.get({"q": "World"}) # Just snippets with "World" should be in items - items = list(response.context["items"].object_list) + items = list(response.context["page_obj"].object_list) self.assertNotIn(self.snippet_a, items) self.assertIn(self.snippet_b, items) self.assertIn(self.snippet_c, items) @@ -2861,7 +2861,7 @@ class TestSnippetListViewWithCustomPrimaryKey(TestCase, WagtailTestUtils): self.assertTemplateUsed(response, "wagtailsnippets/snippets/type_index.html") # All snippets should be in items - items = list(response.context["items"].object_list) + items = list(response.context["page_obj"].object_list) self.assertIn(self.snippet_a, items) self.assertIn(self.snippet_b, items) self.assertIn(self.snippet_c, items) diff --git a/wagtail/snippets/views/snippets.py b/wagtail/snippets/views/snippets.py index 042e9f6b33..70db4b28fe 100644 --- a/wagtail/snippets/views/snippets.py +++ b/wagtail/snippets/views/snippets.py @@ -18,7 +18,13 @@ from django.views.generic import TemplateView from wagtail.admin import messages from wagtail.admin.filters import DateRangePickerWidget, WagtailFilterSet from wagtail.admin.panels import get_edit_handler -from wagtail.admin.ui.tables import Column, DateColumn, InlineActionsTable, UserColumn +from wagtail.admin.ui.tables import ( + BulkActionsCheckboxColumn, + Column, + DateColumn, + InlineActionsTable, + UserColumn, +) from wagtail.admin.views.generic import CreateView, DeleteView, EditView, IndexView from wagtail.admin.views.generic.mixins import RevisionsRevertMixin from wagtail.admin.views.generic.models import RevisionsCompareView @@ -107,17 +113,18 @@ class List(IndexView): # If true, returns just the 'results' include, for use in AJAX responses from search results_only = False + def get_columns(self): + return [ + BulkActionsCheckboxColumn("checkbox", accessor=lambda obj: obj), + *super().get_columns(), + ] + def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) - # The shared admin templates expect the items to be a page object rather - # than the queryset (object_list), so we can't use context_object_name = "items". - paginated_items = context.get("page_obj") - context.update( { "model_opts": self.model._meta, - "items": paginated_items, "can_add_snippet": self.permission_policy.user_has_permission( self.request.user, "add" ),