From a430ed5d9d61671c55cc51dee9afccba2bce7c17 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Fri, 14 Aug 2020 22:58:25 +0200 Subject: [PATCH] Add patch to Snippet chooser to customize search With this patch installed, the snippet chooser checks for a method `chooser_search` in the model. If it exists, this method is used instead of the search backend. --- ...earch-method-from-model-if-available.patch | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 patches/Wagtail-Snippet-Chooser-Call-search-method-from-model-if-available.patch diff --git a/patches/Wagtail-Snippet-Chooser-Call-search-method-from-model-if-available.patch b/patches/Wagtail-Snippet-Chooser-Call-search-method-from-model-if-available.patch new file mode 100644 index 0000000..707bab4 --- /dev/null +++ b/patches/Wagtail-Snippet-Chooser-Call-search-method-from-model-if-available.patch @@ -0,0 +1,28 @@ +From 7b829bfdcd151a256788b99276db0566983b1ee6 Mon Sep 17 00:00:00 2001 +From: Donald Buczek +Date: Fri, 14 Aug 2020 22:55:59 +0200 +Subject: [PATCH] Chooser: Call search method from model if available + +--- + .../site-packages/wagtail/snippets/views/chooser.py | 5 ++++- + 1 file changed, 4 insertions(+), 1 deletion(-) + +diff --git a/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py b/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py +index 7c93edd..26606cd 100644 +--- a/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py ++++ b/lib/python3.7/site-packages/wagtail/snippets/views/chooser.py +@@ -35,7 +35,10 @@ def choose(request, app_label, model_name): + search_query = search_form.cleaned_data['q'] + + search_backend = get_search_backend() +- items = search_backend.search(search_query, items) ++ if callable(getattr(model, "chooser_search", None)): ++ items = model.chooser_search(search_query) ++ else: ++ items = search_backend.search(search_query, items) + is_searching = True + + else: +-- +2.26.2 +