0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Use tables component in snippets listing view

This commit is contained in:
Sage Abdullah 2022-06-06 20:41:55 +07:00 committed by Karl Hobley
parent fd8d57a0ea
commit 80d15637d8
6 changed files with 26 additions and 41 deletions

View File

@ -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:

View File

@ -1,24 +1,2 @@
{% load i18n l10n wagtailadmin_tags wagtailsnippets_admin_tags %}
<table class="listing">
{% if can_delete_snippets %}<col width="5%" />{% endif %}
<col />
<thead>
<tr class="table-headers">
{% include 'wagtailadmin/bulk_actions/select_all_checkbox_cell.html' %}
<th>{% trans "Title" %}</th>
</tr>
</thead>
<tbody>
{% for snippet in items %}
<tr id="snippet-row-{{ snippet.pk }}">
{% 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" %}
<td class="title">
<div class="title-wrapper"><a href="{% url view.edit_url_name snippet.pk|admin_urlquote %}">{{ snippet.latest_revision.object_str|default:snippet }}</a></div>
<ul class="actions">
{% snippet_listing_buttons snippet %}
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% load wagtailadmin_tags %}
{% component table %}

View File

@ -1,8 +1,8 @@
{% load i18n %}
{% if items %}
{% if object_list %}
{% if is_searching or is_filtering %}
<h2 role="alert">
{% 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 %}
<p role="alert">{% blocktrans trimmed with snippet_type_name_plural=model_opts.verbose_name_plural %}Sorry, no {{ snippet_type_name_plural }} match your query{% endblocktrans %}</p>

View File

@ -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 %}
</div>
{% endblock %}

View File

@ -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)

View File

@ -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"
),