35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
from django.db import transaction
|
|
from django.db.models.signals import post_delete, post_save
|
|
from django.dispatch import receiver
|
|
|
|
from notify.delivery import get_provider
|
|
from notify.models import NotificationAttachment, NotificationRecipient
|
|
|
|
__all__ = [
|
|
"delete_attachment_file",
|
|
"post_save_notification",
|
|
]
|
|
|
|
|
|
@receiver(post_save, sender=NotificationRecipient)
|
|
def post_save_notification(sender, instance: NotificationRecipient, created, **kwargs):
|
|
if not created:
|
|
return
|
|
provider = get_provider(instance.channel)
|
|
if provider is None or not provider.requires_async_delivery:
|
|
return
|
|
recipient_id = instance.id
|
|
|
|
def enqueue():
|
|
current = get_provider(instance.channel)
|
|
if current is not None:
|
|
current.enqueue(recipient_id)
|
|
|
|
transaction.on_commit(enqueue)
|
|
|
|
|
|
@receiver(post_delete, sender=NotificationAttachment)
|
|
def delete_attachment_file(sender, instance: NotificationAttachment, **kwargs):
|
|
if instance.file:
|
|
instance.file.delete(save=False)
|