Skip to content

Commit

Permalink
Feature: user & content_type foreign key drop db_constraints. Emulate…
Browse files Browse the repository at this point in the history
… via settings.
  • Loading branch information
jheld committed Feb 21, 2019
1 parent b0f95f1 commit 6b9ad45
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 17 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ Below are some of the settings you may want to use. These should be defined in y
will be matched against the URL path.
[Check our wiki](https://github.com/soynatan/django-easy-audit/wiki/Settings#request-auditing)
for more details on how to use it.

* `DJANGO_EASY_AUDIT_CRUD_DIFFERENCE_CALLBACKS`

May point to a list of callables/string-paths-to-functions-classes in which the application code can determine
on a per CRUDEvent whether or not the application chooses to create the CRUDEvent or not. This is different
from the registered/unregistered settings (e.g. `DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRA`).
This is meant to be for dynamic configurations where the application
may inspect the current save/create/delete and choose whether or not to save that into the database or ignore it.

* `DJANGO_EASY_AUDIT_USER_DB_CONSTRAINT`

Default is `True`. This is reserved for future use (does not do anything yet). The functionality provided by the
setting (whether enabled or disabled) could be handled more explicitly in certain
code paths (or even internally as custom model managers). For projects that separate the easyaudit database, such
that the tables are not on the same database as the user table, this could help with making certain queries easier.
Again, this doesn't do anything yet, and if it ever does, the version will be increased and the README will be
updated accordingly. If you keep your database together (the standard usage), you have nothing to worry about.

## What does it do

Expand Down
42 changes: 42 additions & 0 deletions easyaudit/migrations/0012_auto_20181018_0012.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.12 on 2018-10-18 00:12
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('easyaudit', '0011_auto_20181101_1339'),
]

operations = [
migrations.AlterField(
model_name='crudevent',
name='user',
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='crudevent',
name='content_type',
field=models.ForeignKey(to='contenttypes.ContentType', db_constraint=False, on_delete=models.CASCADE),
),
migrations.AlterField(
model_name='loginevent',
name='user',
field=models.ForeignKey(blank=True, db_constraint=False, null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='requestevent',
name='user',
field=models.ForeignKey(blank=True, db_constraint=False, null=True,
on_delete=django.db.models.deletion.SET_NULL,
to=settings.AUTH_USER_MODEL),
),

]
11 changes: 6 additions & 5 deletions easyaudit/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ class CRUDEvent(models.Model):

event_type = models.SmallIntegerField(choices=TYPES)
object_id = models.IntegerField() # we should try to allow other ID types
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
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)
changed_fields = models.TextField(null=True, blank=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
blank=True, on_delete=models.SET_NULL)
blank=True, on_delete=models.SET_NULL,
db_constraint=False)
user_pk_as_string = models.CharField(max_length=255, null=True, blank=True,
help_text='String version of the user pk')
help_text='String version of the user pk')
datetime = models.DateTimeField(auto_now_add=True)

def is_create(self):
Expand Down Expand Up @@ -59,7 +60,7 @@ class LoginEvent(models.Model):
login_type = models.SmallIntegerField(choices=TYPES)
username = models.CharField(max_length=255, null=True, blank=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
on_delete=models.SET_NULL)
on_delete=models.SET_NULL, db_constraint=False)
remote_ip = models.CharField(max_length=50, null=True, db_index=True)
datetime = models.DateTimeField(auto_now_add=True)

Expand All @@ -74,7 +75,7 @@ class RequestEvent(models.Model):
method = models.CharField(max_length=20, null=False, db_index=True)
query_string = models.TextField(null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
on_delete=models.SET_NULL)
on_delete=models.SET_NULL, db_constraint=False)
remote_ip = models.CharField(max_length=50, null=True, db_index=True)
datetime = models.DateTimeField(auto_now_add=True)

Expand Down
2 changes: 2 additions & 0 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def get_model_list(class_list):
WATCH_REQUEST_EVENTS = getattr(settings, 'DJANGO_EASY_AUDIT_WATCH_REQUEST_EVENTS', True)
REMOTE_ADDR_HEADER = getattr(settings, 'DJANGO_EASY_AUDIT_REMOTE_ADDR_HEADER', 'REMOTE_ADDR')

USER_DB_CONSTRAINT = bool(getattr(settings, 'DJANGO_EASY_AUDIT_USER_DB_CONSTRAINT', True))


# Models which Django Easy Audit will not log.
# By default, all but some models will be audited.
Expand Down
6 changes: 3 additions & 3 deletions easyaudit/signals/auth_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def user_logged_in(sender, request, user, **kwargs):
with transaction.atomic():
login_event = LoginEvent.objects.create(login_type=LoginEvent.LOGIN,
username=getattr(user, user.USERNAME_FIELD),
user=user,
user_id=getattr(user, 'id', None),
remote_ip=request.META[REMOTE_ADDR_HEADER])
except:
pass
Expand All @@ -20,8 +20,8 @@ def user_logged_out(sender, request, user, **kwargs):
try:
with transaction.atomic():
login_event = LoginEvent.objects.create(login_type=LoginEvent.LOGOUT,
username=getattr(user, user.USERNAME_FIELD),
user=user,
username=getattr(user, user.USERNAME_FIELD, None),
user_id=getattr(user, 'id', None),
remote_ip=request.META[REMOTE_ADDR_HEADER])
except:
pass
Expand Down
16 changes: 8 additions & 8 deletions easyaudit/signals/model_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def pre_save(sender, instance, raw, using, update_fields, **kwargs):
object_repr=str(instance),
object_json_repr=object_json_repr,
changed_fields=changed_fields,
content_type=c_t,
content_type_id=c_t.id,
object_id=instance.pk,
user=user,
user_id=getattr(user, 'id', None),
datetime=timezone.now(),
user_pk_as_string=str(user.pk) if user else user
)
Expand Down Expand Up @@ -152,9 +152,9 @@ def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
event_type=event_type,
object_repr=str(instance),
object_json_repr=object_json_repr,
content_type=c_t,
content_type_id=c_t.id,
object_id=instance.pk,
user=user,
user_id=getattr(user, 'id', None),
datetime=timezone.now(),
user_pk_as_string=str(user.pk) if user else user
)
Expand Down Expand Up @@ -232,9 +232,9 @@ def m2m_changed(sender, instance, action, reverse, model, pk_set, using, **kwarg
event_type=event_type,
object_repr=str(instance),
object_json_repr=object_json_repr,
content_type=c_t,
content_type_id=c_t.id,
object_id=instance.pk,
user=user,
user_id=getattr(user, 'id', None),
datetime=timezone.now(),
user_pk_as_string=str(user.pk) if user else user
)
Expand Down Expand Up @@ -275,9 +275,9 @@ def post_delete(sender, instance, using, **kwargs):
event_type=CRUDEvent.DELETE,
object_repr=str(instance),
object_json_repr=object_json_repr,
content_type=c_t,
content_type_id=c_t.id,
object_id=instance.pk,
user=user,
user_id=getattr(user, 'id', None),
datetime=timezone.now(),
user_pk_as_string=str(user.pk) if user else user
)
Expand Down
2 changes: 1 addition & 1 deletion easyaudit/signals/request_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def request_started_handler(sender, environ, **kwargs):
url=environ['PATH_INFO'],
method=environ['REQUEST_METHOD'],
query_string=environ['QUERY_STRING'],
user=user,
user_id=getattr(user, 'id', None),
remote_ip=environ[REMOTE_ADDR_HEADER],
datetime=timezone.now()
)
Expand Down

0 comments on commit 6b9ad45

Please sign in to comment.