Skip to content

Commit

Permalink
#141: Allow multiple fields to be used in a distinct filter
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
MRichards99 committed Aug 25, 2020
1 parent eedc803 commit 26b7893
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
15 changes: 10 additions & 5 deletions common/icat/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 26b7893

Please sign in to comment.