Skip to content

Commit cb94131

Browse files
authored
Endpoint: Make post_delete signal more reliable (#12969)
Continuation of #12867
1 parent 29b1dd2 commit cb94131

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

dojo/endpoint/signals.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import contextlib
2+
13
from auditlog.models import LogEntry
24
from django.conf import settings
35
from django.contrib.contenttypes.models import ContentType
@@ -12,18 +14,20 @@
1214

1315
@receiver(post_delete, sender=Endpoint)
1416
def endpoint_post_delete(sender, instance, using, origin, **kwargs):
15-
if instance == origin:
16-
description = _('The endpoint "%(name)s" was deleted') % {"name": str(instance)}
17-
if settings.ENABLE_AUDITLOG:
18-
if le := LogEntry.objects.filter(
19-
action=LogEntry.Action.DELETE,
20-
content_type=ContentType.objects.get(app_label="dojo", model="endpoint"),
21-
object_id=instance.id,
22-
).order_by("-id").first():
23-
description = _('The endpoint "%(name)s" was deleted by %(user)s') % {
24-
"name": str(instance), "user": le.actor}
25-
create_notification(event="endpoint_deleted", # template does not exists, it will default to "other" but this event name needs to stay because of unit testing
26-
title=_("Deletion of %(name)s") % {"name": str(instance)},
27-
description=description,
28-
url=reverse("endpoint"),
29-
icon="exclamation-triangle")
17+
# Catch instances in async delete where a single object is deleted more than once
18+
with contextlib.suppress(sender.DoesNotExist):
19+
if instance == origin:
20+
description = _('The endpoint "%(name)s" was deleted') % {"name": str(instance)}
21+
if settings.ENABLE_AUDITLOG:
22+
if le := LogEntry.objects.filter(
23+
action=LogEntry.Action.DELETE,
24+
content_type=ContentType.objects.get(app_label="dojo", model="endpoint"),
25+
object_id=instance.id,
26+
).order_by("-id").first():
27+
description = _('The endpoint "%(name)s" was deleted by %(user)s') % {
28+
"name": str(instance), "user": le.actor}
29+
create_notification(event="endpoint_deleted", # template does not exists, it will default to "other" but this event name needs to stay because of unit testing
30+
title=_("Deletion of %(name)s") % {"name": str(instance)},
31+
description=description,
32+
url=reverse("endpoint"),
33+
icon="exclamation-triangle")

0 commit comments

Comments
 (0)