From f99c621909d2c230eab30ca70026c117cf87dc1a Mon Sep 17 00:00:00 2001 From: Safwan Rahman Date: Fri, 10 Jun 2016 17:32:22 +0600 Subject: [PATCH] [issue #5] Adding manifest.json functionality for chrome --- webpush/templates/webpush.html | 2 ++ webpush/urls.py | 1 + webpush/utils.py | 11 ++++++++--- webpush/views.py | 15 ++++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/webpush/templates/webpush.html b/webpush/templates/webpush.html index 6f83bc4..3d7385e 100644 --- a/webpush/templates/webpush.html +++ b/webpush/templates/webpush.html @@ -5,5 +5,7 @@ + + {% endif %} \ No newline at end of file diff --git a/webpush/urls.py b/webpush/urls.py index 6de7e65..32586db 100644 --- a/webpush/urls.py +++ b/webpush/urls.py @@ -4,4 +4,5 @@ urlpatterns = [ url(r'^save_information', views.save_info, name='save_webpush_info'), + url(r'^manifest', views.generate_manifest, name='webpush_manifest_json'), ] \ No newline at end of file diff --git a/webpush/utils.py b/webpush/utils.py index b64fcbb..4054c6e 100644 --- a/webpush/utils.py +++ b/webpush/utils.py @@ -1,6 +1,6 @@ -# import requests from .models import PushInformation, Group +from django.conf import settings from django.forms.models import model_to_dict from pywebpush import WebPusher @@ -24,7 +24,12 @@ def send_notification_to_group(group_name, payload, ttl=0): def _send_notification(push_info, payload, ttl): subscription = push_info.subscription subscription_data = _process_subscription_info(subscription) - req = WebPusher(subscription_data).send(data=payload, ttl=ttl) + # Check if GCM info is provided in the settings + if hasattr(settings,'WEBPUSH_SETTINGS'): + gcm_key = settings.WEBPUSH_SETTINGS.get('GCM_KEY') + else: + gcm_key = None + req = WebPusher(subscription_data).send(data=payload, ttl=ttl, gcm_key=gcm_key) return req def _process_subscription_info(subscription): @@ -35,5 +40,5 @@ def _process_subscription_info(subscription): return { "endpoint": endpoint, - "keys": {"p256dh": str(p256dh), "auth": str(auth)} + "keys": {"p256dh": p256dh, "auth": auth} } \ No newline at end of file diff --git a/webpush/views.py b/webpush/views.py index 3012388..8927b3f 100644 --- a/webpush/views.py +++ b/webpush/views.py @@ -1,15 +1,16 @@ import json +from django.conf import settings from django.core.exceptions import FieldError -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt -from django.views.decorators.http import require_POST +from django.views.decorators.http import require_POST, require_GET +from .config import MANIFEST from .forms import WebPushForm, SubscriptionForm - @require_POST @csrf_exempt def save_info(request): @@ -51,6 +52,14 @@ def save_info(request): return HttpResponse(status=400) +@require_GET +def generate_manifest(request): + if hasattr(settings,'WEBPUSH_SETTINGS'): + return JsonResponse(MANIFEST) + else: + return HttpResponse(status=404) + + def process_subscription_data(post_data): """Process the subscription data according to out model""" subscription_data = post_data.pop("subscription", {})