Skip to content

Commit

Permalink
#140: Allow multiple order filters to be used in conjunction
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Aug 19, 2020
1 parent 51924f8 commit aec81f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
6 changes: 1 addition & 5 deletions common/filter_order_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 8 additions & 8 deletions common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
15 changes: 14 additions & 1 deletion common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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 = []

0 comments on commit aec81f1

Please sign in to comment.