From d7e6382d97b2ced69e767945bebf2100bd1c1090 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Wed, 8 Dec 2021 21:47:16 +0000 Subject: [PATCH] Use wagtail.admin.ui.tables framework for snippet chooser --- .../wagtailsnippets/chooser/results.html | 20 ++------------- wagtail/snippets/views/chooser.py | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/wagtail/snippets/templates/wagtailsnippets/chooser/results.html b/wagtail/snippets/templates/wagtailsnippets/chooser/results.html index 1c13d06da1..1e1afdb26d 100644 --- a/wagtail/snippets/templates/wagtailsnippets/chooser/results.html +++ b/wagtail/snippets/templates/wagtailsnippets/chooser/results.html @@ -1,4 +1,4 @@ -{% load i18n wagtailadmin_tags wagtailsnippets_admin_tags %} +{% load i18n wagtailadmin_tags %} {% if items %} {% if is_searching %}

@@ -10,23 +10,7 @@

{% endif %} - - - - - - - - - {% for snippet in items %} - - - - {% endfor %} - -
{% trans "Title" %}
- -
+ {% component table %} {% url 'wagtailsnippets:choose_results' model_opts.app_label model_opts.model_name as pagination_base_url %} {% include "wagtailadmin/shared/pagination_nav.html" with items=items linkurl=pagination_base_url %} diff --git a/wagtail/snippets/views/chooser.py b/wagtail/snippets/views/chooser.py index 4bbf931fde..b7f616cbac 100644 --- a/wagtail/snippets/views/chooser.py +++ b/wagtail/snippets/views/chooser.py @@ -8,12 +8,31 @@ from django.views.generic.base import View from wagtail.admin.forms.search import SearchForm from wagtail.admin.modal_workflow import render_modal_workflow +from wagtail.admin.ui.tables import Table, TitleColumn from wagtail.core.models import Locale, TranslatableMixin from wagtail.search.backends import get_search_backend from wagtail.search.index import class_is_indexed from wagtail.snippets.views.snippets import get_snippet_model_from_url_params +class ResultsTable(Table): + header_row_classname = 'table-headers' + + +class SnippetTitleColumn(TitleColumn): + def __init__(self, name, model, **kwargs): + self.model_opts = model._meta + super().__init__(name, **kwargs) + + def get_value(self, instance): + return str(instance) + + def get_link_url(self, instance, parent_context): + return reverse( + 'wagtailsnippets:chosen', args=[self.model_opts.app_label, self.model_opts.model_name, quote(instance.pk)] + ) + + class BaseChooseView(View): def get(self, request, app_label, model_name): self.model = get_snippet_model_from_url_params(app_label, model_name) @@ -68,6 +87,10 @@ class BaseChooseView(View): paginator = Paginator(items, per_page=25) self.paginated_items = paginator.get_page(request.GET.get('p')) + self.table = ResultsTable([ + SnippetTitleColumn('title', self.model, label=_('Title'), link_classname='snippet-choice'), + ], self.paginated_items) + return self.render_to_response() def render_to_response(self): @@ -83,6 +106,7 @@ class ChooseView(BaseChooseView): { 'model_opts': self.model._meta, 'items': self.paginated_items, + 'table': self.table, 'is_searchable': self.is_searchable, 'search_form': self.search_form, 'query_string': self.search_query, @@ -101,6 +125,7 @@ class ChooseResultsView(BaseChooseView): return TemplateResponse(self.request, "wagtailsnippets/chooser/results.html", { 'model_opts': self.model._meta, 'items': self.paginated_items, + 'table': self.table, 'query_string': self.search_query, 'is_searching': self.is_searching, })