|
| 1 | +import contextlib |
| 2 | + |
1 | 3 | from auditlog.models import LogEntry |
2 | 4 | from django.conf import settings |
3 | 5 | from django.contrib.contenttypes.models import ContentType |
|
12 | 14 |
|
13 | 15 | @receiver(post_delete, sender=Endpoint) |
14 | 16 | 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