diff --git a/datagateway_api/src/search_api/filters.py b/datagateway_api/src/search_api/filters.py index 2786d0c2..b3879e8c 100644 --- a/datagateway_api/src/search_api/filters.py +++ b/datagateway_api/src/search_api/filters.py @@ -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 @@ -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): diff --git a/test/search_api/filters/test_search_api_where_filter.py b/test/search_api/filters/test_search_api_where_filter.py index f212b20c..99142015 100644 --- a/test/search_api/filters/test_search_api_where_filter.py +++ b/test/search_api/filters/test_search_api_where_filter.py @@ -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(