Skip to content

Commit

Permalink
refactor: move ICAT relations call to filter handler #268
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Feb 3, 2022
1 parent 73b70bd commit 8840a6c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
23 changes: 23 additions & 0 deletions datagateway_api/src/common/filter_order_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ def add_icat_relations_for_non_related_fields_of_panosc_related_entities(
python_icat_include_filter = PythonICATIncludeFilter(icat_relations)
self.filters.append(python_icat_include_filter)

def add_icat_relations_for_panosc_non_related_fields(
self, panosc_entity_name,
):
"""
Retrieve ICAT relations and create a `PythonICATIncludeFilter` for these ICAT
relations
:param panosc_entity_name: A PaNOSC entity name e.g. "Dataset"
:type panosc_entity_name: :class:`str`
:return: ICAT relations for the non related fields of the given PaNOSC entity
"""

icat_relations = mappings.get_icat_relations_for_panosc_non_related_fields(
panosc_entity_name,
)

# Remove any duplicate ICAT relations
icat_relations = list(dict.fromkeys(icat_relations))
if icat_relations:
self.filters.append(PythonICATIncludeFilter(icat_relations))

return icat_relations

def merge_python_icat_limit_skip_filters(self):
"""
When there are both limit and skip filters in a request, merge them into the
Expand Down
16 changes: 6 additions & 10 deletions datagateway_api/src/search_api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

from datagateway_api.src.common.exceptions import MissingRecordError
from datagateway_api.src.common.filter_order_handler import FilterOrderHandler
from datagateway_api.src.datagateway_api.icat.filters import PythonICATIncludeFilter
from datagateway_api.src.search_api.filters import SearchAPIWhereFilter
import datagateway_api.src.search_api.models as models
from datagateway_api.src.search_api.panosc_mappings import mappings
from datagateway_api.src.search_api.query import SearchAPIQuery
from datagateway_api.src.search_api.session_handler import (
client_manager,
Expand Down Expand Up @@ -34,18 +32,16 @@ def get_search(entity_name, filters):
log.info("Searching for %s using request's filters", entity_name)
log.debug("Entity Name: %s, Filters: %s", entity_name, filters)

icat_relations = mappings.get_icat_relations_for_panosc_non_related_fields(
entity_name,
)
# Remove any duplicate ICAT relations
icat_relations = list(dict.fromkeys(icat_relations))
if icat_relations:
filters.append(PythonICATIncludeFilter(icat_relations))

query = SearchAPIQuery(entity_name)

filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
icat_relations = filter_handler.add_icat_relations_for_panosc_non_related_fields(
entity_name,
)
filter_handler.add_icat_relations_for_non_related_fields_of_panosc_related_entities(
entity_name,
)
filter_handler.merge_python_icat_limit_skip_filters()
filter_handler.apply_filters(query)

Expand Down

0 comments on commit 8840a6c

Please sign in to comment.