Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auditing binary model fields instead of throwing error #689

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions auditlog_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,12 @@ class NullableJSONModel(models.Model):
history = AuditlogHistoryField(delete_related=False)


class BinaryModel(models.Model):
binary = models.BinaryField(null=True, blank=True)

history = AuditlogHistoryField(delete_related=False)


class SerializeThisModel(models.Model):
label = models.CharField(max_length=24, unique=True)
timestamp = models.DateTimeField()
Expand Down Expand Up @@ -431,6 +437,7 @@ class AutoManyRelatedModel(models.Model):
auditlog.register(NoDeleteHistoryModel)
auditlog.register(JSONModel)
auditlog.register(NullableJSONModel)
auditlog.register(BinaryModel)
auditlog.register(
SerializeThisModel,
serialize_data=True,
Expand Down
24 changes: 23 additions & 1 deletion auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
AdditionalDataIncludedModel,
AltPrimaryKeyModel,
AutoManyRelatedModel,
BinaryModel,
CharfieldTextfieldModel,
ChoicesFieldModel,
DateTimeFieldModel,
Expand Down Expand Up @@ -1159,6 +1160,27 @@ def test_json_field_value_none(self):
)


class BinaryModelTest(TestCase):
def test_model_with_binary_field(self):
instance = BinaryModel.objects.create(binary=b"\xde\xad\xbe\xef")
self.assertEqual(instance.history.count(), 1)

obj_log_entry = instance.history.latest()

self.assertEqual(obj_log_entry.changes_dict["binary"][0], "None")
self.assertEqual(obj_log_entry.changes_dict["binary"][1], "deadbeef")

instance.binary = None
instance.save()

self.assertEqual(instance.history.count(), 2)

obj_log_entry = instance.history.latest()

self.assertEqual(obj_log_entry.changes_dict["binary"][0], "deadbeef")
self.assertEqual(obj_log_entry.changes_dict["binary"][1], "None")


class UnregisterTest(TestCase):
def setUp(self):
auditlog.unregister(SimpleModel)
Expand Down Expand Up @@ -1264,7 +1286,7 @@ def test_register_models_register_app(self):

self.assertTrue(self.test_auditlog.contains(SimpleExcludeModel))
self.assertTrue(self.test_auditlog.contains(ChoicesFieldModel))
self.assertEqual(len(self.test_auditlog.get_models()), 31)
self.assertEqual(len(self.test_auditlog.get_models()), 32)

def test_register_models_register_model_with_attrs(self):
self.test_auditlog._register_models(
Expand Down
Loading