diff --git a/README.md b/README.md index 3156f5f..6b5ca29 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,29 @@ 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, **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, **kwargs): + print( + f"An web notification was sent from signal sender {sender} with the" + f"uuid {notification.uuid} to {notification.person}." + ) +``` + ## 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 +201,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..f907eea 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=notification, + ) diff --git a/management/commands/send_web_notifications.py b/management/commands/send_web_notifications.py index 6792b4c..e5e21e5 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=notification, + ) 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..0c2cb8f --- /dev/null +++ b/signals/email_notification_sent.py @@ -0,0 +1,4 @@ +from django.dispatch import Signal + + +email_notification_sent = Signal(providing_args=["notification"]) diff --git a/signals/web_notification_sent.py b/signals/web_notification_sent.py new file mode 100644 index 0000000..800e25f --- /dev/null +++ b/signals/web_notification_sent.py @@ -0,0 +1,4 @@ +from django.dispatch import Signal + + +web_notification_sent = Signal(providing_args=["notification"])