From b619a7dd784beb6ad21b8d4b1ea17b2b773bb26e Mon Sep 17 00:00:00 2001 From: Paul Schreiber Date: Thu, 12 Oct 2023 11:32:15 -0400 Subject: [PATCH] fix: make audir log timestamps timezone-aware when needed (#889) https://stackoverflow.com/questions/18622007/runtimewarning-datetimefield-received-a-naive-datetime --- terraso_backend/apps/audit_logs/services.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/terraso_backend/apps/audit_logs/services.py b/terraso_backend/apps/audit_logs/services.py index 24a1e963f..7ae6b478a 100644 --- a/terraso_backend/apps/audit_logs/services.py +++ b/terraso_backend/apps/audit_logs/services.py @@ -2,6 +2,7 @@ from datetime import datetime from enum import Enum +from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.core.paginator import Paginator from django.db import transaction @@ -79,7 +80,12 @@ def log( if client_time is None: client_time = datetime.now() - log.client_timestamp = client_time + if settings.USE_TZ: + from django.utils.timezone import make_aware + + log.client_timestamp = make_aware(client_time) + else: + log.client_timestamp = client_time log.metadata = metadata log.save()