Skip to content

Commit

Permalink
feat: allow multiple datetime formats to be used when filtering in se…
Browse files Browse the repository at this point in the history
…arch API #338
  • Loading branch information
MRichards99 committed Feb 22, 2022
1 parent 60a599e commit f20ad24
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions datagateway_api/src/search_api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PythonICATSkipFilter,
PythonICATWhereFilter,
)
from datagateway_api.src.search_api.models import PaNOSCAttribute
from datagateway_api.src.search_api.panosc_mappings import mappings
from datagateway_api.src.search_api.query import SearchAPIQuery

Expand All @@ -19,6 +20,19 @@ class SearchAPIWhereFilter(PythonICATWhereFilter):
def __init__(self, field, value, operation, search_api_query=None):
self.search_api_query = search_api_query
super().__init__(field, value, operation)

# Detect various datetime formats and convert them into a format that ICAT can
# understand
if (
self.field in PaNOSCAttribute._datetime_field_names
and isinstance(self.value, str)
and DateHandler.is_str_a_date(self.value)
):
value_datetime = DateHandler.str_to_datetime_object(value)
str_datetime = DateHandler.datetime_object_to_str(value_datetime)
# +/- need to be removed so the format works when querying ICAT
self.value = str_datetime.replace("+", " ").replace("-", " ")

log.info("SearchAPIWhereFilter created: %s", repr(self))

def apply_filter(self, query):
Expand Down
2 changes: 1 addition & 1 deletion test/search_api/filters/test_search_api_where_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TestSearchAPIWhereFilter:
SearchAPIWhereFilter("startDate", "2018-05-05T15:00:00.000Z", "gt"),
"Document",
"SELECT o FROM Investigation o WHERE o.startDate >"
" '2018-05-05T15:00:00.000Z'",
" '2018 05 05 15:00:00 00:00'",
id="WHERE filter with date value",
),
pytest.param(
Expand Down

0 comments on commit f20ad24

Please sign in to comment.