Skip to content

Commit

Permalink
feat: add first pass of query param implementation #259
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Nov 19, 2021
1 parent 22e8a73 commit ee668e3
Showing 1 changed file with 110 additions and 7 deletions.
117 changes: 110 additions & 7 deletions datagateway_api/common/search_api/query_filter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from datagateway_api.common.base_query_filter_factory import QueryFilterFactory
from datagateway_api.common.exceptions import FilterError
from datagateway_api.common.search_api.filters import (
SearchAPIIncludeFilter,
SearchAPILimitFilter,
SearchAPISkipFilter,
SearchAPIWhereFilter,
)

log = logging.getLogger()

Expand All @@ -14,22 +20,119 @@ def get_query_filter(request_filter):

if query_param_name == "filter":
log.debug(
f"Filter: {request_filter['filter']}, Type: {type(request_filter['filter'])})"
f"Filter: {request_filter['filter']}, Type: {type(request_filter['filter'])})",
)
for filter_name, filter_input in request_filter["filter"].items():
log.debug(f"Name: {filter_name}, Type: {type(filter_name)}")
log.debug(f"Input: {filter_input}, Type: {type(filter_input)}")
# {"where": {"property": "value"}}
# {"where": {"property": {"operator": "value"}}}
# {"where": {"text": "value"}}
# {"where": {"and": [{"property": "value"}, {"property": "value"}]}}
# {"where": {"or": [{"property": "value"}, {"property": "value"}]}}
if filter_name == "where":
pass
if list(filter_input.keys())[0] == "text":
# Multiple WHERE filters ORing - e.g. title or summary
pass
elif (
list(filter_input.keys())[0] == "and"
or list(filter_input.keys())[0] == "or"
):
boolean_operator = list(filter_input.keys())[0]
conditions = list(filter_input.values())[0]
for condition in conditions:
# Could be nested AND/OR
filter_data = SearchAPIQueryFilterFactory.get_condition_values(
condition,
)
for condition in filter_data:
query_filters.append(
SearchAPIWhereFilter(
condition[0], condition[1], condition[2],
),
)
else:
filter_data = SearchAPIQueryFilterFactory.get_condition_values(
filter_input,
)
for condition in filter_data:

query_filters.append(
SearchAPIWhereFilter(
condition[0], condition[1], condition[2],
),
)

elif filter_name == "include":
pass
# {"include": [{"relation": "relatedModel"}]}
#
# {"include": [{"relation": "relatedModel1"},
# {"relation": "relatedModel2"}]}
#
# {"include": [{"relation": "relatedModel",
# "scope": {"where": {"property": "value"}}}]}

for related_model in filter_input:
included_entity = related_model["relation"]
query_filters.append(SearchAPIIncludeFilter(included_entity))

if "scope" in related_model:
try:
field_name = list(
related_model["scope"]["where"].keys(),
)[0]
full_field_path = f"{included_entity}.{field_name}"
related_model["scope"]["where"][
full_field_path
] = related_model["scope"]["where"].pop("name")

query_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
{"filter": related_model["scope"]},
),
)
except KeyError:
raise FilterError("Error in scope for include filter")
elif filter_name == "limit":
pass
query_filters.append(SearchAPILimitFilter(filter_input))
elif filter_name == "skip":
pass
query_filters.append(SearchAPISkipFilter(filter_input))
else:
raise FilterError(
"No valid filter name given within filter query param"
"No valid filter name given within filter query param",
)

return query_filters
elif query_param_name == "where":
# For the count endpoints
"""
query_filters.extend(
SearchAPIQueryFilterFactory.get_query_filter(
{"filter": related_model["scope"]},
),
),
"""
else:
raise FilterError(f"Bad filter, please check input: {request_filter}")
raise FilterError(
f"Bad filter, please check query parameters: {request_filter}",
)

@staticmethod
def get_condition_values(filter_input):
where_filter_data = []
field = list(filter_input.keys())[0]
filter_data = list(filter_input.values())[0]

if isinstance(filter_data, str):
# {"where": {"property": "value"}}
value = filter_input[field]
operation = "eq"
elif isinstance(filter_data, dict):
# {"where": {"property": {"operator": "value"}}}
print(f"filter data: {filter_data}")
value = list(filter_input[field].values())[0]
operation = list(filter_input[field].keys())[0]

where_filter_data.append((field, value, operation))

return where_filter_data

0 comments on commit ee668e3

Please sign in to comment.