0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Move 'construct queryset' hook handling into the base chooser

This commit is contained in:
Matt Westcott 2022-07-05 15:02:38 +01:00 committed by Matt Westcott
parent 8e47e61087
commit c72a9aba11
3 changed files with 9 additions and 13 deletions

View File

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

View File

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

View File

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