diff --git a/src/fellchensammlung/apps.py b/src/fellchensammlung/apps.py index 5c1f8b3..467b777 100644 --- a/src/fellchensammlung/apps.py +++ b/src/fellchensammlung/apps.py @@ -15,3 +15,4 @@ class FellchensammlungConfig(AppConfig): except Permission.DoesNotExist: pass post_migrate.connect(ensure_languages, sender=self) + import fellchensammlung.receivers diff --git a/src/fellchensammlung/mail.py b/src/fellchensammlung/mail.py index b4dbd25..871142d 100644 --- a/src/fellchensammlung/mail.py +++ b/src/fellchensammlung/mail.py @@ -1,15 +1,10 @@ -from venv import create - -import django.conf.global_settings from django.db.models.signals import post_save from django.dispatch import receiver from django.utils.translation import gettext_lazy as _ -from django.utils.translation import gettext from django.conf import settings from django.core import mail -from django.db.models import Q, Min -from fellchensammlung.models import User +from fellchensammlung.models import User, CommentNotification, BaseNotification from notfellchen.settings import host NEWLINE = "\r\n" @@ -53,3 +48,14 @@ def mail_admins_new_member(sender, instance: User, created: bool, **kwargs): message = mail.EmailMessage(subject, body_text, settings.DEFAULT_FROM_EMAIL, [moderator.email]) print("Sending email to ", moderator.email) message.send() + + +def send_notification_email(notification_pk): + try: + notification = CommentNotification.objects.get(pk=notification_pk) + except CommentNotification.DoesNotExist: + notification = BaseNotification.objects.get(pk=notification_pk) + subject = f"🔔 {notification.title}" + body_text = notification.text + message = mail.EmailMessage(subject, body_text, settings.DEFAULT_FROM_EMAIL, [notification.user.email]) + message.send() diff --git a/src/fellchensammlung/receivers.py b/src/fellchensammlung/receivers.py new file mode 100644 index 0000000..e826fd3 --- /dev/null +++ b/src/fellchensammlung/receivers.py @@ -0,0 +1,17 @@ +from django.db.models.signals import post_save +from django.dispatch import receiver +from fellchensammlung.models import BaseNotification, CommentNotification +from .tasks import task_send_notification_email + + +@receiver(post_save, sender=CommentNotification) +def comment_notification_receiver(sender, instance: BaseNotification, created: bool, **kwargs): + base_notification_receiver(sender, instance, created, **kwargs) + + +@receiver(post_save, sender=BaseNotification) +def base_notification_receiver(sender, instance: BaseNotification, created: bool, **kwargs): + if not created or not instance.user.email_notifications: + return + else: + task_send_notification_email.delay(instance.pk) diff --git a/src/fellchensammlung/tasks.py b/src/fellchensammlung/tasks.py index d1f3dc8..9f48a07 100644 --- a/src/fellchensammlung/tasks.py +++ b/src/fellchensammlung/tasks.py @@ -1,5 +1,7 @@ +from celery.app import shared_task from django.utils import timezone from notfellchen.celery import app as celery_app +from .mail import send_notification_email from .tools.admin import clean_locations, deactivate_unchecked_adoption_notices, deactivate_404_adoption_notices from .tools.misc import healthcheck_ok from .models import Location, AdoptionNotice, Timestamp @@ -43,3 +45,8 @@ def add_adoption_notice_location(pk): def task_healthcheck(): healthcheck_ok() set_timestamp("task_healthcheck") + + +@shared_task +def task_send_notification_email(notification_pk): + send_notification_email(notification_pk)