diff --git a/wagtail/admin/views/generic/chooser.py b/wagtail/admin/views/generic/chooser.py index e273c453eb..9bd7ced4e6 100644 --- a/wagtail/admin/views/generic/chooser.py +++ b/wagtail/admin/views/generic/chooser.py @@ -13,6 +13,7 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django.views.generic.base import ContextMixin, View +from wagtail import hooks from wagtail.admin.admin_url_finder import AdminURLFinder from wagtail.admin.forms.choosers import ( BaseFilterForm, @@ -64,6 +65,7 @@ class BaseChooseView(ModalPageFurnitureMixin, ContextMixin, View): filter_form_class = None template_name = "wagtailadmin/generic/chooser/chooser.html" results_template_name = "wagtailadmin/generic/chooser/results.html" + construct_queryset_hook_name = None def get_object_list(self): return self.model.objects.all() @@ -105,6 +107,11 @@ class BaseChooseView(ModalPageFurnitureMixin, ContextMixin, View): return FilterForm(self.request.GET) def filter_object_list(self, objects): + if self.construct_queryset_hook_name: + # allow hooks to modify the queryset + for hook in hooks.get_hooks(self.construct_queryset_hook_name): + objects = hook(objects, self.request) + self.filter_form = self.get_filter_form() if self.filter_form.is_valid(): objects = self.filter_form.filter(objects) diff --git a/wagtail/documents/views/chooser.py b/wagtail/documents/views/chooser.py index cd17fea396..38623fdb6c 100644 --- a/wagtail/documents/views/chooser.py +++ b/wagtail/documents/views/chooser.py @@ -6,7 +6,6 @@ from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ from django.views.generic.base import View -from wagtail import hooks from wagtail.admin.staticfiles import versioned_static from wagtail.admin.ui.tables import Column, DateColumn from wagtail.admin.views.generic.chooser import ( @@ -70,19 +69,13 @@ class BaseDocumentChooseView(BaseChooseView): results_template_name = "wagtaildocs/chooser/results.html" per_page = 10 ordering = "-created_at" + construct_queryset_hook_name = "construct_document_chooser_queryset" def get_object_list(self): return self.permission_policy.instances_user_has_any_permission_for( self.request.user, ["choose"] ) - def filter_object_list(self, objects): - # allow hooks to modify the queryset - for hook in hooks.get_hooks("construct_document_chooser_queryset"): - objects = hook(objects, self.request) - - return super().filter_object_list(objects) - def get_filter_form(self): FilterForm = self.get_filter_form_class() return FilterForm(self.request.GET, collections=self.collections) diff --git a/wagtail/images/views/chooser.py b/wagtail/images/views/chooser.py index 56f0e9d9ae..ffa10cb0d8 100644 --- a/wagtail/images/views/chooser.py +++ b/wagtail/images/views/chooser.py @@ -7,7 +7,6 @@ from django.utils.http import urlencode from django.utils.translation import gettext_lazy as _ from django.views.generic.base import View -from wagtail import hooks from wagtail.admin.auth import PermissionPolicyChecker from wagtail.admin.modal_workflow import render_modal_workflow from wagtail.admin.models import popular_tags_for_model @@ -82,6 +81,7 @@ class BaseImageChooseView(BaseChooseView): results_template_name = "wagtailimages/chooser/results.html" per_page = getattr(settings, "WAGTAILIMAGES_CHOOSER_PAGE_SIZE", 12) ordering = "-created_at" + construct_queryset_hook_name = "construct_image_chooser_queryset" def get_object_list(self): return ( @@ -93,10 +93,6 @@ class BaseImageChooseView(BaseChooseView): ) def filter_object_list(self, objects): - # allow hooks to modify the queryset - for hook in hooks.get_hooks("construct_image_chooser_queryset"): - objects = hook(objects, self.request) - tag_name = self.request.GET.get("tag") if tag_name: objects = objects.filter(tags__name=tag_name)