Add AUDITLOG_TRUNCATE_CHANGES_DISPLAY and AUDITLOG_TRUNCATE_LIMIT (#684)

* Add AUDITLOG_TRUNCATE_CHANGES_DISPLAY and AUDITLOG_TRUNCATE_LIMIT

To configure how many characters will be truncated or disable it

* 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:
Hoàng Quốc Hưng 2024-11-04 20:39:25 +07:00 committed by GitHub
parent 938e644177
commit d4f99c2729
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 additions and 3 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

@ -45,3 +45,8 @@ settings.AUDITLOG_USE_TEXT_CHANGES_IF_JSON_IS_NOT_PRESENT = getattr(
settings.AUDITLOG_DISABLE_REMOTE_ADDR = getattr(
settings, "AUDITLOG_DISABLE_REMOTE_ADDR", False
)
# Number of characters at which changes_display_dict property should be shown
settings.AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH = getattr(
settings, "AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH", 140
)

View file

@ -506,9 +506,9 @@ class LogEntry(models.Model):
elif field_type in ["ForeignKey", "OneToOneField"]:
value = self._get_changes_display_for_fk_field(field, value)
# check if length is longer than 140 and truncate with ellipsis
if len(value) > 140:
value = f"{value[:140]}..."
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

@ -1561,6 +1561,37 @@ 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_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[: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_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 {length} characters"
"as AUDITLOG_TRUNCATE_CHANGES_DISPLAY is set to a negative number"
),
)
class PostgresArrayFieldModelTest(TestCase):
databases = "__all__"

View file

@ -305,6 +305,16 @@ If the value is `None`, the default getter will be used.
.. versionadded:: 3.0.0
**AUDITLOG_CHANGE_DISPLAY_TRUNCATE_LENGTH**
This configuration variable defines the truncation behavior for strings in `changes_display_dict`, with a default value of `140` characters.
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.
.. versionadded:: 3.1.0
Actors
------