Skip to content

Commit

Permalink
(Bug/fix) support for DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_…
Browse files Browse the repository at this point in the history
…SKIP (#129)

also removes non-supported python version (django 2.0 does not support them); tightens up travis CI
  • Loading branch information
jheld authored Mar 12, 2020
1 parent ff389bb commit 3e541ef
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 37 deletions.
45 changes: 16 additions & 29 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ language: python
dist: xenial

python:
- '2.7'
- '3.4'
- '3.5'
- '3.6'
- '3.7'
- 'pypy2.7-6.0'
- 'pypy3.5-6.0'
- '3.8'
- 'pypy3'

env:
- DJANGO="django>=1.11,<2.0"
- DJANGO="django>=1.11.17,<2.0"
- DJANGO="django>=2.0,<2.1"
- DJANGO="django>=2.1,<2.2"
- DJANGO="django>=2.2,<3.0"
- DJANGO="django>=2.2,<2.2.8"
- DJANGO="django>=2.2.8,<3.0"
- DJANGO="django>=3.0,<3.1"

install:
- pip install ${DJANGO}
Expand All @@ -30,31 +29,19 @@ script:

matrix:
exclude:
- python: "2.7"
env: DJANGO="django>=2.0,<2.1"
- python: "2.7"
env: DJANGO="django>=2.1,<2.2"
- python: "2.7"
env: DJANGO="django>=2.2,<3.0"
- python: "pypy2.7-6.0"
env: DJANGO="django>=1.11.17,<2.0"
- python: "pypy2.7-6.0"
env: DJANGO="django>=2.0,<2.1"
- python: "pypy2.7-6.0"
- python: "3.4"
env: DJANGO="django>=2.1,<2.2"
- python: "pypy2.7-6.0"
env: DJANGO="django>=2.2,<3.0"
- python: "3.4"
env: DJANGO="django>=1.11.17,<2.0"
env: DJANGO="django>=2.2,<2.2.8"
- python: "3.4"
env: DJANGO="django>=2.1,<2.2"
env: DJANGO="django>=2.2.8,<3.0"
- python: "3.4"
env: DJANGO="django>=2.2,<3.0"
env: DJANGO="django>=3.0,<3.1"
- python: "3.5"
env: DJANGO="django>=1.11.17,<2.0"
- python: "3.6"
env: DJANGO="django>=1.11.17,<2.0"
- python: "pypy3.5-6.0"
env: DJANGO="django>=1.11.17,<2.0"
- python: "3.7"
env: DJANGO="django>=1.11,<2.0"
env: DJANGO="django>=3.0,<3.1"
- python: "3.8"
env: DJANGO="django>=2.0,<2.1"
- python: "3.8"
env: DJANGO="django>=2.1,<2.2"
- python: "3.8"
env: DJANGO="django>=2.2,<2.2.8"
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ Below are some of the settings you may want to use. These should be defined in y
this is necessary in order to keep database atomicity concerns in check during signal handlers.

To clarify, this is only _truly_ necessary for the model signals.

* `DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP`

By default this is `False`, but this allows the calling project not to save `CRUDEvent` if the changed fields as
determined by the `pre_save` handler sees that there are no changed fields. We are keeping it off by default so that
projects that wish to use this (potentially less `CRUDEvent`) can choose to turn it on! And those that do not want it (yet or ever),
or those that do not closely follow the release notes of this project will have one less worry when upgrading.


* `DJANGO_EASY_AUDIT_LOGGING_BACKEND`

Expand Down
6 changes: 6 additions & 0 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def get_model_list(class_list):
CRUD_DIFFERENCE_CALLBACKS[idx] = getattr(import_module('.'.join(callback.split('.')[:-1])),
callback.split('.')[-1], None)

# although this setting "exists" here we do not intend to use it anywhere due to test run issues
# maybe we can properly solve this at a latter time. instead, anything inside of this library
# should do the same getattr check here, bsaed on normal `settings` from `django.conf`.
CRUD_EVENT_NO_CHANGED_FIELDS_SKIP = getattr(settings, "DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP", False)


# Purge table optimization:
# If TRUNCATE_TABLE_SQL_STATEMENT is not empty, we use it as custom sql statement
# to speed up table truncation bypassing ORM, i.e.:
Expand Down
2 changes: 1 addition & 1 deletion easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ 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:
if not delta and getattr(settings, "DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP", False):
return False
changed_fields = json.dumps(delta)
event_type = CRUDEvent.UPDATE
Expand Down
27 changes: 22 additions & 5 deletions easyaudit/tests/test_app/tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
import json
import re

from django.test import TestCase, override_settings

try: # Django 2.0
from django.urls import reverse
except: # Django < 2.0
from django.core.urlresolvers import reverse
from django.urls import reverse

from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
import bs4
Expand Down Expand Up @@ -50,6 +49,17 @@ def test_m2m_model(self):
data = json.loads(crud_event.object_json_repr)[0]
self.assertEqual(data['fields']['test_m2m'], [obj.id])

@override_settings(DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP=True)
def test_update_skip_no_changed_fields(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_update(self):
obj = TestModel.objects.create()
crud_event_qs = CRUDEvent.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj))
Expand All @@ -60,12 +70,19 @@ def test_update(self):
last_change = crud_event_qs.first()
self.assertIn('name', last_change.changed_fields)

def test_fake_update(self):
@override_settings(DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP=True)
def test_fake_update_skip_no_changed_fields(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())

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(2, crud_event_qs.count())


@override_settings(TEST=True)
class TestMiddleware(TestCase):
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Software Development :: Libraries :: Python Modules',
],
)

0 comments on commit 3e541ef

Please sign in to comment.