Skip to content

Commit 5d91356

Browse files
authored
Changed timezone.now import to from django.utils import timezone (#643)
* Changed timezone.now import to `from django.utils import timezone` * Added author and changes
1 parent 8bcf4c4 commit 5d91356

File tree

5 files changed

+24
-22
lines changed

5 files changed

+24
-22
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Authors
4343
- Guillermo Eijo (`guilleijo <https://github.com/guilleijo>`_)
4444
- Hamish Downer
4545
- Hanyin Zhang
46+
- Hernan Esteves (`sevetseh28 <https://github.com/sevetseh28>`_)
4647
- Jack Cushman (`jcushman <https://github.com/jcushman>`_)
4748
- James Muranga (`jamesmura <https://github.com/jamesmura>`_)
4849
- James Pulec

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changes
33

44
Unreleased
55
----------
6+
- Changed how `now` is imported from `timezone` (`timezone` module is imported now)
67
- Add simple filtering if provided a minutes argument in `clean_duplicate_history` (gh-606)
78
- Add setting to convert `FileField` to `CharField` instead of `TextField` (gh-623)
89
- import model `ContentType` in `SimpleHistoryAdmin` using `django_apps.get_model`

simple_history/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import unicode_literals
22

33
from django.db import models
4-
from django.utils.timezone import now
4+
from django.utils import timezone
55

66

77
class HistoryDescriptor(object):
@@ -104,7 +104,7 @@ def bulk_history_create(self, objs, batch_size=None):
104104
historical_instances = []
105105
for instance in objs:
106106
row = self.model(
107-
history_date=getattr(instance, "_history_date", now()),
107+
history_date=getattr(instance, "_history_date", timezone.now()),
108108
history_user=getattr(instance, "_history_user", None),
109109
history_change_reason=getattr(instance, "changeReason", ""),
110110
history_type="+",

simple_history/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from django.forms.models import model_to_dict
2020
from django.urls import reverse
2121
from django.utils.text import format_lazy
22-
from django.utils.timezone import now
22+
from django.utils import timezone
2323
from simple_history import utils
2424
from . import exceptions
2525
from .manager import HistoryDescriptor
@@ -477,7 +477,7 @@ def post_delete(self, instance, using=None, **kwargs):
477477

478478
def create_historical_record(self, instance, history_type, using=None):
479479
using = using if self.use_base_model_db else None
480-
history_date = getattr(instance, "_history_date", now())
480+
history_date = getattr(instance, "_history_date", timezone.now())
481481
history_user = self.get_history_user(instance)
482482
history_change_reason = getattr(instance, "changeReason", None)
483483
manager = getattr(instance, self.manager_name)

simple_history/tests/tests/test_utils.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.db import IntegrityError
22
from django.test import TestCase, TransactionTestCase
3-
from django.utils.timezone import now
3+
from django.utils import timezone
44
from mock import Mock, patch
55

66
from simple_history.exceptions import NotHistoricalModelError
@@ -17,18 +17,18 @@
1717
class BulkCreateWithHistoryTestCase(TestCase):
1818
def setUp(self):
1919
self.data = [
20-
Poll(id=1, question="Question 1", pub_date=now()),
21-
Poll(id=2, question="Question 2", pub_date=now()),
22-
Poll(id=3, question="Question 3", pub_date=now()),
23-
Poll(id=4, question="Question 4", pub_date=now()),
24-
Poll(id=5, question="Question 5", pub_date=now()),
20+
Poll(id=1, question="Question 1", pub_date=timezone.now()),
21+
Poll(id=2, question="Question 2", pub_date=timezone.now()),
22+
Poll(id=3, question="Question 3", pub_date=timezone.now()),
23+
Poll(id=4, question="Question 4", pub_date=timezone.now()),
24+
Poll(id=5, question="Question 5", pub_date=timezone.now()),
2525
]
2626
self.data_with_excluded_fields = [
27-
PollWithExcludeFields(id=1, question="Question 1", pub_date=now()),
28-
PollWithExcludeFields(id=2, question="Question 2", pub_date=now()),
29-
PollWithExcludeFields(id=3, question="Question 3", pub_date=now()),
30-
PollWithExcludeFields(id=4, question="Question 4", pub_date=now()),
31-
PollWithExcludeFields(id=5, question="Question 5", pub_date=now()),
27+
PollWithExcludeFields(id=1, question="Question 1", pub_date=timezone.now()),
28+
PollWithExcludeFields(id=2, question="Question 2", pub_date=timezone.now()),
29+
PollWithExcludeFields(id=3, question="Question 3", pub_date=timezone.now()),
30+
PollWithExcludeFields(id=4, question="Question 4", pub_date=timezone.now()),
31+
PollWithExcludeFields(id=5, question="Question 5", pub_date=timezone.now()),
3232
]
3333

3434
def test_bulk_create_history(self):
@@ -84,11 +84,11 @@ def test_bulk_create_history_with_relation_name(self):
8484
class BulkCreateWithHistoryTransactionTestCase(TransactionTestCase):
8585
def setUp(self):
8686
self.data = [
87-
Poll(id=1, question="Question 1", pub_date=now()),
88-
Poll(id=2, question="Question 2", pub_date=now()),
89-
Poll(id=3, question="Question 3", pub_date=now()),
90-
Poll(id=4, question="Question 4", pub_date=now()),
91-
Poll(id=5, question="Question 5", pub_date=now()),
87+
Poll(id=1, question="Question 1", pub_date=timezone.now()),
88+
Poll(id=2, question="Question 2", pub_date=timezone.now()),
89+
Poll(id=3, question="Question 3", pub_date=timezone.now()),
90+
Poll(id=4, question="Question 4", pub_date=timezone.now()),
91+
Poll(id=5, question="Question 5", pub_date=timezone.now()),
9292
]
9393

9494
@patch(
@@ -112,7 +112,7 @@ def test_bulk_create_history_on_objects_that_already_exist(self):
112112
self.assertEqual(Poll.history.count(), 0)
113113

114114
def test_bulk_create_history_rolls_back_when_last_exists(self):
115-
Poll.objects.create(id=5, question="Question 5", pub_date=now())
115+
Poll.objects.create(id=5, question="Question 5", pub_date=timezone.now())
116116

117117
self.assertEqual(Poll.objects.count(), 1)
118118
self.assertEqual(Poll.history.count(), 1)
@@ -149,7 +149,7 @@ def test_bulk_create_no_ids_return(self, hist_manager_mock):
149149
class UpdateChangeReasonTestCase(TestCase):
150150
def test_update_change_reason_with_excluded_fields(self):
151151
poll = PollWithExcludeFields(
152-
question="what's up?", pub_date=now(), place="The Pub"
152+
question="what's up?", pub_date=timezone.now(), place="The Pub"
153153
)
154154
poll.save()
155155
update_change_reason(poll, "Test change reason.")

0 commit comments

Comments
 (0)