From 2a974ed2b836b54d93b8d249430a034eca08b222 Mon Sep 17 00:00:00 2001 From: Gal Singer Date: Wed, 21 Nov 2018 13:56:30 +0200 Subject: [PATCH 1/4] hex the bytes value before smart text in bytes fields --- src/auditlog/diff.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/auditlog/diff.py b/src/auditlog/diff.py index 9f996f60..7e648c6c 100644 --- a/src/auditlog/diff.py +++ b/src/auditlog/diff.py @@ -75,7 +75,10 @@ def get_field_value(obj, field): value = field.default if field.default is not NOT_PROVIDED else None else: try: - value = smart_text(getattr(obj, field.name, None)) + field_value = getattr(obj, field.name, None) + if isinstance(field_value, bytes): + field_value.hex() + value = smart_text(field_value) except ObjectDoesNotExist: value = field.default if field.default is not NOT_PROVIDED else None From 96494959cc6f3d20ea9569de743615b03706ec10 Mon Sep 17 00:00:00 2001 From: Gal Singer Date: Wed, 21 Nov 2018 14:15:00 +0200 Subject: [PATCH 2/4] and now override the actual data --- src/auditlog/diff.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/auditlog/diff.py b/src/auditlog/diff.py index 7e648c6c..a357d28c 100644 --- a/src/auditlog/diff.py +++ b/src/auditlog/diff.py @@ -77,7 +77,7 @@ def get_field_value(obj, field): try: field_value = getattr(obj, field.name, None) if isinstance(field_value, bytes): - field_value.hex() + field_value = field_value.hex() value = smart_text(field_value) except ObjectDoesNotExist: value = field.default if field.default is not NOT_PROVIDED else None From 6e5d506c2f1f3c85c985272556b1e63f1e2ada2c Mon Sep 17 00:00:00 2001 From: Gal Singer Date: Wed, 21 Nov 2018 14:31:03 +0200 Subject: [PATCH 3/4] support both python2 and python3 using hexify and not .hex --- src/auditlog/diff.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/auditlog/diff.py b/src/auditlog/diff.py index a357d28c..36511738 100644 --- a/src/auditlog/diff.py +++ b/src/auditlog/diff.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +import binascii from django.conf import settings from django.core.exceptions import ObjectDoesNotExist @@ -77,7 +78,7 @@ def get_field_value(obj, field): try: field_value = getattr(obj, field.name, None) if isinstance(field_value, bytes): - field_value = field_value.hex() + field_value = binascii.hexlify(field_value) value = smart_text(field_value) except ObjectDoesNotExist: value = field.default if field.default is not NOT_PROVIDED else None From 902d007a56df685a550969ec5b5fd26b9e18d47b Mon Sep 17 00:00:00 2001 From: Gal Singer Date: Wed, 21 Nov 2018 14:42:33 +0200 Subject: [PATCH 4/4] make the casting only in python3 --- src/auditlog/diff.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/auditlog/diff.py b/src/auditlog/diff.py index 36511738..af6a53bc 100644 --- a/src/auditlog/diff.py +++ b/src/auditlog/diff.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals import binascii +import sys from django.conf import settings from django.core.exceptions import ObjectDoesNotExist @@ -77,7 +78,8 @@ def get_field_value(obj, field): else: try: field_value = getattr(obj, field.name, None) - if isinstance(field_value, bytes): + # python2 every str is a instance of bytes + if isinstance(field_value, bytes) and sys.version_info > (3, 0): field_value = binascii.hexlify(field_value) value = smart_text(field_value) except ObjectDoesNotExist: