Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed timezone.now import to from django.utils import timezone #643

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions simple_history/manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import unicode_literals

from django.db import models
from django.utils.timezone import now
from django.utils import timezone


class HistoryDescriptor(object):
Expand Down Expand Up @@ -104,7 +104,7 @@ def bulk_history_create(self, objs, batch_size=None):
historical_instances = []
for instance in objs:
row = self.model(
history_date=getattr(instance, "_history_date", now()),
history_date=getattr(instance, "_history_date", timezone.now()),
history_user=getattr(instance, "_history_user", None),
history_change_reason=getattr(instance, "changeReason", ""),
history_type="+",
Expand Down
4 changes: 2 additions & 2 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from django.forms.models import model_to_dict
from django.urls import reverse
from django.utils.text import format_lazy
from django.utils.timezone import now
from django.utils import timezone
from simple_history import utils
from . import exceptions
from .manager import HistoryDescriptor
Expand Down Expand Up @@ -477,7 +477,7 @@ def post_delete(self, instance, using=None, **kwargs):

def create_historical_record(self, instance, history_type, using=None):
using = using if self.use_base_model_db else None
history_date = getattr(instance, "_history_date", now())
history_date = getattr(instance, "_history_date", timezone.now())
history_user = self.get_history_user(instance)
history_change_reason = getattr(instance, "changeReason", None)
manager = getattr(instance, self.manager_name)
Expand Down
36 changes: 18 additions & 18 deletions simple_history/tests/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import IntegrityError
from django.test import TestCase, TransactionTestCase
from django.utils.timezone import now
from django.utils import timezone
from mock import Mock, patch

from simple_history.exceptions import NotHistoricalModelError
Expand All @@ -17,18 +17,18 @@
class BulkCreateWithHistoryTestCase(TestCase):
def setUp(self):
self.data = [
Poll(id=1, question="Question 1", pub_date=now()),
Poll(id=2, question="Question 2", pub_date=now()),
Poll(id=3, question="Question 3", pub_date=now()),
Poll(id=4, question="Question 4", pub_date=now()),
Poll(id=5, question="Question 5", pub_date=now()),
Poll(id=1, question="Question 1", pub_date=timezone.now()),
Poll(id=2, question="Question 2", pub_date=timezone.now()),
Poll(id=3, question="Question 3", pub_date=timezone.now()),
Poll(id=4, question="Question 4", pub_date=timezone.now()),
Poll(id=5, question="Question 5", pub_date=timezone.now()),
]
self.data_with_excluded_fields = [
PollWithExcludeFields(id=1, question="Question 1", pub_date=now()),
PollWithExcludeFields(id=2, question="Question 2", pub_date=now()),
PollWithExcludeFields(id=3, question="Question 3", pub_date=now()),
PollWithExcludeFields(id=4, question="Question 4", pub_date=now()),
PollWithExcludeFields(id=5, question="Question 5", pub_date=now()),
PollWithExcludeFields(id=1, question="Question 1", pub_date=timezone.now()),
PollWithExcludeFields(id=2, question="Question 2", pub_date=timezone.now()),
PollWithExcludeFields(id=3, question="Question 3", pub_date=timezone.now()),
PollWithExcludeFields(id=4, question="Question 4", pub_date=timezone.now()),
PollWithExcludeFields(id=5, question="Question 5", pub_date=timezone.now()),
]

def test_bulk_create_history(self):
Expand Down Expand Up @@ -84,11 +84,11 @@ def test_bulk_create_history_with_relation_name(self):
class BulkCreateWithHistoryTransactionTestCase(TransactionTestCase):
def setUp(self):
self.data = [
Poll(id=1, question="Question 1", pub_date=now()),
Poll(id=2, question="Question 2", pub_date=now()),
Poll(id=3, question="Question 3", pub_date=now()),
Poll(id=4, question="Question 4", pub_date=now()),
Poll(id=5, question="Question 5", pub_date=now()),
Poll(id=1, question="Question 1", pub_date=timezone.now()),
Poll(id=2, question="Question 2", pub_date=timezone.now()),
Poll(id=3, question="Question 3", pub_date=timezone.now()),
Poll(id=4, question="Question 4", pub_date=timezone.now()),
Poll(id=5, question="Question 5", pub_date=timezone.now()),
]

@patch(
Expand All @@ -112,7 +112,7 @@ def test_bulk_create_history_on_objects_that_already_exist(self):
self.assertEqual(Poll.history.count(), 0)

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

self.assertEqual(Poll.objects.count(), 1)
self.assertEqual(Poll.history.count(), 1)
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_bulk_create_no_ids_return(self, hist_manager_mock):
class UpdateChangeReasonTestCase(TestCase):
def test_update_change_reason_with_excluded_fields(self):
poll = PollWithExcludeFields(
question="what's up?", pub_date=now(), place="The Pub"
question="what's up?", pub_date=timezone.now(), place="The Pub"
)
poll.save()
update_change_reason(poll, "Test change reason.")
Expand Down