diff --git a/common/filter_order_handler.py b/common/filter_order_handler.py index aa799216..cfcba3e2 100644 --- a/common/filter_order_handler.py +++ b/common/filter_order_handler.py @@ -22,14 +22,10 @@ def sort_filters(self): def apply_filters(self, query): """ Given a query apply the filters the handler has in the correct order. + :param query: The query to have filters applied to """ self.sort_filters() - - """ - if any(isinstance(filter, PythonICATOrderFilter) for filter in self.filters): - PythonICATOrderFilter.result_order = [] - """ for filter in self.filters: filter.apply_filter(query) diff --git a/common/icat/filters.py b/common/icat/filters.py index cb2e7689..2594aed4 100644 --- a/common/icat/filters.py +++ b/common/icat/filters.py @@ -78,23 +78,23 @@ def apply_filter(self, query): class PythonICATOrderFilter(OrderFilter): + result_order = [] + def __init__(self, field, direction): # Python ICAT doesn't automatically uppercase the direction, errors otherwise super().__init__(field, direction.upper()) def apply_filter(self, query): - result_order = [(self.field, self.direction)] - log.debug("Result Order: %s", result_order) + PythonICATOrderFilter.result_order.append((self.field, self.direction)) + log.debug("Result Order: %s", PythonICATOrderFilter.result_order) try: log.info("Adding order filter") query.setOrder(PythonICATOrderFilter.result_order) - except ValueError: - raise FilterError( - "Order Filter Error: Either an invalid attribute(s) or attribute(s)" - " contains 1-many relationship" - ) - + except ValueError as e: + # Typically either invalid attribute(s) or attribute(s) contains 1-many + # relationship + raise FilterError(e) class PythonICATSkipFilter(SkipFilter): diff --git a/common/icat/helpers.py b/common/icat/helpers.py index a3859a23..0edb60ee 100644 --- a/common/icat/helpers.py +++ b/common/icat/helpers.py @@ -12,7 +12,7 @@ ) from common.filter_order_handler import FilterOrderHandler from common.constants import Constants -from common.icat.filters import PythonICATLimitFilter, PythonICATWhereFilter +from common.icat.filters import PythonICATOrderFilter, PythonICATWhereFilter log = logging.getLogger() @@ -360,10 +360,15 @@ def update_entity_by_id(client, table_name, id_, new_data): def get_entity_with_filters(client, table_name, filters): + """ + TODO - Add docstring + """ selected_entity_name = get_python_icat_entity_name(client, table_name) query = construct_icat_query(client, selected_entity_name) + filter_handler = FilterOrderHandler() filter_handler.add_filters(filters) + manage_order_filters(filter_handler.filters) filter_handler.apply_filters(query) data = execute_icat_query(client, query, True) @@ -372,3 +377,11 @@ def get_entity_with_filters(client, table_name, filters): raise MissingRecordError("No results found") else: return data + + +def manage_order_filters(filters): + """ + TODO - Add docstring + """ + if any(isinstance(filter, PythonICATOrderFilter) for filter in filters): + PythonICATOrderFilter.result_order = []