From e36987e907a2ce87ab700c46dd4d25fd419071b3 Mon Sep 17 00:00:00 2001 From: jmr Date: Wed, 16 Jun 2021 18:56:20 +0200 Subject: [PATCH 1/2] declare and send signals after web/email notification is sent --- README.md | 20 +++++++++++++++++++ .../commands/send_email_notifications.py | 5 +++++ management/commands/send_web_notifications.py | 5 +++++ signals/__init__.py | 7 +++++++ signals/email_notification_sent.py | 4 ++++ signals/web_notification_sent.py | 4 ++++ 6 files changed, 45 insertions(+) create mode 100644 signals/__init__.py create mode 100644 signals/email_notification_sent.py create mode 100644 signals/web_notification_sent.py diff --git a/README.md b/README.md index 3156f5f..1567da8 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,23 @@ call_command("send_web_notifications") The commands are calling the `process` function on their respective handlers for each notification that are found in the DB with the "Pending" state. +## Knowing when notifications are sent + +We use signals to know when the task runner gets its job done. To know when a notification has been sent, just connect to the signals doing so : + +```python +from django.dispatch import receiver +from osis_notification.signals import email_notification_sent, web_notification_sent + +@receiver(email_notification_sent) +def email_notification_has_been_sent(sender, notification_uuid, **kwargs): + print(f"An email notification was sent from sender {sender} with the uuid {notification_uuid}") + +@receiver(web_notification_sent) +def web_notification_has_been_sent(sender, notification_uuid, **kwargs): + print(f"A web notification was sent from sender {sender} with the uuid {notification_uuid}") +``` + ## Cleaning notifications To avoid database overflowing, all the sent email notifications and the read web notifications are deleted after a defined retention duration. You will have to define this duration in your Django settings like this : @@ -178,3 +195,6 @@ Then you can integrate the component: - `data-url` : API endpoint that returns all the notifications. - `data-interval` : The interval, in second, to fetch the notifications from the server (default to 300). + + + diff --git a/management/commands/send_email_notifications.py b/management/commands/send_email_notifications.py index 789da83..ec67038 100644 --- a/management/commands/send_email_notifications.py +++ b/management/commands/send_email_notifications.py @@ -2,6 +2,7 @@ from osis_notification.contrib.handlers import EmailNotificationHandler from osis_notification.models import EmailNotification +from osis_notification.signals import email_notification_sent class Command(BaseCommand): @@ -10,3 +11,7 @@ class Command(BaseCommand): def handle(self, *args, **options): for notification in EmailNotification.objects.pending(): EmailNotificationHandler.process(notification) + email_notification_sent.send( + sender=self.__class__, + notification_uuid=notification.uuid, + ) diff --git a/management/commands/send_web_notifications.py b/management/commands/send_web_notifications.py index 6792b4c..7ae5107 100644 --- a/management/commands/send_web_notifications.py +++ b/management/commands/send_web_notifications.py @@ -2,6 +2,7 @@ from osis_notification.contrib.handlers import WebNotificationHandler from osis_notification.models import WebNotification +from osis_notification.signals import web_notification_sent class Command(BaseCommand): @@ -10,3 +11,7 @@ class Command(BaseCommand): def handle(self, *args, **options): for notification in WebNotification.objects.pending(): WebNotificationHandler.process(notification) + web_notification_sent.send( + sender=self.__class__, + notification_uuid=notification.uuid, + ) diff --git a/signals/__init__.py b/signals/__init__.py new file mode 100644 index 0000000..acf2678 --- /dev/null +++ b/signals/__init__.py @@ -0,0 +1,7 @@ +from osis_notification.signals.email_notification_sent import email_notification_sent +from osis_notification.signals.web_notification_sent import web_notification_sent + +__all__ = [ + "email_notification_sent", + "web_notification_sent", +] diff --git a/signals/email_notification_sent.py b/signals/email_notification_sent.py new file mode 100644 index 0000000..5f27808 --- /dev/null +++ b/signals/email_notification_sent.py @@ -0,0 +1,4 @@ +from django.dispatch import Signal + + +email_notification_sent = Signal(providing_args=["notification_uuid"]) diff --git a/signals/web_notification_sent.py b/signals/web_notification_sent.py new file mode 100644 index 0000000..2aa8aff --- /dev/null +++ b/signals/web_notification_sent.py @@ -0,0 +1,4 @@ +from django.dispatch import Signal + + +web_notification_sent = Signal(providing_args=["notification_uuid"]) From 064562d9d2eb6b3bef7f469c6027955230966cff Mon Sep 17 00:00:00 2001 From: jmr Date: Mon, 28 Jun 2021 10:04:35 +0200 Subject: [PATCH 2/2] Sending the whole notification in the signal --- README.md | 14 ++++++++++---- management/commands/send_email_notifications.py | 2 +- management/commands/send_web_notifications.py | 2 +- signals/email_notification_sent.py | 2 +- signals/web_notification_sent.py | 2 +- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1567da8..6b5ca29 100644 --- a/README.md +++ b/README.md @@ -141,12 +141,18 @@ from django.dispatch import receiver from osis_notification.signals import email_notification_sent, web_notification_sent @receiver(email_notification_sent) -def email_notification_has_been_sent(sender, notification_uuid, **kwargs): - print(f"An email notification was sent from sender {sender} with the uuid {notification_uuid}") +def email_notification_has_been_sent(sender, notification, **kwargs): + print( + f"An email notification was sent from signal sender {sender} with the" + f"uuid {notification.uuid} to {notification.person}." + ) @receiver(web_notification_sent) -def web_notification_has_been_sent(sender, notification_uuid, **kwargs): - print(f"A web notification was sent from sender {sender} with the uuid {notification_uuid}") +def web_notification_has_been_sent(sender, notification, **kwargs): + print( + f"An web notification was sent from signal sender {sender} with the" + f"uuid {notification.uuid} to {notification.person}." + ) ``` ## Cleaning notifications diff --git a/management/commands/send_email_notifications.py b/management/commands/send_email_notifications.py index ec67038..f907eea 100644 --- a/management/commands/send_email_notifications.py +++ b/management/commands/send_email_notifications.py @@ -13,5 +13,5 @@ def handle(self, *args, **options): EmailNotificationHandler.process(notification) email_notification_sent.send( sender=self.__class__, - notification_uuid=notification.uuid, + notification=notification, ) diff --git a/management/commands/send_web_notifications.py b/management/commands/send_web_notifications.py index 7ae5107..e5e21e5 100644 --- a/management/commands/send_web_notifications.py +++ b/management/commands/send_web_notifications.py @@ -13,5 +13,5 @@ def handle(self, *args, **options): WebNotificationHandler.process(notification) web_notification_sent.send( sender=self.__class__, - notification_uuid=notification.uuid, + notification=notification, ) diff --git a/signals/email_notification_sent.py b/signals/email_notification_sent.py index 5f27808..0c2cb8f 100644 --- a/signals/email_notification_sent.py +++ b/signals/email_notification_sent.py @@ -1,4 +1,4 @@ from django.dispatch import Signal -email_notification_sent = Signal(providing_args=["notification_uuid"]) +email_notification_sent = Signal(providing_args=["notification"]) diff --git a/signals/web_notification_sent.py b/signals/web_notification_sent.py index 2aa8aff..800e25f 100644 --- a/signals/web_notification_sent.py +++ b/signals/web_notification_sent.py @@ -1,4 +1,4 @@ from django.dispatch import Signal -web_notification_sent = Signal(providing_args=["notification_uuid"]) +web_notification_sent = Signal(providing_args=["notification"])