Skip to content

Commit

Permalink
Handle ObjectDoesNotExist in evaluation of repr
Browse files Browse the repository at this point in the history
  • Loading branch information
aleh-rymasheuski committed Dec 2, 2023
1 parent c581a6e commit 32078be
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
- Python: Confirm Python 3.12 support ([#572](https://github.com/jazzband/django-auditlog/pull/572))
- feat: `thread.local` replaced with `ContextVar` to improve context managers in Django 4.2+

#### Fixes

- fix: Handle `ObjectDoesNotExist` in evaluation of `object_repr` ([#592](https://github.com/jazzband/django-auditlog/pull/592))

## 3.0.0-beta.2 (2023-10-05)

#### Breaking Changes
Expand Down
14 changes: 12 additions & 2 deletions auditlog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

from auditlog.diff import mask_str

DEFAULT_OBJECT_REPR = "<error forming object repr>"


class LogEntryManager(models.Manager):
"""
Expand Down Expand Up @@ -54,7 +56,11 @@ def log_create(self, instance, force_log: bool = False, **kwargs):
"content_type", ContentType.objects.get_for_model(instance)
)
kwargs.setdefault("object_pk", pk)
kwargs.setdefault("object_repr", smart_str(instance))
try:
object_repr = smart_str(instance)
except ObjectDoesNotExist:
object_repr = DEFAULT_OBJECT_REPR
kwargs.setdefault("object_repr", object_repr)
kwargs.setdefault(
"serialized_data", self._get_serialized_data_or_none(instance)
)
Expand Down Expand Up @@ -96,7 +102,11 @@ def log_m2m_changes(
"content_type", ContentType.objects.get_for_model(instance)
)
kwargs.setdefault("object_pk", pk)
kwargs.setdefault("object_repr", smart_str(instance))
try:
object_repr = smart_str(instance)
except ObjectDoesNotExist:
object_repr = DEFAULT_OBJECT_REPR

Check warning on line 108 in auditlog/models.py

View check run for this annotation

Codecov / codecov/patch

auditlog/models.py#L107-L108

Added lines #L107 - L108 were not covered by tests
kwargs.setdefault("object_repr", object_repr)
kwargs.setdefault("action", LogEntry.Action.UPDATE)

if isinstance(pk, int):
Expand Down
3 changes: 3 additions & 0 deletions auditlog_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class RelatedModel(RelatedModelParent):

history = AuditlogHistoryField(delete_related=True)

def __str__(self):
return f"RelatedModel #{self.pk} -> {self.related.id}"


class ManyRelatedModel(models.Model):
"""
Expand Down
21 changes: 20 additions & 1 deletion auditlog_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from auditlog.context import disable_auditlog, set_actor
from auditlog.diff import model_instance_diff
from auditlog.middleware import AuditlogMiddleware
from auditlog.models import LogEntry
from auditlog.models import DEFAULT_OBJECT_REPR, LogEntry
from auditlog.registry import AuditlogModelRegistry, AuditLogRegistrationError, auditlog
from auditlog.signals import post_log, pre_log
from auditlog_tests.fixtures.custom_get_cid import get_cid as custom_get_cid
Expand Down Expand Up @@ -1860,6 +1860,25 @@ def test_diff_models_with_related_fields(self):
model_instance_diff(simple2, simple1)
model_instance_diff(simple1, simple2)

def test_object_repr_related_deleted(self):
"""No error is raised when __str__() loads a related object that has been deleted."""
simple = SimpleModel()
simple.save()
related = RelatedModel(related=simple, one_to_one=simple)
related.save()
related_id = related.id

related.refresh_from_db()
simple.delete()
related.delete()

log_entry = (
LogEntry.objects.get_for_model(RelatedModel)
.filter(object_id=related_id)
.get(action=LogEntry.Action.DELETE)
)
self.assertEqual(log_entry.object_repr, DEFAULT_OBJECT_REPR)

def test_when_field_doesnt_exist(self):
"""No error is raised and the default is returned."""
first = SimpleModel(boolean=True)
Expand Down

0 comments on commit 32078be

Please sign in to comment.