Skip to content

Commit

Permalink
move self.search_request into BaseSearchFilter via SearchFilterFactor…
Browse files Browse the repository at this point in the history
…y, re #11578
  • Loading branch information
whatisgalen committed Nov 20, 2024
1 parent a1f34c4 commit e68f68b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
18 changes: 13 additions & 5 deletions arches/app/search/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions arches/app/search/components/base_search_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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):
Expand Down
22 changes: 10 additions & 12 deletions arches/app/search/components/paging_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions arches/app/search/components/term_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit e68f68b

Please sign in to comment.