pwa: create a management command send_webpush (#25462)

This commit is contained in:
Elias Showk 2018-08-23 16:30:08 +02:00
parent 1b4edd9a52
commit 7dbdf23e3b
5 changed files with 119 additions and 0 deletions

View File

View File

@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# combo - Combo PWA App
# Copyright (C) 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/>.
from dateutil.parser import parse
from datetime import timedelta
import argparse
import django
from django.db import transaction
from django.core.management.base import BaseCommand
from django.utils.timezone import make_aware, now, is_naive
from combo.apps.notifications.models import Notification
from combo.apps.pwa.push import send_web_push
from combo.apps.pwa.models import WebPushRecord
from push_notifications.models import WebPushDevice
def to_datetime_argument(string):
try:
return make_aware(parse(string))
except ValueError as verr:
raise argparse.ArgumentTypeError(verr)
class Command(BaseCommand):
help = u'''Send push notification for the last notifications, for example :
$ combo-manage tenant_command send_webpush --all-tenants --since=2018-03-03T00:00:00
this will send all waiting web push notifications from march 3rd 2018 until now
'''
def add_arguments(self, parser):
# Named (optional) arguments
parser.add_argument(
'--since',
dest='since',
default=None,
type=to_datetime_argument,
help='Send push notifications created since this datetime',
)
def handle(self, *args, **options):
''' Send visible and new web-push notifications
'''
if django.VERSION < (1, 11):
print('Exiting send_webpush : running an incompatible version of Django (%s)' % (django.VERSION))
return
verbosity = options.get('verbosity', 1)
since = options.get('since')
if since is None:
since = now() - timedelta(hours=24)
if is_naive(since):
since = make_aware(since)
for notification in Notification.objects.new().visible().filter(start_timestamp__gte=since):
for device in WebPushDevice.objects.filter(user=notification.user, active=True):
with transaction.atomic():
web_push_record, created = WebPushRecord.objects.select_for_update().get_or_create(
notification=notification,
subscription=device)
if web_push_record.status == WebPushRecord.DEFAULT_STATUS:
if verbosity > 0:
print('Found a device id = %s to push the notification id = %s' % (device.id, notification.id))
web_push_record = send_web_push(web_push_record, device)

37
combo/apps/pwa/push.py Normal file
View File

@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
# combo - Combo PWA App
# Copyright (C) 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/>.
from push_notifications.webpush import WebPushError
def send_web_push(web_push_record, device):
'''
Send a push notification to every susbcribed device
Save to status into web_push_record
'''
try:
# send the request to the endpoint
web_push_record.response = device.send_message(web_push_record.payload, ttl=web_push_record.ttl)
if 'success' in web_push_record.response and web_push_record.response['success'] == 1:
web_push_record.set_status_ok()
else:
web_push_record.set_status_err()
except WebPushError as wperr:
web_push_record.set_status_err()
raise
return web_push_record

View File

@ -5,3 +5,4 @@
/sbin/runuser -u combo /usr/bin/combo-manage -- tenant_command update_momo_manifest --all-tenants -v0
/sbin/runuser -u combo /usr/bin/combo-manage -- tenant_command update_index --remove --all-tenants -v0
/sbin/runuser -u combo /usr/bin/combo-manage -- tenant_command notify_payments --all-tenants -v0
/sbin/runuser -u combo /usr/bin/combo-manage -- tenant_command send_webpush --all-tenants