|
38 | 38 | from stac_fastapi.types.conformance import BASE_CONFORMANCE_CLASSES |
39 | 39 | from stac_fastapi.types.extension import ApiExtension |
40 | 40 | from stac_fastapi.types.requests import get_base_url |
| 41 | +from stac_fastapi.types.rfc3339 import DateTimeType |
41 | 42 | from stac_fastapi.types.search import BaseSearchPostRequest |
42 | 43 | from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection |
43 | 44 |
|
@@ -348,46 +349,50 @@ async def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item: |
348 | 349 | ) |
349 | 350 | return self.item_serializer.db_to_stac(item, base_url) |
350 | 351 |
|
| 352 | + # DateTimeType = Union[ |
| 353 | + # datetime, |
| 354 | + # Tuple[datetime, datetime], |
| 355 | + # Tuple[Optional[datetime], Optional[datetime]] |
| 356 | + # ] |
| 357 | + |
351 | 358 | @staticmethod |
352 | | - def _return_date(interval_str): |
| 359 | + def _return_date(interval: Optional[DateTimeType]) -> dict: |
353 | 360 | """ |
354 | | - Convert a date interval string into a dictionary for filtering search results. |
| 361 | + Convert a date interval already parsed as DateTimeType into a dictionary. |
| 362 | +
|
| 363 | + For filtering search results with Elasticsearch. |
355 | 364 |
|
356 | | - The date interval string should be formatted as either a single date or a range of dates separated |
357 | | - by "/". The date format should be ISO-8601 (YYYY-MM-DDTHH:MM:SSZ). If the interval string is a |
358 | | - single date, it will be converted to a dictionary with a single "eq" key whose value is the date in |
359 | | - the ISO-8601 format. If the interval string is a range of dates, it will be converted to a |
360 | | - dictionary with "gte" (greater than or equal to) and "lte" (less than or equal to) keys. If the |
361 | | - interval string is a range of dates with ".." instead of "/", the start and end dates will be |
362 | | - assigned default values to encompass the entire possible date range. |
| 365 | + This function ensures the output dictionary contains 'gte' and 'lte' keys |
| 366 | + even if they are set to None to prevent KeyError in the consuming logic. |
363 | 367 |
|
364 | 368 | Args: |
365 | | - interval_str (str): The date interval string to be converted. |
| 369 | + interval (Optional[DateTimeType]): The date interval, which might be a single datetime, |
| 370 | + a tuple with one or two datetimes, or None. |
366 | 371 |
|
367 | 372 | Returns: |
368 | | - dict: A dictionary representing the date interval for use in filtering search results. |
| 373 | + dict: A dictionary representing the date interval for use in filtering search results, |
| 374 | + always containing 'gte' and 'lte' keys. |
369 | 375 | """ |
370 | | - intervals = interval_str.split("/") |
371 | | - if len(intervals) == 1: |
372 | | - datetime = f"{intervals[0][0:19]}Z" |
373 | | - return {"eq": datetime} |
374 | | - else: |
375 | | - start_date = intervals[0] |
376 | | - end_date = intervals[1] |
377 | | - if ".." not in intervals: |
378 | | - start_date = f"{start_date[0:19]}Z" |
379 | | - end_date = f"{end_date[0:19]}Z" |
380 | | - elif start_date != "..": |
381 | | - start_date = f"{start_date[0:19]}Z" |
382 | | - end_date = "2200-12-01T12:31:12Z" |
383 | | - elif end_date != "..": |
384 | | - start_date = "1900-10-01T00:00:00Z" |
385 | | - end_date = f"{end_date[0:19]}Z" |
386 | | - else: |
387 | | - start_date = "1900-10-01T00:00:00Z" |
388 | | - end_date = "2200-12-01T12:31:12Z" |
| 376 | + result: Dict[str, Optional[str]] = {"gte": None, "lte": None} |
| 377 | + |
| 378 | + if interval is None: |
| 379 | + return result # Return default if no interval is specified |
| 380 | + |
| 381 | + if isinstance(interval, datetime_type): |
| 382 | + # Single datetime object for both 'gte' and 'lte' |
| 383 | + datetime_iso = interval.isoformat() |
| 384 | + result["gte"] = datetime_iso |
| 385 | + result["lte"] = datetime_iso |
| 386 | + return result |
| 387 | + |
| 388 | + # Handling tuples, which may contain one or two datetime objects or None |
| 389 | + start, end = interval |
| 390 | + if start: |
| 391 | + result["gte"] = start.isoformat() |
| 392 | + if end: |
| 393 | + result["lte"] = end.isoformat() |
389 | 394 |
|
390 | | - return {"lte": end_date, "gte": start_date} |
| 395 | + return result |
391 | 396 |
|
392 | 397 | async def get_search( |
393 | 398 | self, |
@@ -459,7 +464,6 @@ async def get_search( |
459 | 464 | "direction": "desc" if sort[0] == "-" else "asc", |
460 | 465 | } |
461 | 466 | ) |
462 | | - print(sort_param) |
463 | 467 | base_args["sortby"] = sort_param |
464 | 468 |
|
465 | 469 | if filter: |
|
0 commit comments