From 7dbdf23e3b2fa493a0517536612f721c315c2c1c Mon Sep 17 00:00:00 2001 From: Elias Showk Date: Thu, 23 Aug 2018 16:30:08 +0200 Subject: [PATCH 4/6] pwa: create a management command send_webpush (#25462) --- combo/apps/pwa/management/__init__.py | 0 .../apps/pwa/management/commands/__init__.py | 0 .../pwa/management/commands/send_webpush.py | 81 +++++++++++++++++++ combo/apps/pwa/push.py | 37 +++++++++ debian/combo.cron.hourly | 1 + 5 files changed, 119 insertions(+) create mode 100644 combo/apps/pwa/management/__init__.py create mode 100644 combo/apps/pwa/management/commands/__init__.py create mode 100644 combo/apps/pwa/management/commands/send_webpush.py create mode 100644 combo/apps/pwa/push.py diff --git a/combo/apps/pwa/management/__init__.py b/combo/apps/pwa/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/combo/apps/pwa/management/commands/__init__.py b/combo/apps/pwa/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/combo/apps/pwa/management/commands/send_webpush.py b/combo/apps/pwa/management/commands/send_webpush.py new file mode 100644 index 0000000..741914c --- /dev/null +++ b/combo/apps/pwa/management/commands/send_webpush.py @@ -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 . + +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) diff --git a/combo/apps/pwa/push.py b/combo/apps/pwa/push.py new file mode 100644 index 0000000..c9b934b --- /dev/null +++ b/combo/apps/pwa/push.py @@ -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 . + +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 diff --git a/debian/combo.cron.hourly b/debian/combo.cron.hourly index cba3913..681a25e 100644 --- a/debian/combo.cron.hourly +++ b/debian/combo.cron.hourly @@ -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 -- 2.18.0