Let filter_object_list handle filtering on things other than filter_form

pull/8931/head
Matt Westcott 2022-07-05 14:52:05 +01:00 zatwierdzone przez Matt Westcott
rodzic 11cb6a1655
commit 8e47e61087
3 zmienionych plików z 16 dodań i 13 usunięć

Wyświetl plik

@ -104,8 +104,11 @@ class BaseChooseView(ModalPageFurnitureMixin, ContextMixin, View):
FilterForm = self.get_filter_form_class()
return FilterForm(self.request.GET)
def filter_object_list(self, objects, form):
return form.filter(objects)
def filter_object_list(self, objects):
self.filter_form = self.get_filter_form()
if self.filter_form.is_valid():
objects = self.filter_form.filter(objects)
return objects
def get_results_url(self):
return reverse(self.results_url_name)
@ -125,10 +128,7 @@ class BaseChooseView(ModalPageFurnitureMixin, ContextMixin, View):
def get(self, request):
objects = self.get_object_list()
objects = self.apply_object_list_ordering(objects)
self.filter_form = self.get_filter_form()
if self.filter_form.is_valid():
objects = self.filter_object_list(objects, self.filter_form)
objects = self.filter_object_list(objects)
paginator = Paginator(objects, per_page=self.per_page)
self.results = paginator.get_page(request.GET.get("p"))

Wyświetl plik

@ -72,14 +72,16 @@ class BaseDocumentChooseView(BaseChooseView):
ordering = "-created_at"
def get_object_list(self):
documents = self.permission_policy.instances_user_has_any_permission_for(
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"):
documents = hook(documents, self.request)
objects = hook(objects, self.request)
return documents
return super().filter_object_list(objects)
def get_filter_form(self):
FilterForm = self.get_filter_form_class()

Wyświetl plik

@ -84,7 +84,7 @@ class BaseImageChooseView(BaseChooseView):
ordering = "-created_at"
def get_object_list(self):
images = (
return (
permission_policy.instances_user_has_any_permission_for(
self.request.user, ["choose"]
)
@ -92,15 +92,16 @@ class BaseImageChooseView(BaseChooseView):
.prefetch_renditions("max-165x165")
)
def filter_object_list(self, objects):
# allow hooks to modify the queryset
for hook in hooks.get_hooks("construct_image_chooser_queryset"):
images = hook(images, self.request)
objects = hook(objects, self.request)
tag_name = self.request.GET.get("tag")
if tag_name:
images = images.filter(tags__name=tag_name)
objects = objects.filter(tags__name=tag_name)
return images
return super().filter_object_list(objects)
def get_filter_form(self):
FilterForm = self.get_filter_form_class()