Disable logging remote IP address

This commit is contained in:
Nathan Bureau 2024-03-21 10:40:54 +01:00
parent b2aff7e313
commit 693a5092be
5 changed files with 35 additions and 1 deletions

View file

@ -5,6 +5,7 @@
## 3.0.0-beta.4 (2024-01-02)
#### Improvements
- feat: Excluding ip address when AUDITLOG_DISABLE_REMOTE_ADDR is set to True ([#620](https://github.com/jazzband/django-auditlog/pull/620))
- feat: If any receiver returns False, no logging will be made. This can be useful if logging should be conditionally enabled / disabled ([#590](https://github.com/jazzband/django-auditlog/pull/590))
- Django: Confirm Django 5.0 support ([#598](https://github.com/jazzband/django-auditlog/pull/598))
- Django: Drop Django 4.1 support ([#598](https://github.com/jazzband/django-auditlog/pull/598))

View file

@ -40,3 +40,8 @@ settings.AUDITLOG_TWO_STEP_MIGRATION = getattr(
settings.AUDITLOG_USE_TEXT_CHANGES_IF_JSON_IS_NOT_PRESENT = getattr(
settings, "AUDITLOG_USE_TEXT_CHANGES_IF_JSON_IS_NOT_PRESENT", False
)
# Disable remote_addr field in database
settings.AUDITLOG_DISABLE_REMOTE_ADDR = getattr(
settings, "AUDITLOG_DISABLE_REMOTE_ADDR", False
)

View file

@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from auditlog.cid import set_cid
@ -12,6 +13,11 @@ class AuditlogMiddleware:
def __init__(self, get_response=None):
self.get_response = get_response
self.disable_remote_addr = getattr(
settings, "AUDITLOG_DISABLE_REMOTE_ADDR", False
)
if not isinstance(self.disable_remote_addr, bool):
raise ValueError("Setting 'AUDITLOG_DISABLE_REMOTE_ADDR' must be a boolean")
@staticmethod
def _get_remote_addr(request):
@ -38,7 +44,10 @@ class AuditlogMiddleware:
return None
def __call__(self, request):
remote_addr = self._get_remote_addr(request)
if self.disable_remote_addr:
remote_addr = None
else:
remote_addr = self._get_remote_addr(request)
user = self._get_actor(request)
set_cid(request)

View file

@ -1273,6 +1273,12 @@ class RegisterModelSettingsTest(TestCase):
):
self.test_auditlog.register_from_settings()
with override_settings(AUDITLOG_DISABLE_REMOTE_ADDR="str"):
with self.assertRaisesMessage(
TypeError, "Setting 'AUDITLOG_DISABLE_REMOTE_ADDR' must be a boolean"
):
self.test_auditlog.register_from_settings()
@override_settings(
AUDITLOG_INCLUDE_ALL_MODELS=True,
AUDITLOG_EXCLUDE_TRACKING_MODELS=("auditlog_tests.SimpleExcludeModel",),

View file

@ -206,6 +206,19 @@ It will be considered when ``AUDITLOG_INCLUDE_ALL_MODELS`` is `True`.
.. versionadded:: 3.0.0
**AUDITLOG_EXCLUDE_TRACKING_FIELDS**
When using "AuditlogMiddleware",
the IP address is logged by default, you can use this setting
to exclude the IP address from logging.
It will be considered when ``AUDITLOG_DISABLE_REMOTE_ADDR`` is `True`.
.. code-block:: python
AUDITLOG_DISABLE_REMOTE_ADDR = True
.. versionadded:: 3.0.0
**AUDITLOG_EXCLUDE_TRACKING_MODELS**
You can use this setting to exclude models in registration process.