Allow default ordering of Snippets IndexView to be specified via SnippetViewSet (#10276)

pull/8345/head
Sage Abdullah 2023-04-03 12:50:20 +01:00 zatwierdzone przez Matt Westcott
rodzic ed58c692ca
commit 3965ceedfa
7 zmienionych plików z 40 dodań i 3 usunięć

Wyświetl plik

@ -27,7 +27,7 @@ Changelog
* Add helpful properties to `Locale` for more convenient usage within templates (Andy Babic)
* Allow customising icons for snippets via `SnippetViewSet.icon` (Daniel Kirkham, Sage Abdullah)
* Allow customising the base URL and URL namespace for snippet views (Sage Abdullah)
* Allow customising the number of items per page for snippet listing views (Sage Abdullah)
* Allow customising the default ordering and number of items per page for snippet listing views (Sage Abdullah)
* Re-label "StreamField blocks" option in block picker to "Blocks" (Thibaud Colas)
* Re-implement styleguide icons list as an auto-generated sequence of tables (Thibaud Colas)
* Switch styleguide navigation to use panel components and minimap (Thibaud Colas)

Wyświetl plik

@ -84,6 +84,7 @@ Viewsets are Wagtail's mechanism for defining a group of related admin views wit
.. autoattribute:: list_filter
.. autoattribute:: filterset_class
.. autoattribute:: list_per_page
.. autoattribute:: ordering
.. autoattribute:: admin_url_namespace
.. autoattribute:: base_url_path
.. autoattribute:: chooser_admin_url_namespace

Wyświetl plik

@ -68,7 +68,7 @@ Those improvements were implemented by Albina Starykova as part of an [Outreachy
* Enhance `wagtail.search.utils.parse_query_string` to allow inner single quotes for key/value parsing (Aman Pandey)
* Add helpful properties to [`Locale`](locale_model_ref) for more convenient usage within templates, see [](i18n_basic_example) (Andy Babic)
* Allow customising the base URL and URL namespace for snippet views (Sage Abdullah)
* Allow customising the number of items per page for snippet listing views (Sage Abdullah)
* Allow customising the default ordering and number of items per page for snippet listing views (Sage Abdullah)
* Re-label "StreamField blocks" option in block picker to "Blocks" (Thibaud Colas)
* Switch styleguide navigation to use panel components and minimap (Thibaud Colas)
* Explicitly specify `MenuItem.name` for Snippets, Reports, and Settings menu items (Sage Abdullah)

Wyświetl plik

@ -575,7 +575,7 @@ Similar URL customisations are also possible for the snippet chooser views throu
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 (or {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.chooser_per_page` for the chooser listing).
To customise the base queryset for the listing view, you could override the {meth}`~wagtail.snippets.views.snippets.SnippetViewSet.get_queryset` method.
To customise the base queryset for the listing view, you could override the {meth}`~wagtail.snippets.views.snippets.SnippetViewSet.get_queryset` method. Additionally, the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.ordering` attribute can be used to specify the default ordering of the listing view.
You can add the ability to filter the listing view by defining a {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.list_filter` attribute and specifying the list of fields to filter. Wagtail uses the django-filter package under the hood, and this attribute will be passed as django-filter's `FilterSet.Meta.fields` attribute. This means you can also pass a dictionary that maps the field name to a list of lookups. If you would like to customise it further, you can also use a custom `wagtail.admin.filters.WagtailFilterSet` subclass by overriding the {attr}`~wagtail.snippets.views.snippets.SnippetViewSet.filterset_class` attribute. The `list_filter` attribute is ignored if `filterset_class` is set. For more details, refer to [django-filter's documentation](https://django-filter.readthedocs.io/en/stable/guide/usage.html#the-filter).

Wyświetl plik

@ -715,3 +715,33 @@ class TestCustomQuerySet(BaseSnippetViewSetTests):
self.assertContains(response, "FooSnippet")
self.assertNotContains(response, "BarSnippet")
self.assertNotContains(response, "[HIDDEN]Snippet")
class TestCustomOrdering(BaseSnippetViewSetTests):
model = FullFeaturedSnippet
@classmethod
def setUpTestData(cls):
default_locale = Locale.get_default()
objects = [
cls.model(text="CCCCCCCCCC", locale=default_locale),
cls.model(text="AAAAAAAAAA", locale=default_locale),
cls.model(text="DDDDDDDDDD", locale=default_locale),
cls.model(text="BBBBBBBBBB", locale=default_locale),
]
cls.model.objects.bulk_create(objects)
def test_index_view_order(self):
response = self.client.get(self.get_url("list"))
# Should sort by text in descending order as specified in SnippetViewSet.ordering
# (not the default ordering of the model)
self.assertFalse(self.model._meta.ordering)
self.assertEqual(
[obj.text for obj in response.context["page_obj"]],
[
"AAAAAAAAAA",
"BBBBBBBBBB",
"CCCCCCCCCC",
"DDDDDDDDDD",
],
)

Wyświetl plik

@ -644,6 +644,9 @@ class SnippetViewSet(ModelViewSet):
#: The number of items to display in the chooser view. Defaults to 10.
chooser_per_page = 10
#: The default ordering to use for the index view. Can be a string or a list/tuple in the same format as Django's :attr:`~django.db.models.Options.ordering`.
ordering = None
#: The URL namespace to use for the admin views.
#: If left unset, ``wagtailsnippets_{app_label}_{model_name}`` is used instead.
admin_url_namespace = None
@ -807,6 +810,7 @@ class SnippetViewSet(ModelViewSet):
list_display=self.list_display,
list_filter=self.list_filter,
paginate_by=self.list_per_page,
default_ordering=self.ordering,
)
@property
@ -827,6 +831,7 @@ class SnippetViewSet(ModelViewSet):
list_display=self.list_display,
list_filter=self.list_filter,
paginate_by=self.list_per_page,
default_ordering=self.ordering,
)
@property

Wyświetl plik

@ -248,6 +248,7 @@ class FullFeaturedSnippetViewSet(SnippetViewSet):
filterset_class = FullFeaturedSnippetFilterSet
list_display = ["text", "country_code", "get_foo_country_code", UpdatedAtColumn()]
index_template_name = "tests/fullfeaturedsnippet_index.html"
ordering = ["text", "-_updated_at", "-pk"]
def get_history_template(self):
return "tests/snippet_history.html"