Skip to content

Commit c766ab1

Browse files
committed
Fixed crash when auditing on binary data
1 parent 4134b15 commit c766ab1

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

easyaudit/tests/test_app/tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from unittest import skip, skipIf
55

66
import django
7+
from django.db.models import BinaryField
8+
9+
from easyaudit.utils import get_field_value
710

811
asgi_views_supported = django.VERSION >= (3, 1)
912
if asgi_views_supported:
@@ -236,3 +239,25 @@ def test_request_event_admin_no_users(self):
236239
response = self.client.get(reverse('admin:easyaudit_requestevent_changelist'))
237240
self.assertEqual(200, response.status_code)
238241
filters = self._list_filters(response.content)
242+
243+
244+
@override_settings(TEST=True)
245+
class TestBinaryData(TestCase):
246+
def test_binary_data(self):
247+
class FakeObject:
248+
pass
249+
obj = FakeObject()
250+
obj.binary_data = b'\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00'
251+
binary_field = BinaryField()
252+
binary_field.name = 'binary_data'
253+
254+
# Small
255+
actual = get_field_value(obj, binary_field)
256+
expected = r"b'\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00'"
257+
assert actual == expected
258+
259+
# Large
260+
obj.binary_data = b'\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00' * 100
261+
actual = get_field_value(obj, binary_field)
262+
assert actual.startswith(expected[:-1])
263+
assert actual.endswith('[truncated 2000 bytes]')

easyaudit/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from __future__ import unicode_literals
22

3-
from uuid import UUID
4-
53
from django.conf import settings
64
from django.core.exceptions import ObjectDoesNotExist
75
from django.db.models import NOT_PROVIDED, DateTimeField
@@ -19,18 +17,24 @@ def get_field_value(obj, field):
1917
:return: The value of the field as a string.
2018
:rtype: str
2119
"""
20+
raw_value = getattr(obj, field.name, None)
2221
if isinstance(field, DateTimeField):
2322
# DateTimeFields are timezone-aware, so we need to convert the field
2423
# to its naive form before we can accuratly compare them for changes.
2524
try:
26-
value = field.to_python(getattr(obj, field.name, None))
25+
value = field.to_python(raw_value)
2726
if value is not None and settings.USE_TZ and not timezone.is_naive(value):
2827
value = timezone.make_naive(value, timezone=timezone.utc)
2928
except ObjectDoesNotExist:
3029
value = field.default if field.default is not NOT_PROVIDED else None
30+
elif isinstance(raw_value, bytes):
31+
if len(raw_value) > 100:
32+
return repr(raw_value[:100]) + '[truncated {} bytes]'.format(len(raw_value) - 100)
33+
else:
34+
return repr(raw_value)
3135
else:
3236
try:
33-
value = smart_text(getattr(obj, field.name, None))
37+
value = smart_text(raw_value)
3438
except ObjectDoesNotExist:
3539
value = field.default if field.default is not NOT_PROVIDED else None
3640

0 commit comments

Comments
 (0)