From 50ccd994d000f7c7b456441aa63f1738bf625c30 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 30 Nov 2023 14:41:44 -0800 Subject: [PATCH 1/3] fix scheduled jobs time --- app/notify_client/job_api_client.py | 20 ++++++++++++++++++++ app/utils/time.py | 6 ++++++ tests/app/notify_client/test_job_client.py | 12 ++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index ac1cdfd9b..bd4400177 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -1,5 +1,9 @@ +import datetime +from zoneinfo import ZoneInfo + from app.extensions import redis_client from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache +from app.utils.csv import get_user_preferred_timezone class JobApiClient(NotifyAdminAPIClient): @@ -86,10 +90,26 @@ class JobApiClient(NotifyAdminAPIClient): def has_jobs(self, service_id): return bool(self.get_jobs(service_id)["data"]) + @classmethod + def convert_user_time_to_utc(cls, scheduled_for): + user_preferred_tz = get_user_preferred_timezone() + + user_date = datetime.datetime.fromisoformat(scheduled_for) + scheduled_for = ( + user_date.replace(tzinfo=ZoneInfo(user_preferred_tz)) + .astimezone(ZoneInfo("UTC")) + .strftime("%Y-%m-%dT%H:%M:%S") + ) + + return scheduled_for + def create_job(self, job_id, service_id, scheduled_for=None): data = {"id": job_id} + # make a datetime object in the user's preferred timezone + if scheduled_for: + scheduled_for = self.convert_user_time_to_utc(scheduled_for) data.update({"scheduled_for": scheduled_for}) data = _attach_current_user(data) diff --git a/app/utils/time.py b/app/utils/time.py index 9ae24b1bd..b658fba56 100644 --- a/app/utils/time.py +++ b/app/utils/time.py @@ -22,3 +22,9 @@ def is_less_than_days_ago(date_from_db, number_of_days): def parse_naive_dt(dt): return parser.parse(dt, ignoretz=True) + + +def hilite(message): + ansi_green = "\033[32m" + ansi_reset = "\033[0m" + return f"{ansi_green}{message}{ansi_reset}" diff --git a/tests/app/notify_client/test_job_client.py b/tests/app/notify_client/test_job_client.py index 7cec603c6..6f76b1786 100644 --- a/tests/app/notify_client/test_job_client.py +++ b/tests/app/notify_client/test_job_client.py @@ -32,16 +32,24 @@ def test_client_creates_job_data_correctly(mocker, fake_uuid): ) +def test_convert_user_time_to_utc(): + original_time = "2023-12-01T12:00:00" + utc_time = JobApiClient.convert_user_time_to_utc(original_time) + assert utc_time == "2023-12-01T17:00:00" + + def test_client_schedules_job(mocker, fake_uuid): mocker.patch("app.notify_client.current_user", id="1") mock_post = mocker.patch("app.notify_client.job_api_client.JobApiClient.post") - when = "2016-08-25T13:04:21.767198" + # The default timezone is US/Easter which is off by 4 hours in the summer from UTC + when_in_utc = "2016-08-25T17:04:21" + when = "2016-08-25T13:04:21" JobApiClient().create_job(fake_uuid, 1, scheduled_for=when) - assert mock_post.call_args[1]["data"]["scheduled_for"] == when + assert mock_post.call_args[1]["data"]["scheduled_for"] == when_in_utc def test_client_gets_job_by_service_and_job(mocker): From 9ee61806f3a9335477c92b17c7e55727d925879c Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Thu, 30 Nov 2023 14:48:06 -0800 Subject: [PATCH 2/3] fix class method --- app/notify_client/job_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index bd4400177..5ea333993 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -109,7 +109,7 @@ class JobApiClient(NotifyAdminAPIClient): # make a datetime object in the user's preferred timezone if scheduled_for: - scheduled_for = self.convert_user_time_to_utc(scheduled_for) + scheduled_for = JobApiClient.convert_user_time_to_utc(scheduled_for) data.update({"scheduled_for": scheduled_for}) data = _attach_current_user(data) From f2355577736480aa3921056544246571ebcb4f18 Mon Sep 17 00:00:00 2001 From: Kenneth Kehl <@kkehl@flexion.us> Date: Fri, 1 Dec 2023 11:14:19 -0800 Subject: [PATCH 3/3] move hilite method as per code review feedback --- app/utils/__init__.py | 8 ++++++++ app/utils/time.py | 6 ------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/utils/__init__.py b/app/utils/__init__.py index 814ce150c..d5905b712 100644 --- a/app/utils/__init__.py +++ b/app/utils/__init__.py @@ -117,6 +117,14 @@ def hide_from_search_engines(f): return decorated_function +# Function used for debugging. +# Do print(hilite(message)) while debugging, then remove your print statements +def hilite(message): + ansi_green = "\033[32m" + ansi_reset = "\033[0m" + return f"{ansi_green}{message}{ansi_reset}" + + # Function to merge two dict or lists with a JSON-like structure into one. # JSON-like means they can contain all types JSON can: all the main primitives # plus nested lists or dictionaries. diff --git a/app/utils/time.py b/app/utils/time.py index b658fba56..9ae24b1bd 100644 --- a/app/utils/time.py +++ b/app/utils/time.py @@ -22,9 +22,3 @@ def is_less_than_days_ago(date_from_db, number_of_days): def parse_naive_dt(dt): return parser.parse(dt, ignoretz=True) - - -def hilite(message): - ansi_green = "\033[32m" - ansi_reset = "\033[0m" - return f"{ansi_green}{message}{ansi_reset}"