Skip to content

Commit

Permalink
Set strict to True when parsing dates in webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-awala committed Aug 18, 2023
1 parent 46aa429 commit 88d0ba5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
5 changes: 3 additions & 2 deletions airflow/utils/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,17 @@ def datetime(*args, **kwargs):
return dt.datetime(*args, **kwargs)


def parse(string: str, timezone=None) -> DateTime:
def parse(string: str, timezone=None, strict=False) -> DateTime:
"""
Parse a time string and return an aware datetime.
:param string: time string
:param timezone: the timezone
:param strict: if False, it will fall back on the dateutil parser if unable to parse with pendulum
"""
from airflow.settings import TIMEZONE

return pendulum.parse(string, tz=timezone or TIMEZONE, strict=False) # type: ignore
return pendulum.parse(string, tz=timezone or TIMEZONE, strict=strict) # type: ignore


@overload
Expand Down
11 changes: 6 additions & 5 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,18 @@ def get_date_time_num_runs_dag_runs_form_data(www_request, session, dag):
}


def _safe_parse_datetime(v, allow_empty=False) -> datetime.datetime | None:
def _safe_parse_datetime(v, allow_empty=False, strict=True) -> datetime.datetime | None:
"""
Parse datetime and return error message for invalid dates.
:param v: the string value to be parsed
:param allow_empty: Set True to return none if empty str or None
:param strict: if False, it will fall back on the dateutil parser if unable to parse with pendulum
"""
if allow_empty is True and not v:
return None
try:
return timezone.parse(v)
return timezone.parse(v, strict=strict)
except (TypeError, ParserError):
abort(400, f"Invalid datetime: {v!r}")

Expand Down Expand Up @@ -1630,7 +1631,7 @@ def get_logs_with_metadata(self, session: Session = NEW_SESSION):

# Convert string datetime into actual datetime
try:
execution_date = timezone.parse(execution_date_str)
execution_date = timezone.parse(execution_date_str, strict=True)
except ValueError:
error_message = (
f"Given execution date, {execution_date}, could not be identified as a date. "
Expand Down Expand Up @@ -2103,7 +2104,7 @@ def trigger(self, dag_id: str, session: Session = NEW_SESSION):
)

try:
execution_date = timezone.parse(request_execution_date)
execution_date = timezone.parse(request_execution_date, strict=True)
except ParserError:
flash("Invalid execution date", "error")
form = DateTimeForm(data={"execution_date": timezone.utcnow().isoformat()})
Expand Down Expand Up @@ -3699,7 +3700,7 @@ def grid_data(self):
num_runs = conf.getint("webserver", "default_dag_run_display_number")

try:
base_date = timezone.parse(request.args["base_date"])
base_date = timezone.parse(request.args["base_date"], strict=True)
except (KeyError, ValueError):
base_date = dag.get_latest_execution_date() or timezone.utcnow()

Expand Down

0 comments on commit 88d0ba5

Please sign in to comment.