kopia lustrzana https://github.com/wagtail/wagtail
Add page type filter to page listing view
rodzic
f9ac7cafce
commit
1b97e21e20
|
@ -119,3 +119,19 @@ class ContentTypeModelChoiceField(django_filters.fields.ModelChoiceField):
|
|||
|
||||
class ContentTypeFilter(django_filters.ModelChoiceFilter):
|
||||
field_class = ContentTypeModelChoiceField
|
||||
|
||||
|
||||
class ContentTypeModelMultipleChoiceField(
|
||||
django_filters.fields.ModelMultipleChoiceField
|
||||
):
|
||||
"""
|
||||
Custom ModelMultipleChoiceField for ContentType, to show the model verbose name as the label rather
|
||||
than the default 'wagtailcore | page' representation of a ContentType
|
||||
"""
|
||||
|
||||
def label_from_instance(self, obj):
|
||||
return get_content_type_label(obj)
|
||||
|
||||
|
||||
class MultipleContentTypeFilter(django_filters.ModelMultipleChoiceFilter):
|
||||
field_class = ContentTypeModelMultipleChoiceField
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
{% comment %}Add an initial disabled & hidden submit button so that pressing 'enter' will not submit form. Reload not required as the content is dynamically loaded.{% endcomment %}
|
||||
<div class="visuallyhidden"><input disabled type="submit" aria-hidden="true"/></div>
|
||||
<div class="submit visuallyhidden"><input type="submit" value="Search" class="button" /></div>
|
||||
{% include "wagtailadmin/shared/filters.html" %}
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1001,7 +1001,7 @@ class TestInWorkflowStatus(WagtailTestUtils, TestCase):
|
|||
# Warm up cache
|
||||
self.client.get(self.url)
|
||||
|
||||
with self.assertNumQueries(47):
|
||||
with self.assertNumQueries(48):
|
||||
response = self.client.get(self.url)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import Count
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.urls import reverse
|
||||
|
@ -6,6 +7,7 @@ from django.utils.translation import gettext
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from wagtail import hooks
|
||||
from wagtail.admin.filters import MultipleContentTypeFilter, WagtailFilterSet
|
||||
from wagtail.admin.forms.search import SearchForm
|
||||
from wagtail.admin.ui.components import MediaContainer
|
||||
from wagtail.admin.ui.side_panels import (
|
||||
|
@ -21,10 +23,26 @@ from wagtail.admin.ui.tables.pages import (
|
|||
PageTitleColumn,
|
||||
)
|
||||
from wagtail.admin.views.generic.models import IndexView as GenericIndexView
|
||||
from wagtail.models import Page
|
||||
from wagtail.models import Page, get_page_models
|
||||
from wagtail.permissions import page_permission_policy
|
||||
|
||||
|
||||
def get_content_types_for_filter():
|
||||
models = [model.__name__.lower() for model in get_page_models()]
|
||||
return ContentType.objects.filter(model__in=models).order_by("model")
|
||||
|
||||
|
||||
class PageFilterSet(WagtailFilterSet):
|
||||
content_type = MultipleContentTypeFilter(
|
||||
label=_("Page type"),
|
||||
queryset=lambda request: get_content_types_for_filter(),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Page
|
||||
fields = ["content_type"]
|
||||
|
||||
|
||||
class BaseIndexView(GenericIndexView):
|
||||
permission_policy = page_permission_policy
|
||||
any_permission_required = {
|
||||
|
@ -40,6 +58,7 @@ class BaseIndexView(GenericIndexView):
|
|||
paginate_by = 50
|
||||
table_class = PageTable
|
||||
table_classname = "listing full-width"
|
||||
filterset_class = PageFilterSet
|
||||
|
||||
columns = [
|
||||
BulkActionsColumn("bulk_actions", width="10px"),
|
||||
|
@ -164,6 +183,8 @@ class BaseIndexView(GenericIndexView):
|
|||
"content_type", "sites_rooted_here"
|
||||
) & self.permission_policy.explorable_instances(self.request.user)
|
||||
|
||||
filters, pages = self.filter_queryset(pages)
|
||||
|
||||
self.ordering = self.get_ordering()
|
||||
|
||||
if not self.is_searching:
|
||||
|
|
Ładowanie…
Reference in New Issue