Skip to content

armgit / fli

Public Imported from GitHub
0

Google Flights MCP, CLI and Python Library

Update DateSearchFilters to bump up from_date to today if it's in the past

Punit Arani84f242d199c4
diff --git a/fli/models/google_flights.py b/fli/models/google_flights.py
index 7f2dd2a..aeba796 100644
--- a/fli/models/google_flights.py
+++ b/fli/models/google_flights.py
@@ -382,10 +382,7 @@ class DateSearchFilters(BaseModel):
def validate_date_order(cls, v: str, info: ValidationInfo) -> str:
"""Validate and adjust the date range constraints."""
if info.field_name == "from_date":
- # If from_date is in the past, set it to today
- from_date = datetime.strptime(v, "%Y-%m-%d").date()
- if from_date < datetime.now().date():
- return datetime.now().date().strftime("%Y-%m-%d")
+ # Remove the past date check since we'll handle it in model validator
return v
# For to_date, check if it's after from_date
@@ -422,6 +419,17 @@ class DateSearchFilters(BaseModel):
)
return self
+ @model_validator(mode="after")
+ def validate_and_adjust_from_date(self) -> "DateSearchFilters":
+ """Adjust from_date to current date if it's in the past."""
+ from_date = self.parsed_from_date.date()
+ current_date = datetime.now().date()
+
+ if from_date < current_date:
+ self.from_date = current_date.strftime("%Y-%m-%d")
+
+ return self
+
def format(self) -> list:
"""Format filters into Google Flights API structure.
diff --git a/tests/models/test_date_search_filters_validation.py b/tests/models/test_date_search_filters_validation.py
index 34c4c31..79e241e 100644
--- a/tests/models/test_date_search_filters_validation.py
+++ b/tests/models/test_date_search_filters_validation.py
@@ -131,6 +131,23 @@ def test_date_search_today_to_date(basic_search_params, future_date):
)
+def test_date_search_past_from_date_after_swap(basic_search_params, future_date):
+ """Test DateSearchFilters bumps up from_date to current date after date swap."""
+ past_date = datetime.now() - timedelta(days=7)
+ later_date = future_date + timedelta(days=7)
+
+ # Create with reversed dates where from_date is in the future but will be swapped with past date
+ filters = DateSearchFilters(
+ **basic_search_params,
+ from_date=later_date.strftime("%Y-%m-%d"),
+ to_date=past_date.strftime("%Y-%m-%d"),
+ )
+
+ # After swap and adjustment, from_date should be today and to_date should be the later date
+ assert filters.from_date == datetime.now().date().strftime("%Y-%m-%d")
+ assert filters.to_date == later_date.strftime("%Y-%m-%d")
+
+
def test_date_search_segment_outside_range(basic_search_params, future_date):
"""Test DateSearchFilters validates flight segment dates are within range."""
from_date = future_date