mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
Add remote address field to LogEntry and autopopulate it when possible
This commit is contained in:
parent
f630ef7da1
commit
93e468ab03
2 changed files with 28 additions and 0 deletions
19
src/auditlog/migrations/0003_logentry_remote_addr.py
Normal file
19
src/auditlog/migrations/0003_logentry_remote_addr.py
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('auditlog', '0002_auto_support_long_primary_keys'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='logentry',
|
||||
name='remote_addr',
|
||||
field=models.GenericIPAddressField(null=True, verbose_name='remote address', blank=True),
|
||||
),
|
||||
]
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import json
|
||||
import threading
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes import generic
|
||||
|
|
@ -119,6 +120,7 @@ class LogEntry(models.Model):
|
|||
action = models.PositiveSmallIntegerField(choices=Action.choices, verbose_name=_("action"))
|
||||
changes = models.TextField(blank=True, verbose_name=_("change message"))
|
||||
actor = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL, related_name='+', verbose_name=_("actor"))
|
||||
remote_addr = models.GenericIPAddressField(blank=True, null=True, verbose_name=_("remote address"))
|
||||
timestamp = models.DateTimeField(auto_now_add=True, verbose_name=_("timestamp"))
|
||||
|
||||
objects = LogEntryManager()
|
||||
|
|
@ -141,6 +143,13 @@ class LogEntry(models.Model):
|
|||
|
||||
return fstring.format(repr=self.object_repr)
|
||||
|
||||
def clean(self):
|
||||
threadlocal = threading.local()
|
||||
|
||||
# Set remote_addr on creation if empty and available in thread
|
||||
if not self.pk and self.remote_addr is None and hasattr(threadlocal, 'auditlog'):
|
||||
self.remote_addr = threading.local().auditlog.get('remote_addr', None)
|
||||
|
||||
@property
|
||||
def changes_dict(self):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Reference in a new issue