Skip to content

Commit

Permalink
AAP-31447: reports: use django.utils.timezone.now() to get the curren…
Browse files Browse the repository at this point in the history
…t day (#1311)

We want to use the `django.utils.timezone.now()` to get a date with UTC TZ.
  • Loading branch information
goneri authored Sep 18, 2024
1 parent bbfc63a commit 9f010b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 9 additions & 4 deletions ansible_ai_connect/users/reports/postman.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from urllib import parse

from django.conf import settings
from django.utils import timezone
from oauth2client.service_account import ServiceAccountCredentials
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
Expand Down Expand Up @@ -213,15 +214,19 @@ def get_drive(self) -> GoogleDrive:

return GoogleDrive(gauth)

@staticmethod
def create_filename(title: str, report_date: datetime):
if report_date.tzinfo is None:
raise ValueError
return f"{report_date.strftime('%Y%m%d')}_{title}.csv"

def send_reports(self, reports: Reports):
drive = self.get_drive()
folder_id = self.get_folder_id(drive)
file_name_prefix = (
reports.created_before if reports.created_before else datetime.now()
).strftime("%Y%m%d")
report_date = reports.created_before or timezone.now()

for report in reports.data:
file_name = f"{file_name_prefix}_{report.title}.csv"
file_name = self.create_filename(report.title, report_date)
file = drive.CreateFile({"parents": [{"id": folder_id}], "title": file_name})
file.SetContentString(report.data)
file.Upload()
Expand Down
12 changes: 11 additions & 1 deletion ansible_ai_connect/users/reports/tests/test_postman.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import re
from abc import abstractmethod
from datetime import datetime
from unittest.mock import Mock, patch

from django.test import override_settings
from django.utils import timezone
from oauth2client.service_account import ServiceAccountCredentials
from slack_sdk.errors import SlackApiError

Expand Down Expand Up @@ -243,7 +245,7 @@ def test_send_reports(self, sa_credentials, g_drive, g_auth):
ServiceAccountCredentials, "from_json_keyfile_dict", return_value={"credentials": "secret"}
)
def test_send_reports_with_created_before(self, sa_credentials, g_drive, g_auth):
created_before = datetime.fromisoformat("20241231")
created_before = datetime.fromisoformat("2024-12-31T00:00:00Z")
reports: Reports = Reports(data=[Report("title", "data")], created_before=created_before)
self.assert_send_reports(sa_credentials, g_drive, g_auth, reports, created_before)

Expand All @@ -260,3 +262,11 @@ def test_send_reports_with_missing_folder(self, *args, **kwargs):
with self.assertLogs(logger="root", level="INFO") as log:
postman.send_reports(reports)
self.assertInLog("Unable to locate folder", log)

def test_create_filename(self):
filename = GoogleDrivePostman.create_filename("my_report", timezone.now())
self.assertTrue(re.match(r"^20\d\d\d\d\d\d_my_report.csv$", filename))

def test_create_filename_with_invalid_dt(self):
with self.assertRaises(ValueError):
GoogleDrivePostman.create_filename("my_report", datetime.now())

0 comments on commit 9f010b1

Please sign in to comment.