Skip to content

Commit

Permalink
fix: make count endpoints work with empty where filter input #309
Browse files Browse the repository at this point in the history
  • Loading branch information
VKTB committed Feb 8, 2022
1 parent b3bb508 commit 56d613d
Showing 1 changed file with 74 additions and 69 deletions.
143 changes: 74 additions & 69 deletions datagateway_api/src/search_api/query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,84 +106,89 @@ def get_where_filter(where_filter_input, entity_name):
"""

where_filters = []
if (
list(where_filter_input.keys())[0] == "and"
or list(where_filter_input.keys())[0] == "or"
):
log.debug("and/or operators found: %s", list(where_filter_input.keys())[0])
boolean_operator = list(where_filter_input.keys())[0]
conditions = list(where_filter_input.values())[0]
conditional_where_filters = []

for condition in conditions:
# Could be nested AND/OR
where_filter = {
"filter": {"where": condition},
}
conditional_where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
),
if where_filter_input:
if (
list(where_filter_input.keys())[0] == "and"
or list(where_filter_input.keys())[0] == "or"
):
log.debug(
"and/or operators found: %s", list(where_filter_input.keys())[0],
)
boolean_operator = list(where_filter_input.keys())[0]
conditions = list(where_filter_input.values())[0]
conditional_where_filters = []

for condition in conditions:
# Could be nested AND/OR
where_filter = {
"filter": {"where": condition},
}
conditional_where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
),
)

nested = NestedWhereFilters(
conditional_where_filters[:-1],
conditional_where_filters[-1],
boolean_operator,
SearchAPIQuery(entity_name),
)
where_filters.append(nested)
elif list(where_filter_input.keys())[0] == "text":
log.debug("Text operator found within JSON where object")
try:
entity_class = getattr(search_api_models, entity_name)
except AttributeError as e:
raise SearchAPIError(
f"No text operator fields have been defined for {entity_name}"
f", {e.args}",
nested = NestedWhereFilters(
conditional_where_filters[:-1],
conditional_where_filters[-1],
boolean_operator,
SearchAPIQuery(entity_name),
)
where_filters.append(nested)
elif list(where_filter_input.keys())[0] == "text":
log.debug("Text operator found within JSON where object")
try:
entity_class = getattr(search_api_models, entity_name)
except AttributeError as e:
raise SearchAPIError(
f"No text operator fields have been defined for {entity_name}"
f", {e.args}",
)

or_conditional_filters = []
field_names = entity_class._text_operator_fields
log.debug(
"Text operators found for PaNOSC %s: %s", entity_name, field_names,
)
if not field_names:
# No text operator fields present, simply log and move on, we should
# ignore text operator queries on entities where `_text_operator_fields`
# is empty (meaning they are not present in the origina PaNOSC data
# model)
log.info(
"No text operator fields found for PaNOSC entity %s, will"
" ignore",
entity_name,
or_conditional_filters = []
field_names = entity_class._text_operator_fields
log.debug(
"Text operators found for PaNOSC %s: %s", entity_name, field_names,
)
else:
for field_name in field_names:
or_conditional_filters.append(
{field_name: {"like": where_filter_input["text"]}},
if not field_names:
# No text operator fields present, simply log and move on, we should
# ignore text operator queries on entities where
# `_text_operator_fields` is empty (meaning they are not present in
# the origina PaNOSC data model)
log.info(
"No text operator fields found for PaNOSC entity %s, will"
" ignore",
entity_name,
)
else:
for field_name in field_names:
or_conditional_filters.append(
{field_name: {"like": where_filter_input["text"]}},
)

where_filter = {
"filter": {"where": {"or": or_conditional_filters}},
}
where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
where_filter = {
"filter": {"where": {"or": or_conditional_filters}},
}
where_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
where_filter, entity_name,
),
)
else:
log.info(
"Basic where filter found, extracting field, value and operation",
)
filter_data = SearchAPIQueryFilterFactory.get_condition_values(
where_filter_input,
)
where_filters.append(
SearchAPIWhereFilter(
field=filter_data[0],
value=filter_data[1],
operation=filter_data[2],
),
)
else:
log.info("Basic where filter found, extracting field, value and operation")
filter_data = SearchAPIQueryFilterFactory.get_condition_values(
where_filter_input,
)
where_filters.append(
SearchAPIWhereFilter(
field=filter_data[0],
value=filter_data[1],
operation=filter_data[2],
),
)

return where_filters

Expand Down

0 comments on commit 56d613d

Please sign in to comment.