From 4a78bf7de5c66b0127f17928d9681d3835199abe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Benjamin=20H=C3=A4ttasch?=
 <benjamin.haettasch@fachschaft.informatik.tu-darmstadt.de>
Date: Mon, 16 May 2022 01:22:44 +0200
Subject: [PATCH] Introduce sending of mails a new slot is added for a
 published plan

This will send an email with information about the slot and a link to the corresponding AK to the contact mail adress specified for a given event, but only if the plan feature is used, the plan is already visible and the slot was not directly scheduled
This implements #117
---
 AKSubmission/models.py | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/AKSubmission/models.py b/AKSubmission/models.py
index 9f435bd8..cd8a855b 100644
--- a/AKSubmission/models.py
+++ b/AKSubmission/models.py
@@ -1,17 +1,18 @@
+from django.apps import apps
 from django.conf import settings
 from django.core.mail import EmailMessage
 from django.db.models.signals import post_save
 from django.dispatch import receiver
 from django.urls import reverse_lazy
 
-from AKModel.models import AKOrgaMessage
+from AKModel.models import AKOrgaMessage, AKSlot
 
 
 @receiver(post_save, sender=AKOrgaMessage)
-def orga_message_saved_handler(sender, instance: AKOrgaMessage, **kwargs):
-    # React to saved (thus new) Orga message by sending an email
+def orga_message_saved_handler(sender, instance: AKOrgaMessage, created, **kwargs):
+    # React to newly created Orga message by sending an email
 
-    if settings.SEND_MAILS:
+    if created and settings.SEND_MAILS:
         host = 'https://' + settings.ALLOWED_HOSTS[0] if len(settings.ALLOWED_HOSTS) > 0 else 'http://127.0.0.1:8000'
         url = f"{host}{reverse_lazy('submit:ak_detail', kwargs={'pk': instance.ak.pk, 'event_slug': instance.ak.event.slug})}"
 
@@ -22,3 +23,20 @@ def orga_message_saved_handler(sender, instance: AKOrgaMessage, **kwargs):
             [instance.ak.event.contact_email]
         )
         mail.send(fail_silently=True)
+
+
+@receiver(post_save, sender=AKSlot)
+def slot_created_handler(sender, instance: AKSlot, created, **kwargs):
+    # React to slots that are created after the plan was already published by sending an email
+
+    if created and settings.SEND_MAILS and apps.is_installed("AKPlan") and not instance.event.plan_hidden and instance.room is None and instance.start is None:
+        host = 'https://' + settings.ALLOWED_HOSTS[0] if len(settings.ALLOWED_HOSTS) > 0 else 'http://127.0.0.1:8000'
+        url = f"{host}{reverse_lazy('submit:ak_detail', kwargs={'pk': instance.ak.pk, 'event_slug': instance.ak.event.slug})}"
+
+        mail = EmailMessage(
+            f"[AKPlanning] New slot for AK '{instance.ak}' ({instance.ak.event}) added",
+            f"New slot requested.\n\nAK: {instance.ak}\nDuration: {instance.duration}\n\n{url}",
+            settings.DEFAULT_FROM_EMAIL,
+            [instance.ak.event.contact_email]
+        )
+        mail.send(fail_silently=True)
-- 
GitLab