Skip to content

Commit

Permalink
Do not create CRUDEvent on update if none of the fields were updated. (
Browse files Browse the repository at this point in the history
  • Loading branch information
guy881 authored Feb 2, 2020
1 parent 806ae6b commit 0e720f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 0 additions & 1 deletion easyaudit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.db import models


# Create your models here.
class CRUDEvent(models.Model):
CREATE = 1
UPDATE = 2
Expand Down
2 changes: 2 additions & 0 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
if not created:
old_model = sender.objects.get(pk=instance.pk)
delta = model_delta(old_model, instance)
if not delta:
return False
changed_fields = json.dumps(delta)
event_type = CRUDEvent.UPDATE

Expand Down
16 changes: 16 additions & 0 deletions easyaudit/tests/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ def test_m2m_model(self):
data = json.loads(crud_event.object_json_repr)[0]
self.assertEqual(data['fields']['test_m2m'], [obj.id])

def test_update(self):
obj = TestModel.objects.create()
crud_event_qs = CRUDEvent.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj))
self.assertEqual(1, crud_event_qs.count())
obj.name = 'changed name'
obj.save()
self.assertEqual(2, crud_event_qs.count())
last_change = crud_event_qs.first()
self.assertIn('name', last_change.changed_fields)

def test_fake_update(self):
obj = TestModel.objects.create()
crud_event_qs = CRUDEvent.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj))
obj.save()
self.assertEqual(1, crud_event_qs.count())


@override_settings(TEST=True)
class TestMiddleware(TestCase):
Expand Down

0 comments on commit 0e720f3

Please sign in to comment.