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

Fixes issue #175 (possible exception with custom managers hiding som… #176

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):

# created or updated?
if not created:
old_model = sender.objects.get(pk=instance.pk)
old_model = sender._default_manager.get(pk=instance.pk)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_base_manager i think

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be _base_manager which will always return all records, the _default_manager may be filtering out records and can still raise an DoesNotExist exception.

delta = model_delta(old_model, instance)
if not delta and getattr(settings, "DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIP", False):
return False
Expand Down
25 changes: 25 additions & 0 deletions easyaudit/tests/test_app/migrations/0004_testsoftdeletemodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 2.2 on 2021-02-06 20:15

from django.db import migrations, models
import django.db.models.manager


class Migration(migrations.Migration):

dependencies = [
('test_app', '0003_testbigintforeignkey_testbigintm2m_testbigintmodel_testuuidforeignkey_testuuidm2m_testuuidmodel'),
]

operations = [
migrations.CreateModel(
name='TestSoftDeleteModel',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('deleted', models.BooleanField(default=False)),
('some_other_field', models.CharField(default='', max_length=50)),
],
managers=[
('all_objects', django.db.models.manager.Manager()),
],
),
]
12 changes: 12 additions & 0 deletions easyaudit/tests/test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ class TestBigIntM2M(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50)
test_m2m = models.ManyToManyField(TestBigIntModel)

class SoftDeleteManager(models.Manager):
def get_queryset(self):
return super().get_queryset().exclude(deleted=True)

class TestSoftDeleteModel(models.Model):
id = models.BigAutoField(primary_key=True)
deleted = models.BooleanField(default=False)
some_other_field = models.CharField(max_length=50, default='')

all_objects = models.Manager()
objects = SoftDeleteManager()
17 changes: 16 additions & 1 deletion easyaudit/tests/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from test_app.models import (
TestModel, TestForeignKey, TestM2M,
TestBigIntModel, TestBigIntForeignKey, TestBigIntM2M,
TestUUIDModel, TestUUIDForeignKey, TestUUIDM2M
TestUUIDModel, TestUUIDForeignKey, TestUUIDM2M,
TestSoftDeleteModel
)
from easyaudit.models import CRUDEvent, RequestEvent
from easyaudit.middleware.easyaudit import set_current_user, clear_request
Expand Down Expand Up @@ -278,3 +279,17 @@ def test_request_event_admin_no_users(self):
response = self.client.get(reverse('admin:easyaudit_requestevent_changelist'))
self.assertEqual(200, response.status_code)
filters = self._list_filters(response.content)

@override_settings(TEST=True)
class TestCustomManager(TestCase):
Model = TestSoftDeleteModel

def test_soft_delete_model(self):
obj = self.Model.objects.create(deleted=True)
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.some_other_field = 'this should not fail'
obj.save()
crud_event_qs = CRUDEvent.objects.filter(object_id=obj.id, content_type=ContentType.objects.get_for_model(obj))
self.assertEqual(2, crud_event_qs.count())