57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
# combo - content management system
|
|
# Copyright (C) 2015-2018 Entr'ouvert
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU Affero General Public License as published
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import json
|
|
import logging
|
|
|
|
from django.conf import settings
|
|
from django.db.models.signals import post_save
|
|
from django.dispatch import receiver
|
|
|
|
try:
|
|
import pywebpush
|
|
except ImportError:
|
|
pywebpush = None
|
|
|
|
from combo.apps.notifications.models import Notification
|
|
|
|
from .models import PushSubscription
|
|
|
|
|
|
@receiver(post_save, sender=Notification)
|
|
def notification(sender, instance=None, created=False, **kwargs):
|
|
if not pywebpush:
|
|
return
|
|
if not created:
|
|
return
|
|
message = json.dumps({
|
|
'summary': instance.summary,
|
|
'body': instance.body,
|
|
'url': instance.url,
|
|
})
|
|
|
|
for subscription in PushSubscription.objects.filter(user_id=instance.user_id):
|
|
try:
|
|
pywebpush.webpush(
|
|
subscription_info=subscription.subscription_info,
|
|
data=message,
|
|
vapid_private_key=settings.PWA_VAPID_PRIVATE_KEY,
|
|
vapid_claims=settings.PWA_VAPID_CLAIMS
|
|
)
|
|
except pywebpush.WebPushException as e:
|
|
logger = logging.getLogger(__name__)
|
|
logger.exception('webpush error (%r)', e)
|