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

Change "object_id" to be a "CharField" #138

Merged
merged 8 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions easyaudit/migrations/0014_auto_20200513_0008.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.0.6 on 2020-05-13 00:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('easyaudit', '0013_auto_20190723_0126'),
]

operations = [
migrations.AlterField(
model_name='crudevent',
name='object_id',
field=models.CharField(max_length=255),
),
]
2 changes: 1 addition & 1 deletion easyaudit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CRUDEvent(models.Model):
)

event_type = models.SmallIntegerField(choices=TYPES)
object_id = models.IntegerField() # we should try to allow other ID types
object_id = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, db_constraint=False)
object_repr = models.TextField(null=True, blank=True)
object_json_repr = models.TextField(null=True, blank=True)
Expand Down
12 changes: 7 additions & 5 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
# We need a better way for this to work. ManyToMany will fail on pre_save on create
return None

if instance.pk is None:
jheld marked this conversation as resolved.
Show resolved Hide resolved
created = True
else:
# Query the object, see if it exists
try:
old_model = sender.objects.get(id=instance.pk)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please change this to pk=instance.pk

I have primary keys not named id so this throws an exception

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@steverecio updated

created = False
except (sender.DoesNotExist, ):
old_model = None
created = True

# created or updated?
if not created:
old_model = sender.objects.get(pk=instance.pk)
if old_model:
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
Binary file modified easyaudit/tests/db.sqlite3
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 3.0.6 on 2020-05-14 17:28

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
('test_app', '0002_auto_20180220_1533'),
]

operations = [
migrations.CreateModel(
name='TestBigIntModel',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('name', models.CharField(default='test data', max_length=50)),
],
),
migrations.CreateModel(
name='TestUUIDModel',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('name', models.CharField(default='test data', max_length=50)),
],
),
migrations.CreateModel(
name='TestUUIDM2M',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('name', models.CharField(max_length=50)),
('test_m2m', models.ManyToManyField(to='test_app.TestUUIDModel')),
],
),
migrations.CreateModel(
name='TestUUIDForeignKey',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True)),
('name', models.CharField(max_length=50)),
('test_fk', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.TestUUIDModel')),
],
),
migrations.CreateModel(
name='TestBigIntM2M',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=50)),
('test_m2m', models.ManyToManyField(to='test_app.TestBigIntModel')),
],
),
migrations.CreateModel(
name='TestBigIntForeignKey',
fields=[
('id', models.BigAutoField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=50)),
('test_fk', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='test_app.TestBigIntModel')),
],
),
]
42 changes: 42 additions & 0 deletions easyaudit/tests/test_app/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import uuid

from django.db import models


Expand All @@ -13,3 +15,43 @@ class TestForeignKey(models.Model):
class TestM2M(models.Model):
name = models.CharField(max_length=50)
test_m2m = models.ManyToManyField(TestModel)


class TestUUIDModel(models.Model):
id = models.UUIDField(
primary_key=True, unique=True, editable=False, default=uuid.uuid4
)
name = models.CharField(max_length=50, default='test data')


class TestUUIDForeignKey(models.Model):
id = models.UUIDField(
primary_key=True, unique=True, editable=False, default=uuid.uuid4
)
name = models.CharField(max_length=50)
test_fk = models.ForeignKey(TestUUIDModel, on_delete=models.CASCADE)


class TestUUIDM2M(models.Model):
id = models.UUIDField(
primary_key=True, unique=True, editable=False, default=uuid.uuid4
)
name = models.CharField(max_length=50)
test_m2m = models.ManyToManyField(TestUUIDModel)


class TestBigIntModel(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50, default='test data')


class TestBigIntForeignKey(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50)
test_fk = models.ForeignKey(TestBigIntModel, on_delete=models.CASCADE)


class TestBigIntM2M(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=50)
test_m2m = models.ManyToManyField(TestBigIntModel)
44 changes: 31 additions & 13 deletions easyaudit/tests/test_app/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
import bs4
from test_app.models import TestModel, TestForeignKey, TestM2M
from test_app.models import (
TestModel, TestForeignKey, TestM2M,
TestBigIntModel, TestBigIntForeignKey, TestBigIntM2M,
TestUUIDModel, TestUUIDForeignKey, TestUUIDM2M
)
from easyaudit.models import CRUDEvent
from easyaudit.middleware.easyaudit import set_current_user, clear_request

Expand All @@ -23,36 +27,38 @@

@override_settings(TEST=True)
class TestAuditModels(TestCase):
Model = TestModel
FKModel = TestForeignKey
M2MModel = TestM2M

def test_create_model(self):
obj = TestModel.objects.create()
self.assertEqual(obj.id, 1)
obj = self.Model.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())
crud_event = crud_event_qs[0]
data = json.loads(crud_event.object_json_repr)[0]
self.assertEqual(data['fields']['name'], obj.name)

def test_fk_model(self):
obj = TestModel.objects.create()
obj_fk = TestForeignKey(name='test', test_fk=obj)
obj = self.Model.objects.create()
obj_fk = self.FKModel(name='test', test_fk=obj)
obj_fk.save()
crud_event = CRUDEvent.objects.filter(object_id=obj_fk.id, content_type=ContentType.objects.get_for_model(obj_fk))[0]
data = json.loads(crud_event.object_json_repr)[0]
self.assertEqual(data['fields']['test_fk'], obj.id)
self.assertEqual(str(data['fields']['test_fk']), str(obj.id))

def test_m2m_model(self):
obj = TestModel.objects.create()
obj_m2m = TestM2M(name='test')
obj = self.Model.objects.create()
obj_m2m = self.M2MModel(name='test')
obj_m2m.save()
obj_m2m.test_m2m.add(obj)
crud_event = CRUDEvent.objects.filter(object_id=obj_m2m.id, content_type=ContentType.objects.get_for_model(obj_m2m))[0]
data = json.loads(crud_event.object_json_repr)[0]
self.assertEqual(data['fields']['test_m2m'], [obj.id])
self.assertEqual([str(d) for d in data['fields']['test_m2m']], [str(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()
obj = self.Model.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'
Expand All @@ -62,7 +68,7 @@ def test_update_skip_no_changed_fields(self):
self.assertIn('name', last_change.changed_fields)

def test_update(self):
obj = TestModel.objects.create()
obj = self.Model.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'
Expand All @@ -73,18 +79,30 @@ def test_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()
obj = self.Model.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()
obj = self.Model.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())


class TestAuditUUIDModels(TestAuditModels):
Model = TestUUIDModel
FKModel = TestUUIDForeignKey
M2MModel = TestUUIDM2M


class TestAuditBigIntModels(TestAuditModels):
Model = TestBigIntModel
FKModel = TestBigIntForeignKey
M2MModel = TestBigIntM2M


@override_settings(TEST=True)
class TestMiddleware(TestCase):
def _setup_user(self, email, password):
Expand Down
6 changes: 6 additions & 0 deletions easyaudit/tests/test_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
urlpatterns = [
url("create-obj", views.create_obj_view, name="create-obj"),
url("update-obj", views.update_obj_view, name="update-obj"),

url("create-uuid-obj", views.create_uuid_obj_view, name="create-uuid-obj"),
url("update-uuid-obj", views.update_uuid_obj_view, name="update-uuid-obj"),

url("create-big-obj", views.create_big_obj_view, name="create-big-obj"),
url("update-big-obj", views.update_big_obj_view, name="update-big-obj"),
]
47 changes: 41 additions & 6 deletions easyaudit/tests/test_app/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
from datetime import datetime

from django.http import HttpResponse
from test_app.models import TestModel
from test_app.models import TestModel, TestUUIDModel, TestBigIntModel


def create_obj(Model):
return Model.objects.create()


def update_obj(Model, pk, name):
tm = Model.objects.get(pk=pk)
tm.name = name
tm.save()
return tm


def create_obj_view(request):
return HttpResponse(TestModel.objects.create().id)
return HttpResponse(create_obj(TestModel).id)


def update_obj_view(request):
tm = TestModel.objects.get(pk=request.GET['id'])
tm.name = request.GET['id']
tm.save()
return HttpResponse(tm.id)
name = datetime.now().isoformat()
return HttpResponse(update_obj(
TestModel, request.GET['id'], name
).id)


def create_uuid_obj_view(request):
return HttpResponse(create_obj(TestUUIDModel).id)


def update_uuid_obj_view(request):
name = datetime.now().isoformat()
return HttpResponse(update_obj(
TestUUIDModel, request.GET['id'], name
).id)


def create_big_obj_view(request):
return HttpResponse(create_obj(TestBigIntModel).id)


def update_big_obj_view(request):
name = datetime.now().isoformat()
return HttpResponse(update_obj(
TestBigIntModel, request.GET['id'], name
).id)