add webpushnotification dependency and post_save hook for notifications

This commit is contained in:
Fay Arnold 2021-04-30 14:30:55 +01:00
parent 4a62ae1ee3
commit 2bffcb2059
2 changed files with 31 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import requests
import json
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.mail import send_mail
@ -275,6 +276,35 @@ def send_email_on_notification(sender, instance, created, **kwargs):
html_message=html_message
)
@receiver(post_save, sender=Notification)
def send_webpush_on_notification(sender, instance, created, **kwargs):
if instance.summary :
if instance.user: # if the person exists and accepts notifications
try:
# local author
if instance.author.startswith(settings.SITE_URL):
who = str(Model.resolve_id(instance.author.replace(settings.SITE_URL, '')).get_full_name())
# external author
else:
who = requests.get(instance.author).json()['name']
except:
who = "Quelqu'un"
payload = {
"head": "Notification from " + who,
"body": instance.summary,
"id": instance.user.id
} # make the payload that we wanna send?
url = settings.SITE_URL + reverse("send_webpush") # get the url to post webpush to
response = requests.post(
url,
data=json.dumps(payload)
)
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_user_settings(sender, instance, created, **kwargs):
try:

View File

@ -12,6 +12,7 @@ packages = find:
install_requires =
djangoldp~=2.1.0
djangoldp_account~=2.1
djangoldp_webpushnotification~=1.0.0
[options.extras_require]
include_package_data = True