From 4ae70464f6b904860cf051c5aabacc259cc59df3 Mon Sep 17 00:00:00 2001 From: Stephen McMurtry Date: Tue, 25 Jul 2023 16:23:31 -0400 Subject: [PATCH] Make the validator fns for JobSchema work with both timezone aware and timezone naive datetimes (#1942) - make the validator functions for the marshmallow JobSchema work for timezone-aware and timezone-naive times, since these both have a .timestamp() method that converts a datetime to a number of seconds since Jan 1, 1970 UTC time: https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp - remove some unused validator functions that looked like they contained the same bug --- app/schemas.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/app/schemas.py b/app/schemas.py index 11216c17a3..75a6a2e78f 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -39,9 +39,10 @@ def _validate_positive_number(value, msg="Not a positive integer"): raise ValidationError(msg) -def _validate_datetime_not_too_far_in_future(dte): +def _validate_datetime_not_too_far_in_future(dte: datetime): max_hours = current_app.config["JOBS_MAX_SCHEDULE_HOURS_AHEAD"] - if dte > datetime.utcnow() + timedelta(hours=max_hours): + max_schedule_time = datetime.utcnow() + timedelta(hours=max_hours) + if dte.timestamp() > max_schedule_time.timestamp(): msg = f"Date cannot be more than {max_hours} hours in the future" raise ValidationError(msg) @@ -51,18 +52,8 @@ def _validate_not_in_future(dte, msg="Date cannot be in the future"): raise ValidationError(msg) -def _validate_not_in_past(dte, msg="Date cannot be in the past"): - if dte < date.today(): - raise ValidationError(msg) - - -def _validate_datetime_not_in_future(dte, msg="Date cannot be in the future"): - if dte > datetime.utcnow(): - raise ValidationError(msg) - - -def _validate_datetime_not_in_past(dte, msg="Date cannot be in the past"): - if dte < datetime.utcnow(): +def _validate_datetime_not_in_past(dte: datetime, msg="Date cannot be in the past"): + if dte.timestamp() < datetime.utcnow().timestamp(): raise ValidationError(msg)