From 4f016feb784e094dbb841bff8e3aeb69f4b8216e Mon Sep 17 00:00:00 2001 From: typonaut <55769287+typonaut@users.noreply.github.com> Date: Tue, 21 Nov 2023 19:08:26 +0000 Subject: [PATCH 1/2] Update mixins.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Revision to deal with multiple values of 'q', for example where forms.MultipleChoiceField() and/or forms.CheckboxSelectMultiple widget are used (or simply "GET …?q=hello&q=world"). Principally uses .getlist() rather than .get() on the querydict - the querydict.getlist() method is essentially unchanged since Django 1.11, so should not have a backwards compatibility effect. --- extra_views/contrib/mixins.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/extra_views/contrib/mixins.py b/extra_views/contrib/mixins.py index 9b6b0e2..d784a4c 100644 --- a/extra_views/contrib/mixins.py +++ b/extra_views/contrib/mixins.py @@ -49,11 +49,23 @@ class SearchableListMixin(object): search_use_q = True check_lookups = True - def get_words(self, query): + def get_words(self, query): if self.search_split: - return query.split() - return [query] - + s_query = [] + for w in query: + s_query += w.split() + return self.strip_list(s_query) + return self.strip_list(query) + + def strip_list(self, query): + """ + Replicates previous behaviour of 'get_search_query()' where + it was expected that the returned string would have '.strip()' + applied, ie: + return self.search_use_q and self.request.GET.get("q", "").strip() + """ + return [i.strip() for i in query] + def get_search_fields_with_filters(self): fields = [] for sf in self.search_fields: @@ -80,9 +92,13 @@ def try_convert_to_date(self, word): def get_search_query(self): """ Get query from request.GET 'q' parameter when search_use_q is set to True - Override this method to provide your own query to search + Override this method to provide your own query to search. + + Updated to deliver a list via .getlist() rather than .get() in order + to accomodate multiple values of 'q', for example from + forms.MultipleChoiceField() and/or forms.CheckboxSelectMultiple widget. """ - return self.search_use_q and self.request.GET.get("q", "").strip() + return self.search_use_q and self.request.GET.getlist("q", "") def get_queryset(self): qs = super(SearchableListMixin, self).get_queryset() From 553c5ac155f346cdd6bb8dccc5e57957b99991d4 Mon Sep 17 00:00:00 2001 From: typonaut <55769287+typonaut@users.noreply.github.com> Date: Tue, 21 Nov 2023 19:16:39 +0000 Subject: [PATCH 2/2] Update mixins.py --- extra_views/contrib/mixins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extra_views/contrib/mixins.py b/extra_views/contrib/mixins.py index d784a4c..997f5a8 100644 --- a/extra_views/contrib/mixins.py +++ b/extra_views/contrib/mixins.py @@ -49,7 +49,7 @@ class SearchableListMixin(object): search_use_q = True check_lookups = True - def get_words(self, query): + def get_words(self, query): if self.search_split: s_query = [] for w in query: