mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
Add AUDITLOG_TRUNCATE_CHANGES_DISPLAY and AUDITLOG_TRUNCATE_LIMIT
To configure how many characters will be truncated or disable it
This commit is contained in:
parent
b1ecc8f754
commit
4541be6f21
5 changed files with 59 additions and 3 deletions
|
|
@ -45,3 +45,11 @@ settings.AUDITLOG_USE_TEXT_CHANGES_IF_JSON_IS_NOT_PRESENT = getattr(
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ 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>"
|
||||
|
||||
|
|
@ -506,9 +507,11 @@ 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]}..."
|
||||
if (
|
||||
settings.AUDITLOG_TRUNCATE_CHANGES_DISPLAY
|
||||
and len(value) > settings.AUDITLOG_TRUNCATE_LIMIT
|
||||
):
|
||||
value = truncatechars(value, settings.AUDITLOG_TRUNCATE_LIMIT)
|
||||
|
||||
values_display.append(value)
|
||||
|
||||
|
|
|
|||
11
auditlog/text.py
Normal file
11
auditlog/text.py
Normal file
|
|
@ -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)
|
||||
|
|
@ -1561,6 +1561,32 @@ 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
|
||||
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__"
|
||||
|
|
|
|||
|
|
@ -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
|
||||
------
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue