From 26b7893c60284f606df16063d6bf955874c72eb1 Mon Sep 17 00:00:00 2001 From: Matthew Richards Date: Tue, 25 Aug 2020 14:13:46 +0000 Subject: [PATCH] #141: Allow multiple fields to be used in a distinct filter - Inside a PythonICATDistinctFieldFilter, a where filter is created for each field in the request. These are then searched for in execute_icat_query() and the data is compiled to only include data from those fields --- common/icat/filters.py | 15 ++++++++++----- common/icat/helpers.py | 12 ++++++------ 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/common/icat/filters.py b/common/icat/filters.py index 3f3b4701..b80dab45 100644 --- a/common/icat/filters.py +++ b/common/icat/filters.py @@ -66,9 +66,11 @@ def create_condition(attribute_name, operator, value): conditions = {} # Removing quote marks when doing conditions with IN expressions - jpql_value = f"{value}" if isinstance(value, tuple) else f"'{value}'" + jpql_value = ( + f"{value}" if isinstance(value, tuple) or operator == "!=" else f"'{value}'" + ) conditions[attribute_name] = f"{operator} {jpql_value}" - + log.debug("Conditions in ICAT where filter, %s", conditions) return conditions @@ -79,11 +81,14 @@ def __init__(self, fields): def apply_filter(self, query): try: log.info("Adding ICAT distinct filter to ICAT query") - log.debug(f"Self fields: {self.fields}") - - query.setAttribute(self.fields[0]) query.setAggregate("DISTINCT") + # Using where filters to identify which fields to apply distinct too + for field in self.fields: + where_filter = PythonICATWhereFilter(field, "null", "ne") + where_filter.apply_filter(query) + + log.debug("Fields for distinct filter: %s", self.fields) except ValueError as e: raise FilterError(e) diff --git a/common/icat/helpers.py b/common/icat/helpers.py index 8b32e098..a3efb003 100644 --- a/common/icat/helpers.py +++ b/common/icat/helpers.py @@ -162,21 +162,21 @@ def execute_icat_query(client, query, return_json_formattable=False): data = [] log.debug(f"Conditions: {query.conditions}") + # For each condition, extract the attribute names and compile them into a list # TODO - The keys need to be checked to ensure they're != null attribute_names = query.conditions.keys() log.debug(f"Attribute Names: {attribute_names}") - # For each condition, extract the attribute names and compile them into a list - # TODO - Are attributes listed in alphabetical order? Which order should these - # attribute names be put into? - + # Extract the data of the fields used in the distinct filter and return it as a + # response for result in query_result: + distinct_result = {} dict_result = result.as_dict() - + for key, value in dict_result.items(): if key in attribute_names: distinct_result[key] = value - + data.append(distinct_result) return data