Add AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH settings

to keep or truncate strings of `changes_display_dict` property at a variable length
This commit is contained in:
hoangquochung1110 2024-11-01 22:03:48 +07:00
parent 4541be6f21
commit 19000db3e7
6 changed files with 31 additions and 40 deletions

View file

@ -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))

View file

@ -46,10 +46,7 @@ settings.AUDITLOG_DISABLE_REMOTE_ADDR = getattr(
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
)

View file

@ -24,7 +24,6 @@ from django.utils.encoding import smart_str
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>"
@ -507,11 +506,9 @@ class LogEntry(models.Model):
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)

View file

@ -1,11 +0,0 @@
class Truncator:
def __init__(self, text) -> None:
self.text = text
def chars(self, length: int) -> str:
return f"{self.text[:length]}..."
def truncatechars(text, length):
return Truncator(text).chars(length)

View file

@ -1561,29 +1561,34 @@ class CharFieldTextFieldModelTest(TestCase):
msg="The field should display the entire string because it is less than 140 characters",
)
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
def test_changes_display_dict_longtextfield_to_be_truncated_at_custom_length(self):
with override_settings(AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH=10):
length = 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.",
f"{self.PLACEHOLDER_LONGCHAR[:length]}...",
msg=f"The string should be truncated at {length} 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):
length = 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 {length}.",
)
def test_changes_display_dict_longtextfield_with_truncation_disabled(self):
with override_settings(AUDITLOG_TRUNCATE_CHANGES_DISPLAY=False):
limit = settings.AUDITLOG_TRUNCATE_LIMIT
self.assertTrue(len(self.PLACEHOLDER_LONGTEXTFIELD) > limit)
with override_settings(AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH=-1):
length = settings.AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH
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"
f"even though it is longer than {length} characters"
"as AUDITLOG_TRUNCATE_CHANGES_DISPLAY is set to a negative number"
),
)

View file

@ -305,13 +305,15 @@ 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
This configuration variable defines the truncation behavior for strings in `changes_display_dict`, with a default value of `140` characters.
**AUDITLOG_TRUNCATE_LIMIT**
0: The entire string is truncated, resulting in an empty output.
Positive values (e.g., 5): Truncates the string, keeping only the specified number of characters followed by an ellipsis (...) after the limit.
Negative values: No truncation occurs, and the full string is displayed.
Number of characters at which `changes_display_dict` property should be truncated
.. versionadded:: 3.1.0
Actors
------