Skip to content

Commit

Permalink
Make the validator fns for JobSchema work with both timezone aware an…
Browse files Browse the repository at this point in the history
…d 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
  • Loading branch information
smcmurtry authored Jul 25, 2023
1 parent 38e6be7 commit 4ae7046
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)


Expand Down

0 comments on commit 4ae7046

Please sign in to comment.