From e68f68b680ab6037dbbe4bdb5e39ab0b90354ca4 Mon Sep 17 00:00:00 2001 From: Galen Date: Tue, 19 Nov 2024 22:26:28 -0800 Subject: [PATCH] move self.search_request into BaseSearchFilter via SearchFilterFactory, re #11578 --- arches/app/search/components/base.py | 18 ++++++++++----- .../app/search/components/base_search_view.py | 16 ++++++++------ arches/app/search/components/paging_filter.py | 22 +++++++++---------- arches/app/search/components/term_filter.py | 3 +-- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/arches/app/search/components/base.py b/arches/app/search/components/base.py index b0b6e06cf46..146e981b18f 100644 --- a/arches/app/search/components/base.py +++ b/arches/app/search/components/base.py @@ -9,8 +9,11 @@ class BaseSearchFilter: - def __init__(self, request=None, user=None, componentname=None): + def __init__( + self, request=None, search_request=None, user=None, componentname=None + ): self.request = request + self.search_request = search_request self.user = user self.componentname = componentname @@ -54,6 +57,13 @@ def __init__(self, request=None, user=None): for search_filter in SearchComponent.objects.all() } self.search_filters_instances = {} + request_object = ( + self.request.GET if self.request.method == "GET" else self.request.POST + ) + request_object = request_object.dict() + self.search_request = self.create_search_query_dict( + list(request_object.items()) + ) def get_filter(self, componentname): if componentname in self.search_filters: @@ -71,7 +81,7 @@ def get_filter(self, componentname): ) if class_method: filter_instance = class_method( - self.request, self.user, componentname + self.request, self.search_request, self.user, componentname ) self.search_filters_instances[search_filter.componentname] = ( filter_instance @@ -83,10 +93,8 @@ def get_filter(self, componentname): def get_searchview_name(self): if not self.request: searchview_component_name = None - elif self.request.method == "POST": - searchview_component_name = self.request.POST.get("search-view", None) else: - searchview_component_name = self.request.GET.get("search-view", None) + searchview_component_name = self.search_request.get("search-view", None) if not searchview_component_name: # get default search_view component diff --git a/arches/app/search/components/base_search_view.py b/arches/app/search/components/base_search_view.py index 6a7dcc04118..a654c9c2d81 100644 --- a/arches/app/search/components/base_search_view.py +++ b/arches/app/search/components/base_search_view.py @@ -48,8 +48,15 @@ class BaseSearchView(BaseSearchFilter): how to execute a search in the search_results method """ - def __init__(self, request=None, user=None, componentname=None): - super().__init__(request=request, user=user, componentname=componentname) + def __init__( + self, request=None, search_request=None, user=None, componentname=None + ): + super().__init__( + request=request, + search_request=search_request, + user=user, + componentname=componentname, + ) self.searchview_component = SearchComponent.objects.get( componentname=componentname ) @@ -95,11 +102,6 @@ def __init__(self, request=None, user=None, componentname=None): item.componentname, float("inf") ), ) - request_object = ( - self.request.GET if self.request.method == "GET" else self.request.POST - ) - request_object = request_object.dict() - self.search_request = self.create_query_dict(request_object) @property def required_search_filters(self): diff --git a/arches/app/search/components/paging_filter.py b/arches/app/search/components/paging_filter.py index 1ef6217572e..f6437d1cadd 100644 --- a/arches/app/search/components/paging_filter.py +++ b/arches/app/search/components/paging_filter.py @@ -17,28 +17,26 @@ class PagingFilter(BaseSearchFilter): def append_dsl(self, search_query_object, **kwargs): - search_request = kwargs.get("search_request") - export = search_request.get("export", None) - mobile_download = search_request.get("mobiledownload", None) + export = self.search_request.get("export", None) + mobile_download = self.search_request.get("mobiledownload", None) page = ( 1 - if search_request.get(self.componentname) == "" - or isinstance(search_request.get(self.componentname), dict) - else int(search_request.get(self.componentname, 1)) + if self.search_request.get(self.componentname) == "" + or isinstance(self.search_request.get(self.componentname), dict) + else int(self.search_request.get(self.componentname, 1)) ) if export is not None: limit = settings.SEARCH_RESULT_LIMIT elif mobile_download is not None: - limit = search_request.get("resourcecount") + limit = self.search_request.get("resourcecount") else: limit = settings.SEARCH_ITEMS_PER_PAGE - limit = int(search_request.get("limit", limit)) + limit = int(self.search_request.get("limit", limit)) search_query_object["query"].start = limit * int(page - 1) search_query_object["query"].limit = limit def post_search_hook(self, search_query_object, response_object, **kwargs): - search_request = kwargs.get("search_request") total = ( response_object["results"]["hits"]["total"]["value"] if response_object["results"]["hits"]["total"]["value"] @@ -47,9 +45,9 @@ def post_search_hook(self, search_query_object, response_object, **kwargs): ) page = ( 1 - if search_request.get(self.componentname) == "" - or isinstance(search_request.get(self.componentname), dict) - else int(search_request.get(self.componentname, 1)) + if self.search_request.get(self.componentname) == "" + or isinstance(self.search_request.get(self.componentname), dict) + else int(self.search_request.get(self.componentname, 1)) ) paginator, pages = get_paginator( diff --git a/arches/app/search/components/term_filter.py b/arches/app/search/components/term_filter.py index 4620b655fe9..17b2f000032 100644 --- a/arches/app/search/components/term_filter.py +++ b/arches/app/search/components/term_filter.py @@ -34,8 +34,7 @@ def append_dsl(self, search_query_object, **kwargs): include_provisional = kwargs.get("include_provisional") search_query = Bool() querystring_params = kwargs.get("querystring", "[]") - search_request = kwargs.get("search_request") - language = search_request.get("language", "*") + language = self.search_request.get("language", "*") for term in JSONDeserializer().deserialize(querystring_params): if term["type"] == "term" or term["type"] == "string": string_filter = Bool()