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

Do not create CRUDEvent on update if none of the fields has changed. #106

Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -67,6 +67,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 @@ -47,6 +47,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())


class TestMiddleware(TestCase):
def _setup_user(self, email, password):
Expand Down