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 requests
|
||||||
|
import json
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail
|
||||||
@ -275,6 +276,35 @@ def send_email_on_notification(sender, instance, created, **kwargs):
|
|||||||
html_message=html_message
|
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)
|
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
|
||||||
def create_user_settings(sender, instance, created, **kwargs):
|
def create_user_settings(sender, instance, created, **kwargs):
|
||||||
try:
|
try:
|
||||||
|
@ -12,6 +12,7 @@ packages = find:
|
|||||||
install_requires =
|
install_requires =
|
||||||
djangoldp~=2.1.0
|
djangoldp~=2.1.0
|
||||||
djangoldp_account~=2.1
|
djangoldp_account~=2.1
|
||||||
|
djangoldp_webpushnotification~=1.0.0
|
||||||
|
|
||||||
[options.extras_require]
|
[options.extras_require]
|
||||||
include_package_data = True
|
include_package_data = True
|
||||||
|
Loading…
Reference in New Issue
Block a user