From 7343dfe48e34f2825ef7050954624610e0b8dea6 Mon Sep 17 00:00:00 2001 From: hoangquochung1110 Date: Fri, 1 Nov 2024 22:03:48 +0700 Subject: [PATCH] Add AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH settings to keep or truncate strings of `changes_display_dict` property at a variable length --- CHANGELOG.md | 1 + auditlog/conf.py | 9 +++------ auditlog/models.py | 9 +++------ auditlog/text.py | 11 ----------- auditlog_tests/tests.py | 22 ++++++++++++++-------- docs/source/usage.rst | 8 ++++---- 6 files changed, 25 insertions(+), 35 deletions(-) delete mode 100644 auditlog/text.py diff --git a/CHANGELOG.md b/CHANGELOG.md index d2f3b430..ebd95d76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/auditlog/conf.py b/auditlog/conf.py index 7cc29f36..fe347ccd 100644 --- a/auditlog/conf.py +++ b/auditlog/conf.py @@ -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 +) diff --git a/auditlog/models.py b/auditlog/models.py index 31154470..49e7aba6 100644 --- a/auditlog/models.py +++ b/auditlog/models.py @@ -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 = "" @@ -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) diff --git a/auditlog/text.py b/auditlog/text.py deleted file mode 100644 index 57833b2a..00000000 --- a/auditlog/text.py +++ /dev/null @@ -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) diff --git a/auditlog_tests/tests.py b/auditlog_tests/tests.py index 9c52c028..755b2fa0 100644 --- a/auditlog_tests/tests.py +++ b/auditlog_tests/tests.py @@ -1562,20 +1562,26 @@ 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], @@ -1583,7 +1589,7 @@ def test_changes_display_dict_longtextfield_with_truncation_disabled(self): 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" ), ) diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 539a271c..72614e17 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -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 ------