Skip to content

Commit

Permalink
Add AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH settings
Browse files Browse the repository at this point in the history
to keep or truncate strings of `changes_display_dict` property at a variable length
  • Loading branch information
hoangquochung1110 committed Nov 1, 2024
1 parent 4541be6 commit 7343dfe
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- 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))
- feat: Added `AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH` settings to keep or truncate strings of `changes_display_dict` property at variable length. ([#684](https://github.com/jazzband/django-auditlog/pull/684))
- Drop Python 3.8 support. ([#678](https://github.com/jazzband/django-auditlog/pull/678))
- Confirm Django 5.1 support and drop Django 3.2 support. ([#677](https://github.com/jazzband/django-auditlog/pull/677))

Expand Down
9 changes: 3 additions & 6 deletions auditlog/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@
settings, "AUDITLOG_DISABLE_REMOTE_ADDR", False
)

# Enable changes_display_dict truncator
settings.AUDITLOG_TRUNCATE_CHANGES_DISPLAY = getattr(
settings, "AUDITLOG_TRUNCATE_CHANGES_DISPLAY", True
)

# Number of characters at which changes_display_dict property should be shown
settings.AUDITLOG_TRUNCATE_LIMIT = getattr(settings, "AUDITLOG_TRUNCATE_LIMIT", 140)
settings.AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH = getattr(
settings, "AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH", 140
)
9 changes: 3 additions & 6 deletions auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from django.utils.translation import gettext_lazy as _

from auditlog.diff import mask_str
from auditlog.text import truncatechars

DEFAULT_OBJECT_REPR = "<error forming object repr>"

Expand Down Expand Up @@ -507,11 +506,9 @@ def changes_display_dict(self):
elif field_type in ["ForeignKey", "OneToOneField"]:
value = self._get_changes_display_for_fk_field(field, value)

if (
settings.AUDITLOG_TRUNCATE_CHANGES_DISPLAY
and len(value) > settings.AUDITLOG_TRUNCATE_LIMIT
):
value = truncatechars(value, settings.AUDITLOG_TRUNCATE_LIMIT)
truncate_at = settings.AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH
if 0 <= truncate_at < len(value):
value = value[:truncate_at] + ("..." if truncate_at > 0 else "")

values_display.append(value)

Expand Down
11 changes: 0 additions & 11 deletions auditlog/text.py

This file was deleted.

22 changes: 14 additions & 8 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1562,28 +1562,34 @@ def test_changes_display_dict_longtextfield(self):
)

def test_changes_display_dict_longtextfield_to_be_truncated_with_custom_limit(self):
with override_settings(
AUDITLOG_TRUNCATE_CHANGES_DISPLAY=True,
AUDITLOG_TRUNCATE_LIMIT=10,
):
limit = settings.AUDITLOG_TRUNCATE_LIMIT
with override_settings(AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH=10):
limit = settings.AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH
self.assertEqual(
self.obj.history.latest().changes_display_dict["longtextfield"][1],
f"{self.PLACEHOLDER_LONGCHAR[:limit]}...",
msg=f"The string should be truncated at {limit} characters with an ellipsis at the end.",
)

def test_changes_display_dict_longtextfield_to_be_truncated_to_empty_string(self):
with override_settings(AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH=0):
limit = settings.AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH
self.assertEqual(
self.obj.history.latest().changes_display_dict["longtextfield"][1],
"",
msg=f"The string should be empty as AUDITLOG_TRUNCATE_CHANGES_DISPLAY is set to {limit}.",
)

def test_changes_display_dict_longtextfield_with_truncation_disabled(self):
with override_settings(AUDITLOG_TRUNCATE_CHANGES_DISPLAY=False):
limit = settings.AUDITLOG_TRUNCATE_LIMIT
with override_settings(AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH=-1):
limit = settings.AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH
self.assertTrue(len(self.PLACEHOLDER_LONGTEXTFIELD) > limit)
self.assertEqual(
self.obj.history.latest().changes_display_dict["longtextfield"][1],
self.PLACEHOLDER_LONGTEXTFIELD,
msg=(
"The field should display the entire string "
f"even though it is longer than {limit} characters"
"as AUDITLOG_TRUNCATE_CHANGES_DISPLAY is set to False"
"as AUDITLOG_TRUNCATE_CHANGES_DISPLAY is set to a negative number"
),
)

Expand Down
8 changes: 4 additions & 4 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ If the value is `None`, the default getter will be used.

.. versionadded:: 3.0.0

**AUDITLOG_TRUNCATE_CHANGES_DISPLAY**
**AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH**

You can use this settings to truncate characters in `changes_display_dict` property, True by default
Number of characters at which strings in `changes_display_dict` property should be truncated if length exceeded

**AUDITLOG_TRUNCATE_LIMIT**
Set to 140 by default.

Number of characters at which `changes_display_dict` property should be truncated
You can use this settings to preserve the strings by set it to a negative number.

Actors
------
Expand Down

0 comments on commit 7343dfe

Please sign in to comment.