Replace the django.utils.timezone.utc by datetime.timezone.utc (#448)

Co-authored-by: Alieh Rymašeŭski <alieh.rymasheuski@gmail.com>
This commit is contained in:
Hasan Ramezani 2022-11-07 16:56:51 +03:30 committed by GitHub
parent 36eaaaa2a9
commit 2b0bc9efa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 13 deletions

View file

@ -7,6 +7,7 @@
- feat: New context manager `disable_auditlog` to turn off logging and a new setting `AUDITLOG_DISABLE_ON_RAW_SAVE`
to disable it during raw-save operations like loaddata. [#446](https://github.com/jazzband/django-auditlog/pull/446)
- Python: Confirm Python 3.11 support ([#447](https://github.com/jazzband/django-auditlog/pull/447))
- feat: Replace the `django.utils.timezone.utc` by `datetime.timezone.utc`. [#448](https://github.com/jazzband/django-auditlog/pull/448)
#### Fixes

View file

@ -1,7 +1,9 @@
from datetime import timezone
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import NOT_PROVIDED, DateTimeField, JSONField, Model
from django.utils import timezone
from django.utils import timezone as django_timezone
from django.utils.encoding import smart_str
@ -63,8 +65,12 @@ def get_field_value(obj, field):
# DateTimeFields are timezone-aware, so we need to convert the field
# to its naive form before we can accurately compare them for changes.
value = field.to_python(getattr(obj, field.name, None))
if value is not None and settings.USE_TZ and not timezone.is_naive(value):
value = timezone.make_naive(value, timezone=timezone.utc)
if (
value is not None
and settings.USE_TZ
and not django_timezone.is_naive(value)
):
value = django_timezone.make_naive(value, timezone=timezone.utc)
elif isinstance(field, JSONField):
value = field.to_python(getattr(obj, field.name, None))
else:

View file

@ -1,6 +1,7 @@
import ast
import json
from copy import deepcopy
from datetime import timezone
from typing import Any, Dict, List
from dateutil import parser
@ -12,7 +13,7 @@ from django.core import serializers
from django.core.exceptions import FieldDoesNotExist
from django.db import DEFAULT_DB_ALIAS, models
from django.db.models import Q, QuerySet
from django.utils import formats, timezone
from django.utils import formats
from django.utils.encoding import smart_str
from django.utils.translation import gettext_lazy as _

View file

@ -2,6 +2,7 @@ import datetime
import itertools
import json
import warnings
from datetime import timezone
from unittest import mock
import freezegun
@ -16,7 +17,8 @@ from django.core import management
from django.db.models.signals import pre_save
from django.test import RequestFactory, TestCase, override_settings
from django.urls import reverse
from django.utils import dateformat, formats, timezone
from django.utils import dateformat, formats
from django.utils import timezone as django_timezone
from auditlog.admin import LogEntryAdmin
from auditlog.context import disable_auditlog, set_actor
@ -618,8 +620,8 @@ class AdditionalDataModelTest(TestCase):
class DateTimeFieldModelTest(TestCase):
"""Tests if DateTimeField changes are recognised correctly"""
utc_plus_one = timezone.get_fixed_timezone(datetime.timedelta(hours=1))
now = timezone.now()
utc_plus_one = django_timezone.get_fixed_timezone(datetime.timedelta(hours=1))
now = django_timezone.now()
def setUp(self):
super().setUp()
@ -788,7 +790,7 @@ class DateTimeFieldModelTest(TestCase):
" DATETIME_FORMAT"
),
)
timestamp = timezone.now()
timestamp = django_timezone.now()
dtm.timestamp = timestamp
dtm.save()
localized_timestamp = timestamp.astimezone(gettz(settings.TIME_ZONE))
@ -912,7 +914,9 @@ class DateTimeFieldModelTest(TestCase):
dtm.save()
# Change with naive field doesnt raise error
dtm.naive_dt = timezone.make_naive(timezone.now(), timezone=timezone.utc)
dtm.naive_dt = django_timezone.make_naive(
django_timezone.now(), timezone=timezone.utc
)
dtm.save()
@ -1588,7 +1592,7 @@ class ModelInstanceDiffTest(TestCase):
class TestModelSerialization(TestCase):
def setUp(self):
super().setUp()
self.test_date = datetime.datetime(2022, 1, 1, 12, tzinfo=datetime.timezone.utc)
self.test_date = datetime.datetime(2022, 1, 1, 12, tzinfo=timezone.utc)
self.test_date_string = datetime.datetime.strftime(
self.test_date, "%Y-%m-%dT%XZ"
)
@ -1843,7 +1847,9 @@ class DisableTest(TestCase):
def test_create(self):
# Mimic the way imports create objects
inst = SimpleModel(
text="I am a bit more difficult.", boolean=False, datetime=timezone.now()
text="I am a bit more difficult.",
boolean=False,
datetime=django_timezone.now(),
)
SimpleModel.save_base(inst, raw=True)
self.assertEqual(0, LogEntry.objects.get_for_object(inst).count())
@ -1855,7 +1861,9 @@ class DisableTest(TestCase):
def test_update(self):
inst = SimpleModel(
text="I am a bit more difficult.", boolean=False, datetime=timezone.now()
text="I am a bit more difficult.",
boolean=False,
datetime=django_timezone.now(),
)
SimpleModel.save_base(inst, raw=True)
inst.text = "I feel refreshed"
@ -1864,7 +1872,9 @@ class DisableTest(TestCase):
def test_update_with_context_manager(self):
inst = SimpleModel(
text="I am a bit more difficult.", boolean=False, datetime=timezone.now()
text="I am a bit more difficult.",
boolean=False,
datetime=django_timezone.now(),
)
SimpleModel.save_base(inst, raw=True)
with disable_auditlog():