feat: Completely rework notification framework
This change is needed as a) there are many different types of notifications. A notification about a new comment does not only relate the comment but also adoption notice and a user that made the comment. Therefore, I introduce a new general notification that has optional links to other objects. b) this change allows to define a different representation of e-mails vs. display on the website. The new notification type is in charge of representation from now on. Title and text will be deprecated and eventually removed
This commit is contained in:
@@ -1,23 +1,22 @@
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
from fellchensammlung.models import BaseNotification, CommentNotification, User, TrustLevel, RescueOrganization
|
||||
from fellchensammlung.models import Notification, User, TrustLevel, RescueOrganization, \
|
||||
NotificationTypeChoices
|
||||
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)
|
||||
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:
|
||||
@receiver(post_save, sender=Notification)
|
||||
def base_notification_receiver(sender, instance: Notification, created: bool, **kwargs):
|
||||
print("Dada")
|
||||
if not created or not instance.user_to_notify.email_notifications:
|
||||
return
|
||||
else:
|
||||
print("Dodo")
|
||||
task_send_notification_email.delay(instance.pk)
|
||||
|
||||
|
||||
@receiver(post_save, sender=RescueOrganization)
|
||||
def rescue_org_receiver(sender, instance: RescueOrganization, created: bool, **kwargs):
|
||||
if instance.location:
|
||||
@@ -40,5 +39,9 @@ def notification_new_user(sender, instance: User, created: bool, **kwargs):
|
||||
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 = Notification.objects.create(title=subject,
|
||||
text=body_text,
|
||||
notification_type=NotificationTypeChoices.NEW_USER,
|
||||
user_to_notify=moderator,
|
||||
user_related=instance)
|
||||
notification.save()
|
||||
|
Reference in New Issue
Block a user