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 actor email #641

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Improvements

- feat: Added 'LogEntry.actor_email` field. ([#641](https://github.com/jazzband/django-auditlog/pull/641))
- Add Python 3.13 support. ([#671](https://github.com/jazzband/django-auditlog/pull/671))
- feat: Added `LogEntry.remote_port` field. ([#671](https://github.com/jazzband/django-auditlog/pull/671))
- feat: Added `truncate` option to `auditlogflush` management command. ([#681](https://github.com/jazzband/django-auditlog/pull/681))
Expand Down
7 changes: 6 additions & 1 deletion auditlog/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def set_actor(actor, remote_addr=None, remote_port=None):
auditlog_value.set(context_data)

# Connect signal for automatic logging
set_actor = partial(_set_actor, user=actor, signal_duid=context_data["signal_duid"])
set_actor = partial(
_set_actor,
user=actor,
signal_duid=context_data["signal_duid"],
)
pre_save.connect(
set_actor,
sender=LogEntry,
Expand Down Expand Up @@ -62,6 +66,7 @@ def _set_actor(user, sender, instance, signal_duid, **kwargs):
and instance.actor is None
):
instance.actor = user
instance.actor_email = hasattr(user, "email") and user.email or None

instance.remote_addr = auditlog["remote_addr"]
instance.remote_port = auditlog["remote_port"]
Expand Down
20 changes: 20 additions & 0 deletions auditlog/migrations/0017_add_actor_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("auditlog", "0016_logentry_remote_port"),
]

operations = [
migrations.AddField(
model_name="logentry",
name="actor_email",
field=models.CharField(
null=True,
verbose_name="actor email",
blank=True,
max_length=254,
),
),
]
1 change: 1 addition & 0 deletions auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ class Action:
additional_data = models.JSONField(
blank=True, null=True, verbose_name=_("additional data")
)
actor_email = models.CharField(blank=True, null=True, max_length=254)

objects = LogEntryManager()

Expand Down
6 changes: 6 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,10 @@ def setUp(self):
super().setUp()

def tearDown(self):
user_email = self.user.email
self.user.delete()
auditlog_entries = LogEntry.objects.filter(actor_email=user_email).all()
self.assertIsNotNone(auditlog_entries, msg="All auditlog entries are deleted.")
super().tearDown()

def make_object(self):
Expand All @@ -272,6 +275,7 @@ def make_object(self):
def check_create_log_entry(self, obj, log_entry):
super().check_create_log_entry(obj, log_entry)
self.assertEqual(log_entry.actor, self.user)
self.assertEqual(log_entry.actor_email, self.user.email)

def update(self, obj):
with set_actor(self.user):
Expand All @@ -280,6 +284,7 @@ def update(self, obj):
def check_update_log_entry(self, obj, log_entry):
super().check_update_log_entry(obj, log_entry)
self.assertEqual(log_entry.actor, self.user)
self.assertEqual(log_entry.actor_email, self.user.email)

def delete(self, obj):
with set_actor(self.user):
Expand All @@ -288,6 +293,7 @@ def delete(self, obj):
def check_delete_log_entry(self, obj, log_entry):
super().check_delete_log_entry(obj, log_entry)
self.assertEqual(log_entry.actor, self.user)
self.assertEqual(log_entry.actor_email, self.user.email)


class AltPrimaryKeyModelBase(SimpleModelTest):
Expand Down