diff --git a/ansible_ai_connect/users/reports/postman.py b/ansible_ai_connect/users/reports/postman.py index b33cf65c2..51bd7ffa6 100644 --- a/ansible_ai_connect/users/reports/postman.py +++ b/ansible_ai_connect/users/reports/postman.py @@ -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 @@ -213,15 +214,17 @@ def get_drive(self) -> GoogleDrive: return GoogleDrive(gauth) + @staticmethod + def create_filename(title: str, report_date: datetime): + 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() diff --git a/ansible_ai_connect/users/reports/tests/test_postman.py b/ansible_ai_connect/users/reports/tests/test_postman.py index 533eb98dc..fcd00e820 100644 --- a/ansible_ai_connect/users/reports/tests/test_postman.py +++ b/ansible_ai_connect/users/reports/tests/test_postman.py @@ -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 @@ -260,3 +262,7 @@ 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))