From ab2580311ae4e3516ec944e24539b8789f14d952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Wed, 27 Dec 2023 12:20:01 -0300 Subject: [PATCH] =?UTF-8?q?Armadilha=20para=20descobrir=20dele=C3=A7=C3=A3?= =?UTF-8?q?o=20de=20registros?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/espacos/apps.py | 5 +++++ sigi/apps/espacos/signals.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 sigi/apps/espacos/signals.py diff --git a/sigi/apps/espacos/apps.py b/sigi/apps/espacos/apps.py index 15da7f9..0dab50c 100644 --- a/sigi/apps/espacos/apps.py +++ b/sigi/apps/espacos/apps.py @@ -7,3 +7,8 @@ class EspacosConfig(AppConfig): name = "espacos" name = "sigi.apps.espacos" verbose_name = _("Agenda de espaços") + + def ready(self): + from . import signals + + return super().ready() diff --git a/sigi/apps/espacos/signals.py b/sigi/apps/espacos/signals.py new file mode 100644 index 0000000..af52cc1 --- /dev/null +++ b/sigi/apps/espacos/signals.py @@ -0,0 +1,22 @@ +import traceback +from django.conf import settings +from django.core.mail import send_mail +from django.db.models.signals import pre_delete +from django.dispatch import receiver +from django.utils import timezone +from sigi.apps.espacos.models import Reserva + + +@receiver(pre_delete, sender=Reserva) +def reserva_pre_delete(sender, **kwargs): + origin = kwargs.get("origin", None) + if origin and origin.count() == Reserva.objects.count(): + stack_array = [f"At {timezone.localtime()} all records deleted:"] + stack_array.extend(traceback.format_stack()) + send_mail( + subject="Armadilha deleção de reservas", + message="\n".join(stack_array), + from_email=settings.DEFAULT_FROM_EMAIL, + recipient_list=[email for name, email in settings.ADMINS], + fail_silently=True, + )