Skip to content

Commit 056afd0

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 91f055d + 5249423 commit 056afd0

File tree

10 files changed

+220
-77
lines changed

10 files changed

+220
-77
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,23 @@ Below are some of the settings you may want to use. These should be defined in y
5959
will be matched against the URL path.
6060
[Check our wiki](https://github.com/soynatan/django-easy-audit/wiki/Settings#request-auditing)
6161
for more details on how to use it.
62+
63+
* `DJANGO_EASY_AUDIT_CRUD_DIFFERENCE_CALLBACKS`
64+
65+
May point to a list of callables/string-paths-to-functions-classes in which the application code can determine
66+
on a per CRUDEvent whether or not the application chooses to create the CRUDEvent or not. This is different
67+
from the registered/unregistered settings (e.g. `DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRA`).
68+
This is meant to be for dynamic configurations where the application
69+
may inspect the current save/create/delete and choose whether or not to save that into the database or ignore it.
70+
71+
* `DJANGO_EASY_AUDIT_USER_DB_CONSTRAINT`
72+
73+
Default is `True`. This is reserved for future use (does not do anything yet). The functionality provided by the
74+
setting (whether enabled or disabled) could be handled more explicitly in certain
75+
code paths (or even internally as custom model managers). For projects that separate the easyaudit database, such
76+
that the tables are not on the same database as the user table, this could help with making certain queries easier.
77+
Again, this doesn't do anything yet, and if it ever does, the version will be increased and the README will be
78+
updated accordingly. If you keep your database together (the standard usage), you have nothing to worry about.
6279

6380
## What does it do
6481

easyaudit/migrations/0009_auto_20180314_2225.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ class Migration(migrations.Migration):
2020
migrations.AlterField(
2121
model_name='requestevent',
2222
name='url',
23-
field=models.TextField(db_index=True),
23+
field=models.TextField(db_index=False),
2424
),
2525
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11 on 2018-11-01 13:39
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('easyaudit', '0010_repr_text'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='requestevent',
17+
name='url',
18+
field=models.TextField(),
19+
),
20+
]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.12 on 2018-10-18 00:12
3+
from __future__ import unicode_literals
4+
5+
from django.conf import settings
6+
from django.db import migrations, models
7+
import django.db.models.deletion
8+
9+
10+
class Migration(migrations.Migration):
11+
12+
dependencies = [
13+
('easyaudit', '0011_auto_20181101_1339'),
14+
]
15+
16+
operations = [
17+
migrations.AlterField(
18+
model_name='crudevent',
19+
name='user',
20+
field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
21+
),
22+
migrations.AlterField(
23+
model_name='crudevent',
24+
name='content_type',
25+
field=models.ForeignKey(to='contenttypes.ContentType', db_constraint=False, on_delete=models.CASCADE),
26+
),
27+
migrations.AlterField(
28+
model_name='loginevent',
29+
name='user',
30+
field=models.ForeignKey(blank=True, db_constraint=False, null=True,
31+
on_delete=django.db.models.deletion.SET_NULL,
32+
to=settings.AUTH_USER_MODEL),
33+
),
34+
migrations.AlterField(
35+
model_name='requestevent',
36+
name='user',
37+
field=models.ForeignKey(blank=True, db_constraint=False, null=True,
38+
on_delete=django.db.models.deletion.SET_NULL,
39+
to=settings.AUTH_USER_MODEL),
40+
),
41+
42+
]

easyaudit/models.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ class CRUDEvent(models.Model):
2121

2222
event_type = models.SmallIntegerField(choices=TYPES)
2323
object_id = models.IntegerField() # we should try to allow other ID types
24-
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
24+
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, db_constraint=False)
2525
object_repr = models.TextField(null=True, blank=True)
2626
object_json_repr = models.TextField(null=True, blank=True)
2727
changed_fields = models.TextField(null=True, blank=True)
2828
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True,
29-
blank=True, on_delete=models.SET_NULL)
29+
blank=True, on_delete=models.SET_NULL,
30+
db_constraint=False)
3031
user_pk_as_string = models.CharField(max_length=255, null=True, blank=True,
31-
help_text='String version of the user pk')
32+
help_text='String version of the user pk')
3233
datetime = models.DateTimeField(auto_now_add=True)
3334

3435
def is_create(self):
@@ -59,7 +60,7 @@ class LoginEvent(models.Model):
5960
login_type = models.SmallIntegerField(choices=TYPES)
6061
username = models.CharField(max_length=255, null=True, blank=True)
6162
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
62-
on_delete=models.SET_NULL)
63+
on_delete=models.SET_NULL, db_constraint=False)
6364
remote_ip = models.CharField(max_length=50, null=True, db_index=True)
6465
datetime = models.DateTimeField(auto_now_add=True)
6566

@@ -70,11 +71,11 @@ class Meta:
7071

7172

7273
class RequestEvent(models.Model):
73-
url = models.TextField(null=False, db_index=True)
74+
url = models.TextField(null=False, db_index=False)
7475
method = models.CharField(max_length=20, null=False, db_index=True)
7576
query_string = models.TextField(null=True)
7677
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True,
77-
on_delete=models.SET_NULL)
78+
on_delete=models.SET_NULL, db_constraint=False)
7879
remote_ip = models.CharField(max_length=50, null=True, db_index=True)
7980
datetime = models.DateTimeField(auto_now_add=True)
8081

easyaudit/settings.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from importlib import import_module
22

3-
from django.apps.registry import apps
3+
from django.apps import apps
44
from django.conf import settings
5-
from django.contrib.admin.models import LogEntry
65
from django.contrib.auth.models import Permission
76
from django.contrib.contenttypes.models import ContentType
87
from django.contrib.sessions.models import Session
@@ -31,12 +30,20 @@ def get_model_list(class_list):
3130
WATCH_REQUEST_EVENTS = getattr(settings, 'DJANGO_EASY_AUDIT_WATCH_REQUEST_EVENTS', True)
3231
REMOTE_ADDR_HEADER = getattr(settings, 'DJANGO_EASY_AUDIT_REMOTE_ADDR_HEADER', 'REMOTE_ADDR')
3332

33+
USER_DB_CONSTRAINT = bool(getattr(settings, 'DJANGO_EASY_AUDIT_USER_DB_CONSTRAINT', True))
34+
3435

3536
# Models which Django Easy Audit will not log.
3637
# By default, all but some models will be audited.
3738
# The list of excluded models can be overwritten or extended
3839
# by defining the following settings in the project.
39-
UNREGISTERED_CLASSES = [CRUDEvent, LoginEvent, RequestEvent, Migration, LogEntry, Session, Permission, ContentType, MigrationRecorder.Migration]
40+
UNREGISTERED_CLASSES = [CRUDEvent, LoginEvent, RequestEvent, Migration, Session, Permission, ContentType, MigrationRecorder.Migration]
41+
42+
# Import and unregister LogEntry class only if Django Admin app is installed
43+
if apps.is_installed('django.contrib.admin'):
44+
from django.contrib.admin.models import LogEntry
45+
UNREGISTERED_CLASSES += [LogEntry]
46+
4047
UNREGISTERED_CLASSES = getattr(settings, 'DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_DEFAULT', UNREGISTERED_CLASSES)
4148
UNREGISTERED_CLASSES.extend(getattr(settings, 'DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRA', []))
4249
get_model_list(UNREGISTERED_CLASSES)
@@ -60,6 +67,13 @@ def get_model_list(class_list):
6067
UNREGISTERED_URLS.extend(getattr(settings, 'DJANGO_EASY_AUDIT_UNREGISTERED_URLS_EXTRA', []))
6168

6269

70+
# URLs which Django Easy Audit WILL log.
71+
# If the following setting is defined in the project,
72+
# only the listed URLs will be audited, and every other
73+
# URL will be excluded.
74+
REGISTERED_URLS = getattr(settings, 'DJANGO_EASY_AUDIT_REGISTERED_URLS', [])
75+
76+
6377
# By default all modules are listed in the admin.
6478
# This can be changed with the following settings.
6579
ADMIN_SHOW_MODEL_EVENTS = getattr(settings, 'DJANGO_EASY_AUDIT_ADMIN_SHOW_MODEL_EVENTS', True)

easyaudit/signals/auth_signals.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def user_logged_in(sender, request, user, **kwargs):
1010
with transaction.atomic():
1111
login_event = LoginEvent.objects.create(login_type=LoginEvent.LOGIN,
1212
username=getattr(user, user.USERNAME_FIELD),
13-
user=user,
13+
user_id=getattr(user, 'id', None),
1414
remote_ip=request.META[REMOTE_ADDR_HEADER])
1515
except:
1616
pass
@@ -20,8 +20,8 @@ def user_logged_out(sender, request, user, **kwargs):
2020
try:
2121
with transaction.atomic():
2222
login_event = LoginEvent.objects.create(login_type=LoginEvent.LOGOUT,
23-
username=getattr(user, user.USERNAME_FIELD),
24-
user=user,
23+
username=getattr(user, user.USERNAME_FIELD, None),
24+
user_id=getattr(user, 'id', None),
2525
remote_ip=request.META[REMOTE_ADDR_HEADER])
2626
except:
2727
pass

0 commit comments

Comments
 (0)