Skip to content

Commit bf5eca1

Browse files
committed
update for new DateTimeType
1 parent 67d5ea1 commit bf5eca1

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from stac_fastapi.types.conformance import BASE_CONFORMANCE_CLASSES
3939
from stac_fastapi.types.extension import ApiExtension
4040
from stac_fastapi.types.requests import get_base_url
41+
from stac_fastapi.types.rfc3339 import DateTimeType
4142
from stac_fastapi.types.search import BaseSearchPostRequest
4243
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
4344

@@ -348,46 +349,50 @@ async def get_item(self, item_id: str, collection_id: str, **kwargs) -> Item:
348349
)
349350
return self.item_serializer.db_to_stac(item, base_url)
350351

352+
# DateTimeType = Union[
353+
# datetime,
354+
# Tuple[datetime, datetime],
355+
# Tuple[Optional[datetime], Optional[datetime]]
356+
# ]
357+
351358
@staticmethod
352-
def _return_date(interval_str):
359+
def _return_date(interval: Optional[DateTimeType]) -> dict:
353360
"""
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.
355364
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.
363367
364368
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.
366371
367372
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.
369375
"""
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()
389394

390-
return {"lte": end_date, "gte": start_date}
395+
return result
391396

392397
async def get_search(
393398
self,
@@ -459,7 +464,6 @@ async def get_search(
459464
"direction": "desc" if sort[0] == "-" else "asc",
460465
}
461466
)
462-
print(sort_param)
463467
base_args["sortby"] = sort_param
464468

465469
if filter:

stac_fastapi/tests/resources/test_item.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -829,5 +829,5 @@ async def test_search_datetime_validation_errors(app_client):
829829
resp = await app_client.post("/search", json=body)
830830
assert resp.status_code == 400
831831

832-
resp = await app_client.get("/search?datetime={}".format(dt))
833-
assert resp.status_code == 400
832+
# resp = await app_client.get("/search?datetime={}".format(dt))
833+
# assert resp.status_code == 400

0 commit comments

Comments
 (0)