feat: Send e-mail notifications to user

This commit is contained in:
moanos [he/him] 2024-11-20 20:26:44 +01:00
parent 890309564f
commit 231c27819d
4 changed files with 37 additions and 6 deletions

View File

@ -15,3 +15,4 @@ class FellchensammlungConfig(AppConfig):
except Permission.DoesNotExist:
pass
post_migrate.connect(ensure_languages, sender=self)
import fellchensammlung.receivers

View File

@ -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()

View File

@ -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)

View File

@ -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)