Skip to content

Commit

Permalink
Add AUDITLOG_TRUNCATE_CHANGES_DISPLAY and AUDITLOG_TRUNCATE_LIMIT
Browse files Browse the repository at this point in the history
To configure how many characters will be truncated or disable it
  • Loading branch information
hoangquochung1110 committed Oct 31, 2024
1 parent b1ecc8f commit 5bdd0da
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
8 changes: 8 additions & 0 deletions auditlog/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,11 @@
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)
9 changes: 6 additions & 3 deletions auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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 @@ -506,9 +507,11 @@ def changes_display_dict(self):
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]}..."
if (
settings.AUDITLOG_TRUNCATE_CHANGES_DISPLAY
and len(value) > settings.AUDITLOG_TRUNCATE_LIMIT
):
value = truncatechars(value, settings.AUDITLOG_TRUNCATE_LIMIT)

values_display.append(value)

Expand Down
11 changes: 11 additions & 0 deletions auditlog/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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)
26 changes: 26 additions & 0 deletions auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,32 @@ def test_changes_display_dict_longtextfield(self):
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
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_with_truncation_disabled(self):
with override_settings(AUDITLOG_TRUNCATE_CHANGES_DISPLAY=False):
limit = settings.AUDITLOG_TRUNCATE_LIMIT
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"
),
)


class PostgresArrayFieldModelTest(TestCase):
databases = "__all__"
Expand Down
8 changes: 8 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ If the value is `None`, the default getter will be used.

.. versionadded:: 3.0.0

**AUDITLOG_TRUNCATE_CHANGES_DISPLAY**

You can use this settings to truncate characters in `changes_display_dict` property, True by default

**AUDITLOG_TRUNCATE_LIMIT**

Number of characters at which `changes_display_dict` property should be truncated

Actors
------

Expand Down

0 comments on commit 5bdd0da

Please sign in to comment.