kopia lustrzana https://github.com/wagtail/wagtail
Allow customising the number of snippets per page on the listing view
rodzic
8d2b1c68cb
commit
f533ab219d
|
@ -81,6 +81,7 @@ Viewsets are Wagtail's mechanism for defining a group of related admin views wit
|
|||
|
||||
.. autoattribute:: icon
|
||||
.. autoattribute:: list_display
|
||||
.. autoattribute:: list_per_page
|
||||
.. autoattribute:: admin_url_namespace
|
||||
.. autoattribute:: base_url_path
|
||||
.. autoattribute:: chooser_admin_url_namespace
|
||||
|
|
|
@ -573,7 +573,7 @@ The {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.admin_url_namespace`
|
|||
|
||||
Similar URL customisations are also possible for the snippet chooser views through {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.chooser_admin_url_namespace`, {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.chooser_base_url_path`, {meth}`~wagtail.snippets.views.snippets.SnippetViewSet.get_chooser_admin_url_namespace`, and {meth}`~wagtail.snippets.views.snippets.SnippetViewSet.get_chooser_admin_base_path`.
|
||||
|
||||
The {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.list_display` attribute can be set to specify the columns shown on the listing view. You can also add the ability to filter the listing view by defining a {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.filterset_class` attribute on a subclass of `SnippetViewSet`.
|
||||
The {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.list_display` attribute can be set to specify the columns shown on the listing view. To customise the number of items to be displayed per page, you can set the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.list_per_page` attribute. You can also add the ability to filter the listing view by defining a {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.filterset_class` attribute on a subclass of `SnippetViewSet`.
|
||||
|
||||
For example:
|
||||
|
||||
|
@ -588,6 +588,7 @@ from myapp.models import MemberFilterSet
|
|||
class MemberViewSet(SnippetViewSet):
|
||||
icon = "user"
|
||||
list_display = ["name", "shirt_size", "get_shirt_size_display", UpdatedAtColumn()]
|
||||
list_per_page = 50
|
||||
admin_url_namespace = "member_views"
|
||||
base_url_path = "internal/member"
|
||||
filterset_class = MemberFilterSet
|
||||
|
|
|
@ -6,7 +6,7 @@ from django.urls import reverse
|
|||
from wagtail.admin.admin_url_finder import AdminURLFinder
|
||||
from wagtail.admin.panels import get_edit_handler
|
||||
from wagtail.coreutils import get_dummy_request
|
||||
from wagtail.models import Workflow, WorkflowContentType
|
||||
from wagtail.models import Locale, Workflow, WorkflowContentType
|
||||
from wagtail.test.testapp.models import Advert, FullFeaturedSnippet, SnippetChooserModel
|
||||
from wagtail.test.utils import WagtailTestUtils
|
||||
|
||||
|
@ -257,3 +257,39 @@ class TestAdminURLs(WagtailTestUtils, TestCase):
|
|||
reverse(viewset.chooser_viewset.get_url_name("choose")),
|
||||
expected_choose_url,
|
||||
)
|
||||
|
||||
|
||||
class TestPagination(WagtailTestUtils, TestCase):
|
||||
def setUp(self):
|
||||
self.user = self.login()
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
default_locale = Locale.get_default()
|
||||
objects = [
|
||||
FullFeaturedSnippet(text=f"Snippet {i}", locale=default_locale)
|
||||
for i in range(32)
|
||||
]
|
||||
FullFeaturedSnippet.objects.bulk_create(objects)
|
||||
objects = [Advert(text=f"Snippet {i}") for i in range(32)]
|
||||
Advert.objects.bulk_create(objects)
|
||||
|
||||
def test_default_list_pagination(self):
|
||||
list_url = reverse(Advert.snippet_viewset.get_url_name("list"))
|
||||
response = self.client.get(list_url)
|
||||
|
||||
# Default is 20 per page
|
||||
self.assertEqual(Advert.objects.all().count(), 32)
|
||||
self.assertContains(response, "Page 1 of 2")
|
||||
self.assertContains(response, "Next")
|
||||
self.assertContains(response, list_url + "?p=2")
|
||||
|
||||
def test_custom_list_pagination(self):
|
||||
list_url = reverse(FullFeaturedSnippet.snippet_viewset.get_url_name("list"))
|
||||
response = self.client.get(list_url)
|
||||
|
||||
# FullFeaturedSnippet is set to display 5 per page
|
||||
self.assertEqual(FullFeaturedSnippet.objects.all().count(), 32)
|
||||
self.assertContains(response, "Page 1 of 7")
|
||||
self.assertContains(response, "Next")
|
||||
self.assertContains(response, list_url + "?p=2")
|
||||
|
|
|
@ -147,7 +147,6 @@ class IndexView(generic.IndexViewOptionalFeaturesMixin, generic.IndexView):
|
|||
index_results_url_name = None
|
||||
delete_url_name = None
|
||||
any_permission_required = ["add", "change", "delete"]
|
||||
paginate_by = 20
|
||||
page_kwarg = "p"
|
||||
# If true, returns just the 'results' include, for use in AJAX responses from search
|
||||
results_only = False
|
||||
|
@ -609,6 +608,9 @@ class SnippetViewSet(ViewSet):
|
|||
#: If left unset, the ``list_display`` attribute of the index view will be used instead, which by default is defined as ``["__str__", wagtail.admin.ui.tables.UpdatedAtColumn()]``.
|
||||
list_display = None
|
||||
|
||||
#: The number of items to display per page in the index view. Defaults to 20.
|
||||
list_per_page = 20
|
||||
|
||||
#: The URL namespace to use for the admin views.
|
||||
#: If left unset, ``wagtailsnippets_{app_label}_{model_name}`` is used instead.
|
||||
admin_url_namespace = None
|
||||
|
@ -746,6 +748,7 @@ class SnippetViewSet(ViewSet):
|
|||
edit_url_name=self.get_url_name("edit"),
|
||||
delete_url_name=self.get_url_name("delete"),
|
||||
list_display=self.list_display,
|
||||
paginate_by=self.list_per_page,
|
||||
)
|
||||
|
||||
@property
|
||||
|
@ -762,6 +765,7 @@ class SnippetViewSet(ViewSet):
|
|||
edit_url_name=self.get_url_name("edit"),
|
||||
delete_url_name=self.get_url_name("delete"),
|
||||
list_display=self.list_display,
|
||||
paginate_by=self.list_per_page,
|
||||
)
|
||||
|
||||
@property
|
||||
|
|
|
@ -236,6 +236,7 @@ class FullFeaturedSnippetViewSet(SnippetViewSet):
|
|||
base_url_path = "deep/within/the/admin"
|
||||
chooser_admin_url_namespace = "my_chooser_namespace"
|
||||
chooser_base_url_path = "choose/wisely"
|
||||
list_per_page = 5
|
||||
|
||||
|
||||
register_snippet(FullFeaturedSnippet, viewset=FullFeaturedSnippetViewSet)
|
||||
|
|
Ładowanie…
Reference in New Issue