@@ -271,6 +271,12 @@ async def item_collection(
271271 Exception: If any error occurs while reading the items from the database.
272272 """
273273 request : Request = kwargs ["request" ]
274+ query_params = dict (request .query_params ) # Convert MultiDict to dict
275+
276+ # I am not sure why I have to do this
277+ if "datetime" in query_params :
278+ datetime = query_params ['datetime' ]
279+
274280 base_url = str (request .base_url )
275281
276282 collection = await self .get_collection (
@@ -357,18 +363,18 @@ async def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
357363 # ]
358364
359365 @staticmethod
360- def _return_date (interval : Optional [DateTimeType ] ) -> dict :
366+ def _return_date (interval : Optional [Union [ DateTimeType , str ]] ) -> Dict [ str , Optional [ str ]] :
361367 """
362- Convert a date interval already parsed as DateTimeType into a dictionary.
363-
364- For filtering search results with Elasticsearch.
368+ Convert a date interval (which may be a datetime, a tuple of one or two datetimes,
369+ a string representing a datetime or range, or None) into a dictionary for filtering
370+ search results with Elasticsearch.
365371
366- This function ensures the output dictionary contains 'gte' and 'lte' keys
367- even if they are set to None to prevent KeyError in the consuming logic.
372+ This function ensures the output dictionary contains 'gte' and 'lte' keys,
373+ even if they are set to None, to prevent KeyError in the consuming logic.
368374
369375 Args:
370- interval (Optional[DateTimeType]): The date interval, which might be a single datetime,
371- a tuple with one or two datetimes, or None.
376+ interval (Optional[Union[ DateTimeType, str] ]): The date interval, which might be a single datetime,
377+ a tuple with one or two datetimes, a string, or None.
372378
373379 Returns:
374380 dict: A dictionary representing the date interval for use in filtering search results,
@@ -377,21 +383,28 @@ def _return_date(interval: Optional[DateTimeType]) -> dict:
377383 result : Dict [str , Optional [str ]] = {"gte" : None , "lte" : None }
378384
379385 if interval is None :
380- return result # Return default if no interval is specified
386+ return result
381387
382- if isinstance (interval , datetime_type ):
383- # Single datetime object for both 'gte' and 'lte'
384- datetime_iso = interval .isoformat ()
385- result ["gte" ] = datetime_iso
386- result ["lte" ] = datetime_iso
388+ if isinstance (interval , str ):
389+ if '/' in interval :
390+ parts = interval .split ('/' )
391+ result ['gte' ] = parts [0 ] if parts [0 ] != ".." else None
392+ result ['lte' ] = parts [1 ] if len (parts ) > 1 and parts [1 ] != ".." else None
393+ else :
394+ converted_time = interval if interval != ".." else None
395+ result ['gte' ] = result ['lte' ] = converted_time
387396 return result
388397
389- # Handling tuples, which may contain one or two datetime objects or None
390- start , end = interval
391- if start :
392- result ["gte" ] = start .isoformat ()
393- if end :
394- result ["lte" ] = end .isoformat ()
398+ if isinstance (interval , datetime_type ):
399+ datetime_iso = interval .isoformat ()
400+ result ['gte' ] = result ['lte' ] = datetime_iso
401+ elif isinstance (interval , tuple ):
402+ start , end = interval
403+ # Ensure datetimes are converted to UTC and formatted with 'Z'
404+ if start :
405+ result ["gte" ] = start .strftime ('%Y-%m-%dT%H:%M:%S.%f' )[:- 3 ] + 'Z'
406+ if end :
407+ result ["lte" ] = end .strftime ('%Y-%m-%dT%H:%M:%S.%f' )[:- 3 ] + 'Z'
395408
396409 return result
397410
0 commit comments