Skip to content

Commit

Permalink
Dont put auto_now and auto_now_add fields in the history
Browse files Browse the repository at this point in the history
  • Loading branch information
knokko committed Apr 4, 2024
1 parent dcbf985 commit ce6ef4d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
22 changes: 20 additions & 2 deletions binder/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.contrib.auth import get_user_model
from django.conf import settings
from django.dispatch import Signal
from django.core.exceptions import FieldDoesNotExist

from .json import jsondumps, JsonResponse

Expand Down Expand Up @@ -172,7 +173,18 @@ def _commit():
)
changeset.save()

auto_now_counter = 0

for (model, oid, field), (old, new, diff) in _Transaction.changes.items():

# Skip auto_now and auto_now_add fields because they are not interesting for the history
try:
field_properties = model._meta.get_field(field)
if field_properties.auto_now or field_properties.auto_now_add:
auto_now_counter += 1
except (FieldDoesNotExist, AttributeError):
pass

# New instances get None for all the before values
if old is NewInstanceField:
old = None
Expand All @@ -191,8 +203,14 @@ def _commit():

transaction_commit.send(sender=None, changeset=changeset)

# Save the changeset again, to update the date to be as close to DB transaction commit start as possible.
changeset.save()
if changeset.changes.count() > auto_now_counter:
# Save the changeset again, to update the date to be as close to DB transaction commit start as possible.
changeset.save()
else:
# Changesets without real changes are useless
changeset.changes.all().delete()
changeset.delete()

_Transaction.stop()


Expand Down
20 changes: 20 additions & 0 deletions tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,27 @@ def test_model_with_history_creates_changes_on_creation(self):
self.assertEqual(1, Change.objects.filter(changeset=cs, model='Animal', field='deleted', before='null', after='false').count())
self.assertEqual(1, Change.objects.filter(changeset=cs, model='Animal', field='birth_date', before='null', after='null').count())

def test_do_not_keep_auto_now_only_changesets(self):
tim = ContactPerson.objects.create(name="Tim")

self.assertEqual(0, Changeset.objects.count())
self.assertEqual(0, Change.objects.count())

self.assertEqual(self.client.patch(
'/contact_person/%d/' % tim.pk, data=json.dumps({'name': 'Tim'}), content_type='application/json'
).status_code, 200)

self.assertEqual(0, Change.objects.count())
self.assertEqual(0, Changeset.objects.count())

self.assertEqual(self.client.patch(
'/contact_person/%d/' % tim.pk, data=json.dumps({'name': 'knokko'}), content_type='application/json'
).status_code, 200)

self.assertEqual(2, Change.objects.count())
self.assertEqual(1, Changeset.objects.count())
self.assertEqual(1, Change.objects.filter(model='ContactPerson', after='"knokko"').count())
self.assertEqual(1, Change.objects.filter(model='ContactPerson', field='updated_at').count())

def test_model_with_history_creates_changes_on_update_but_only_for_changed_fields(self):
daffy = Animal(name='Daffy Duck')
Expand Down
3 changes: 3 additions & 0 deletions tests/testapp/models/contact_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class ContactPerson(BinderModel):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Binder:
history = True

def __str__(self):
return 'contact_person %d: %s' % (self.pk, self.name)

Expand Down

0 comments on commit ce6ef4d

Please sign in to comment.