From 1a570ea80526f0daa8f69cba5ead77a43b7acd9a Mon Sep 17 00:00:00 2001 From: Elias Showk Date: Thu, 23 Aug 2018 16:25:41 +0200 Subject: [PATCH 1/6] pwa: add django-push-notification and compatibility to Django 1.11 (#25462) --- combo/settings.py | 42 +++++++++++++++++++++++++++++++++++++++++- requirements.txt | 2 +- tests/test_pwa.py | 19 +++++++++++-------- tox.ini | 1 + 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/combo/settings.py b/combo/settings.py index 95bedf4..81f84f9 100644 --- a/combo/settings.py +++ b/combo/settings.py @@ -24,6 +24,8 @@ and to disable DEBUG mode in production. """ import os + +import django from django.conf import global_settings from django.utils.translation import ugettext_lazy as _ @@ -75,7 +77,6 @@ INSTALLED_APPS = ( 'combo.apps.usersearch', 'combo.apps.maps', 'combo.apps.calendar', - 'combo.apps.pwa', 'haystack', 'xstatic.pkg.josefinsans', 'xstatic.pkg.leaflet', @@ -84,6 +85,12 @@ INSTALLED_APPS = ( 'xstatic.pkg.leaflet_markercluster', ) +if django.VERSION >= (1, 11): + INSTALLED_APPS += ( + 'push_notifications', + 'combo.apps.pwa', + ) + MIDDLEWARE_CLASSES = ( 'combo.middleware.GlobalRequestMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', @@ -312,6 +319,39 @@ WCS_FORM_ASSET_SLOTS = {} BOOKING_CALENDAR_CELL_ENABLED = False NEWSLETTERS_CELL_ENABLED = False +# How-to configure VAPID +# $ pip install pywebpush py-vapid +# * Create a temporary file (claim.json) like this: +# +# { +# "sub": "mailto: android@entrouvert.com", +# "aud": "https://fcm.googleapis.com" +# } +# +# * Generate public and private keys: +# +# $ vapid --sign claim.json +# +# * Generate client public key (applicationServerKey) +# +# $ vapid --applicationServerKey +# +# Application Server Key = BEFuGfKKEFp-kEBMxAIw7ng8HeH_QwnH5_h55ijKD4FRvgdJU1GVlDo8K5U5ak4cMZdQTUJlkA34llWF0xHya70 +# +# * Configure settings: +# ** ``APP_SERVER_KEY``: Copy the value output of the previous command to your local_settings.py or to the multinenant configuration combo_settings.py +# ** do not touch WP_CLAIMS + +PUSH_NOTIFICATIONS_SETTINGS = { + 'WP_PRIVATE_KEY': os.path.join(BASE_DIR, "private_key.pem"), # file generated by the vapid command (from py-vapid) + 'WP_CLAIMS': { + "sub": "mailto: android@entrouvert.com" # 'sub' *must* be the only item, do not touch this, you could break VAPID protocol + }, + 'WP_ERROR_TIMEOUT': 10, # timeout for the request on the push server + 'UPDATE_ON_DUPLICATE_REG_ID': True, + 'MESSAGE_TAG': 'Publik PWA module', # default tag (for optionnal client-side grouping notification together) +} + local_settings_file = os.environ.get('COMBO_SETTINGS_FILE', os.path.join(os.path.dirname(__file__), 'local_settings.py')) if os.path.exists(local_settings_file): diff --git a/requirements.txt b/requirements.txt index 6d9cf38..5ad600b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -Django>=1.8, <1.9 +Django>=1.8, <1.12 django-ckeditor<4.5.3 gadjo feedparser diff --git a/tests/test_pwa.py b/tests/test_pwa.py index 07ebaf7..80d0be3 100644 --- a/tests/test_pwa.py +++ b/tests/test_pwa.py @@ -1,18 +1,21 @@ import os import pytest +import django from django.conf import settings from django.test import override_settings pytestmark = pytest.mark.django_db -def test_manifest_json(app): - app.get('/manifest.json', status=404) - templates_settings = [settings.TEMPLATES[0].copy()] - templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))] - with override_settings(TEMPLATES=templates_settings): - assert app.get('/manifest.json', status=200).json['name'] == 'test' +if django.VERSION >= (1, 11): + def test_manifest_json(app): + app.get('/manifest.json', status=404) -def test_service_worker(app): - app.get('/service-worker.js', status=200) + templates_settings = [settings.TEMPLATES[0].copy()] + templates_settings[0]['DIRS'] = ['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))] + with override_settings(TEMPLATES=templates_settings): + assert app.get('/manifest.json', status=200).json['name'] == 'test' + + def test_service_worker(app): + app.get('/service-worker.js', status=200) diff --git a/tox.ini b/tox.ini index 2c90217..5a90dc5 100644 --- a/tox.ini +++ b/tox.ini @@ -12,6 +12,7 @@ setenv = deps = django18: django>=1.8,<1.9 django111: django>=1.11,<1.12 + django111: django-push-notifications pytest-cov pytest-django pytest-freezegun -- 2.18.0