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

We want to use the `django.utils.timezone.now()` to get a date with UTC TZ.
  • Loading branch information
goneri committed Sep 17, 2024
1 parent ba0feae commit 26ee2f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 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,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()
Expand Down
6 changes: 6 additions & 0 deletions 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 @@ -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))

0 comments on commit 26ee2f7

Please sign in to comment.