add webpushnotification dependency and post_save hook for notifications
This commit is contained in:
parent
4a62ae1ee3
commit
2bffcb2059
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user