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

Add generic FK and path info to logentry #239

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions auditlog/admin.py
Original file line number Diff line number Diff line change
@@ -8,9 +8,9 @@ class LogEntryAdmin(admin.ModelAdmin, LogEntryAdminMixin):
list_display = ['created', 'resource_url', 'action', 'msg_short', 'user_url']
search_fields = ['timestamp', 'object_repr', 'changes', 'actor__first_name', 'actor__last_name']
list_filter = ['action', ResourceTypeFilter]
readonly_fields = ['created', 'resource_url', 'action', 'user_url', 'msg']
readonly_fields = ['created', 'resource_url', 'action', 'user_url', 'msg', 'path_info']
fieldsets = [
(None, {'fields': ['created', 'user_url', 'resource_url']}),
(None, {'fields': ['created', 'user_url', 'resource_url', 'path_info']}),
('Changes', {'fields': ['action', 'msg']}),
]

2 changes: 2 additions & 0 deletions auditlog/middleware.py
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ def process_request(self, request):
threadlocal.auditlog = {
'signal_duid': (self.__class__, time.time()),
'remote_addr': request.META.get('REMOTE_ADDR'),
'path_info': request.META.get('PATH_INFO', None)
}

# In case of proxy, set 'original' address
@@ -74,3 +75,4 @@ def set_actor(user, sender, instance, signal_duid, **kwargs):
instance.actor = user

instance.remote_addr = threadlocal.auditlog['remote_addr']
instance.path_info = threadlocal.auditlog['path_info']
19 changes: 19 additions & 0 deletions auditlog/migrations/0008_auto_20190906_0840.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.1.9 on 2019-09-06 06:40

import django.contrib.postgres.fields.jsonb
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('auditlog', '0007_object_pk_type'),
]

operations = [
migrations.AlterField(
model_name='logentry',
name='additional_data',
field=django.contrib.postgres.fields.jsonb.JSONField(blank=True, null=True, verbose_name='additional data'),
),
]
18 changes: 18 additions & 0 deletions auditlog/migrations/0009_logentry_path_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1.9 on 2020-03-05 17:15

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('auditlog', '0008_auto_20190906_0840'),
]

operations = [
migrations.AddField(
model_name='logentry',
name='path_info',
field=models.TextField(blank=True, null=True, verbose_name='path info'),
),
]
9 changes: 7 additions & 2 deletions auditlog/mixins.py
Original file line number Diff line number Diff line change
@@ -14,8 +14,13 @@
class LogEntryAdminMixin(object):

def created(self, obj):
return obj.timestamp.strftime('%Y-%m-%d %H:%M:%S')

viewname = 'admin:auditlog_logentry_change'
created_time = obj.timestamp.strftime('%Y-%m-%d %H:%M:%S')
try:
link = urlresolvers.reverse(viewname, args=[obj.id])
except NoReverseMatch:
return u'%s' % (obj.id)
return format_html(u'<a href="{}">{}</a>', link, created_time)
created.short_description = 'Created'

def user_url(self, obj):
6 changes: 4 additions & 2 deletions auditlog/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ast
import json

from django.contrib.contenttypes import fields
from dateutil import parser
from dateutil.tz import gettz
from django.conf import settings
@@ -12,7 +12,7 @@
from django.utils import formats, timezone
from django.utils.encoding import smart_str
from django.utils.translation import ugettext_lazy as _
from jsonfield.fields import JSONField
from django.contrib.postgres.fields import JSONField


class LogEntryManager(models.Manager):
@@ -168,6 +168,7 @@ class Action:
(DELETE, _("delete")),
)

content_object = fields.GenericForeignKey('content_type', 'object_id')
content_type = models.ForeignKey(to='contenttypes.ContentType', on_delete=models.CASCADE, related_name='+',
verbose_name=_("content type"))
object_pk = models.CharField(db_index=True, max_length=255, verbose_name=_("object pk"))
@@ -178,6 +179,7 @@ class Action:
actor = models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, blank=True, null=True,
related_name='+', verbose_name=_("actor"))
remote_addr = models.GenericIPAddressField(blank=True, null=True, verbose_name=_("remote address"))
path_info = models.TextField(_("path info"), null=True, blank=True)
timestamp = models.DateTimeField(auto_now_add=True, verbose_name=_("timestamp"))
additional_data = JSONField(blank=True, null=True, verbose_name=_("additional data"))