Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

declare and send signals after web/email notification is sent #14

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}."
)
```

AL9000 marked this conversation as resolved.
Show resolved Hide resolved
## 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 :
Expand Down Expand Up @@ -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).



5 changes: 5 additions & 0 deletions management/commands/send_email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
)
5 changes: 5 additions & 0 deletions management/commands/send_web_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
)
7 changes: 7 additions & 0 deletions signals/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
]
4 changes: 4 additions & 0 deletions signals/email_notification_sent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.dispatch import Signal


email_notification_sent = Signal(providing_args=["notification"])
4 changes: 4 additions & 0 deletions signals/web_notification_sent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.dispatch import Signal


web_notification_sent = Signal(providing_args=["notification"])