Skip to content

Commit

Permalink
#141: Fix where distinct filter doesn't show field because of overla…
Browse files Browse the repository at this point in the history
…pping WHERE filter

- This change means that if there's a distinct filter of an attribute, and a WHERE filter specifying a condition of the same attribute, the distinct filter will act as intended (where before the data of that attribute wasn't added to the response)
  • Loading branch information
MRichards99 committed Aug 26, 2020
1 parent 0029978 commit 92d6b01
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
class Constants:
DATABASE_URL = config.get_db_url()
ACCEPTED_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
PYTHON_ICAT_DISTNCT_CONDITION = "!= null"
34 changes: 24 additions & 10 deletions common/icat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,22 +156,21 @@ def execute_query(self, client, return_json_formattable=False):
if self.query.aggregate == "DISTINCT":
distinct_filter_flag = True
# Check query's conditions for the ones created by the distinct filter
attribute_names = []
self.attribute_names = []
log.debug("Query conditions: %s", self.query.conditions)

for key, value in self.query.conditions.items():
# TODO - Consider that value might be a list, rather than a string value
# Value can be a list if there's multiple WHERE filters for the same
# attribute name within an ICAT query
if isinstance(value, list):
for sub_value in value:
pass

# TODO - Does "!= null" need to be added as a constant? How can it be used
# in the distinct filter side of things?
if value == "!= null":
attribute_names.append(key)
self.check_attribute_name_for_distinct(key, sub_value)
elif isinstance(value, str):
self.check_attribute_name_for_distinct(key, value)
log.debug(
"Attribute names used in the distinct filter, as captured by the"
" query's conditions %s",
attribute_names,
self.attribute_names,
)
else:
distinct_filter_flag = False
Expand All @@ -195,7 +194,7 @@ def execute_query(self, client, return_json_formattable=False):
if distinct_filter_flag:
# Add only the required data as per request's distinct filter
# fields
if key in attribute_names:
if key in self.attribute_names:
distinct_result[key] = value

# Add to the response's data depending on whether request has a distinct
Expand All @@ -208,6 +207,21 @@ def execute_query(self, client, return_json_formattable=False):
else:
return query_result

def check_attribute_name_for_distinct(self, key, value):
"""
Check the attribute name to see if its associated value is used to signify the
attribute is requested in a distinct filter and if so, append it to the list of
attribute names
:param key: Name of an attribute
:type key: :class:`str`
:param value: Expression that should be applied to the associated attribute
e.g. "= 'Metadata'"
:type value: :class:`str`
"""
if value == Constants.PYTHON_ICAT_DISTNCT_CONDITION:
self.attribute_names.append(key)


def get_python_icat_entity_name(client, database_table_name):
"""
Expand Down

0 comments on commit 92d6b01

Please sign in to comment.