Skip to content

Commit

Permalink
Made skip_history_when_saving work when creating
Browse files Browse the repository at this point in the history
...a model object - not just when updating an object.

Also fixed an unrelated `PollWithManyToMany` instance being used in the
assertions in the existing test for `skip_history_when_saving`.
  • Loading branch information
ddabble committed Feb 19, 2024
1 parent ac44d22 commit 0d9da42
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes
Unreleased
----------

- Made ``skip_history_when_saving`` work when creating an object - not just when
updating an object (gh-1262)

3.5.0 (2024-02-19)
------------------
Expand Down
2 changes: 1 addition & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def get_meta_options(self, model):
def post_save(self, instance, created, using=None, **kwargs):
if not getattr(settings, "SIMPLE_HISTORY_ENABLED", True):
return
if not created and hasattr(instance, "skip_history_when_saving"):
if getattr(instance, "skip_history_when_saving", False):
return
if not kwargs.get("raw", False):
self.create_historical_record(instance, created and "+" or "~", using=using)
Expand Down
22 changes: 17 additions & 5 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2177,21 +2177,21 @@ def test_m2m_relation(self):
self.assertEqual(self.poll.history.all()[0].places.count(), 0)
self.assertEqual(poll_2.history.all()[0].places.count(), 2)

def test_skip_history(self):
def test_skip_history_when_updating_an_object(self):
skip_poll = PollWithManyToMany.objects.create(
question="skip history?", pub_date=today
)
self.assertEqual(self.poll.history.all().count(), 1)
self.assertEqual(self.poll.history.all()[0].places.count(), 0)
self.assertEqual(skip_poll.history.all().count(), 1)
self.assertEqual(skip_poll.history.all()[0].places.count(), 0)

skip_poll.skip_history_when_saving = True

skip_poll.question = "huh?"
skip_poll.save()
skip_poll.places.add(self.place)

self.assertEqual(self.poll.history.all().count(), 1)
self.assertEqual(self.poll.history.all()[0].places.count(), 0)
self.assertEqual(skip_poll.history.all().count(), 1)
self.assertEqual(skip_poll.history.all()[0].places.count(), 0)

del skip_poll.skip_history_when_saving
place_2 = Place.objects.create(name="Place 2")
Expand All @@ -2201,6 +2201,18 @@ def test_skip_history(self):
self.assertEqual(skip_poll.history.all().count(), 2)
self.assertEqual(skip_poll.history.all()[0].places.count(), 2)

def test_skip_history_when_creating_an_object(self):
initial_poll_count = PollWithManyToMany.objects.count()

skip_poll = PollWithManyToMany(question="skip history?", pub_date=today)
skip_poll.skip_history_when_saving = True
skip_poll.save()
skip_poll.places.add(self.place)

self.assertEqual(skip_poll.history.all().count(), 0)
self.assertEqual(PollWithManyToMany.objects.count(), initial_poll_count + 1)
self.assertEqual(skip_poll.places.count(), 1)

def test_diff_against(self):
self.poll.places.add(self.place)
add_record, create_record = self.poll.history.all()
Expand Down

0 comments on commit 0d9da42

Please sign in to comment.