Skip to content

fix(search): clear datetime between searches #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions stac_pydantic/api/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
Point,
Polygon,
)
from pydantic import BaseModel, Field, TypeAdapter, field_validator, model_validator
from pydantic import (
BaseModel,
Field,
PrivateAttr,
TypeAdapter,
field_validator,
model_validator,
)

from stac_pydantic.api.extensions.fields import FieldsExtension
from stac_pydantic.api.extensions.query import Operator
Expand Down Expand Up @@ -45,8 +52,17 @@ class Search(BaseModel):
limit: Optional[int] = 10

# Private properties to store the parsed datetime values. Not part of the model schema.
_start_date: Optional[dt] = None
_end_date: Optional[dt] = None
_start_date: Optional[dt] = PrivateAttr(default=None)
_end_date: Optional[dt] = PrivateAttr(default=None)

def __init__(self, **data: Any) -> None:
super().__init__(**data)
# Copy class variables to the instance variables
self._start_date = getattr(self.__class__, "_start_date_cls", None)
self._end_date = getattr(self.__class__, "_end_date_cls", None)
# Reset class variables
self.__class__._start_date_cls = None
self.__class__._end_date_cls = None

# Properties to return the private values
@property
Expand Down Expand Up @@ -133,8 +149,8 @@ def validate_datetime(cls, value: Optional[str]) -> Optional[str]:
)

# Store the parsed dates
cls._start_date = dates[0]
cls._end_date = dates[1]
cls._start_date_cls = dates[0]
cls._end_date_cls = dates[1]
# Return the original string value
return value

Expand Down
4 changes: 4 additions & 0 deletions tests/api/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def test_temporal_search_two_tailed():
assert search.start_date is None
assert search.end_date == utcnow

search = Search(collections=["collection1"])
assert search.start_date is None
assert search.end_date is None


def test_temporal_search_open():
# Test open date range
Expand Down