From 1dd53a87e9f7d6fa2223d7aeb81106b9b2b1ddeb Mon Sep 17 00:00:00 2001 From: moanos Date: Wed, 20 Nov 2024 23:02:41 +0100 Subject: [PATCH] feat: Notify admins of new user via notification framework #10 --- src/fellchensammlung/mail.py | 18 ------------------ src/fellchensammlung/receivers.py | 22 +++++++++++++++++++++- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/fellchensammlung/mail.py b/src/fellchensammlung/mail.py index af550f3..99e0b74 100644 --- a/src/fellchensammlung/mail.py +++ b/src/fellchensammlung/mail.py @@ -32,24 +32,6 @@ def mail_admins_new_report(report): message.send() -@receiver(post_save, sender=User) -def mail_admins_new_member(sender, instance: User, created: bool, **kwargs): - if not created: - return - subject = _("Neuer User") + f": {instance.username}" - for moderator in User.objects.filter(trust_level__gt=User.TRUST_LEVEL[User.MODERATOR]): - greeting = _("Moin,") + "{NEWLINE}" - new_report_text = _("es hat sich eine neue Person registriert.") + "{NEWLINE}" - user_detail_text = _("Username") + f": {instance.username}{NEWLINE}" + _( - "E-Mail") + f": {instance.email}{NEWLINE}" - user_url = "https://" + host + instance.get_absolute_url() - link_text = f"Um alle Details zu sehen, geh bitte auf: {user_url}" - body_text = greeting + new_report_text + user_detail_text + link_text - 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) diff --git a/src/fellchensammlung/receivers.py b/src/fellchensammlung/receivers.py index e826fd3..1512d84 100644 --- a/src/fellchensammlung/receivers.py +++ b/src/fellchensammlung/receivers.py @@ -1,7 +1,9 @@ from django.db.models.signals import post_save from django.dispatch import receiver -from fellchensammlung.models import BaseNotification, CommentNotification +from fellchensammlung.models import BaseNotification, CommentNotification, User, TrustLevel from .tasks import task_send_notification_email +from notfellchen.settings import host +from django.utils.translation import gettext_lazy as _ @receiver(post_save, sender=CommentNotification) @@ -15,3 +17,21 @@ def base_notification_receiver(sender, instance: BaseNotification, created: bool return else: task_send_notification_email.delay(instance.pk) + + +@receiver(post_save, sender=User) +def notification_new_user(sender, instance: User, created: bool, **kwargs): + NEWLINE = "\r\n" + if not created: + return + # Create Notification text + subject = _("Neuer User") + f": {instance.username}" + new_user_text = _("Es hat sich eine neue Person registriert.") + f"{NEWLINE}" + user_detail_text = _("Username") + f": {instance.username}{NEWLINE}" + _( + "E-Mail") + f": {instance.email}{NEWLINE}" + user_url = "https://" + host + instance.get_absolute_url() + link_text = f"Um alle Details zu sehen, geh bitte auf: {user_url}" + body_text = new_user_text + user_detail_text + link_text + for moderator in User.objects.filter(trust_level__gt=TrustLevel.MODERATOR): + notification = BaseNotification.objects.create(title=subject, text=body_text, user=moderator) + notification.save()