From 8555332bdae8c7d2d4057f4f402ecc309f27a8ca Mon Sep 17 00:00:00 2001 From: Gustavo Reyes Date: Fri, 3 Dec 2021 11:25:47 +0100 Subject: [PATCH 1/7] Enable username logging --- src/django_audit_log/logger.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/django_audit_log/logger.py b/src/django_audit_log/logger.py index bd0601b..0d1112c 100644 --- a/src/django_audit_log/logger.py +++ b/src/django_audit_log/logger.py @@ -63,6 +63,7 @@ def set_user_from_request( email=getattr(user, 'email', '') if user else '', roles=roles, ip=get_client_ip(request), + username=request.user.username if user.is_authenticated else '', ) return self From e302e4b0f5b88d07876b6f53a443e797bdde2aca Mon Sep 17 00:00:00 2001 From: Gustavo Reyes Date: Fri, 3 Dec 2021 12:19:12 +0100 Subject: [PATCH 2/7] Add new condition to avoid errors --- src/django_audit_log/logger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/django_audit_log/logger.py b/src/django_audit_log/logger.py index 0d1112c..cf78584 100644 --- a/src/django_audit_log/logger.py +++ b/src/django_audit_log/logger.py @@ -63,7 +63,7 @@ def set_user_from_request( email=getattr(user, 'email', '') if user else '', roles=roles, ip=get_client_ip(request), - username=request.user.username if user.is_authenticated else '', + username=request.user.username if user and user.is_authenticated else '', ) return self From 67a461f2f6fda9d76bd93ebcb755bdc74242225f Mon Sep 17 00:00:00 2001 From: Gustavo Reyes Date: Fri, 3 Dec 2021 12:23:17 +0100 Subject: [PATCH 3/7] Update tests with username param --- tests/test_logger.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_logger.py b/tests/test_logger.py index d68c9ca..e38379e 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -77,6 +77,8 @@ def test_set_user_from_request(self): self.assertEqual(audit_log.user['email'], 'username@host.com') self.assertEqual(audit_log.user['roles'], ['testgroup']) self.assertEqual(audit_log.user['ip'], '127.0.0.1') + self.assertEqual(audit_log.user['username'], 'username') + def test_extras_user_from_request(self): user = User.objects.create_user(username='username', email='username@host.com') @@ -97,6 +99,7 @@ def test_extras_user_from_request(self): self.assertEqual(extras['user']['email'], 'username@host.com') self.assertEqual(extras['user']['roles'], ['testgroup']) self.assertEqual(extras['user']['ip'], '127.0.0.1') + self.assertEqual(extras['user']['username'], 'username') def test_get_headers_from_response(self): expected_headers = { From 4b3b01c3c97b41e4063433f350ba5fc08e306e13 Mon Sep 17 00:00:00 2001 From: Gustavo Reyes Date: Fri, 3 Dec 2021 12:59:59 +0100 Subject: [PATCH 4/7] Remove empty line --- tests/test_logger.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_logger.py b/tests/test_logger.py index e38379e..f3998df 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -79,7 +79,6 @@ def test_set_user_from_request(self): self.assertEqual(audit_log.user['ip'], '127.0.0.1') self.assertEqual(audit_log.user['username'], 'username') - def test_extras_user_from_request(self): user = User.objects.create_user(username='username', email='username@host.com') group, _ = Group.objects.get_or_create(name='testgroup') From f7545cab85f8881456b59f0225eb596fd07c967b Mon Sep 17 00:00:00 2001 From: Gustavo Reyes Date: Fri, 3 Dec 2021 13:02:37 +0100 Subject: [PATCH 5/7] Update changelog --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index 6788fe1..04b7f9d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,4 +1,6 @@ +## [unreleased - 03-01-2021] +- Allow `username` logging when user is authenticated ## 0.4.0 (29-01-2020) ### Note worthy changes From f32b16d5c8fb229b57d3b28847fee6e111c61a8c Mon Sep 17 00:00:00 2001 From: Gustavo Reyes Date: Fri, 3 Dec 2021 13:03:16 +0100 Subject: [PATCH 6/7] Add empty line --- Changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Changelog.md b/Changelog.md index 04b7f9d..315eaf5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ ## [unreleased - 03-01-2021] - Allow `username` logging when user is authenticated + ## 0.4.0 (29-01-2020) ### Note worthy changes From ca20ef90d5ba98e7063eeaf200cc69d0430d3ea6 Mon Sep 17 00:00:00 2001 From: Gustavo Reyes Date: Fri, 3 Dec 2021 13:09:51 +0100 Subject: [PATCH 7/7] Fix date --- Changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 315eaf5..cc4116c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,5 @@ -## [unreleased - 03-01-2021] +## [unreleased - 03-12-2021] - Allow `username` logging when user is authenticated ## 0.4.0 (29-01-2020)