Skip to content

Commit

Permalink
Making various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
safwanrahman committed Jun 3, 2019
1 parent f6d35a4 commit 09996c0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 41 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,14 @@ So in order to send notification, see below.


```python
from webpush import WebPushException, send_single_notification
from webpush.utils import send_to_subscription

payload = {"head": "Welcome!", "body": "Hello World"}

user = request.user
push_infos = user.webpush_info.select_related("subscription")
for push_info in push_infos:
try:
send_single_notification(push_info.subscription, payload)

except WebPushException as ex:
extra = {"subscription": push_info.subscription, "exception": ex}
log.error("Sending Push notification failed.", extra=extra)
if "<Response [410]>" in str(ex):
# 410 = Endpoint Not Valid
# remove subscription
push_info.subscription.delete()
send_to_subscription(push_info.subscription, payload)

```

Expand Down
12 changes: 5 additions & 7 deletions webpush/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from .models import PushInformation
from .utils import _send_notification

from pywebpush import WebPushException

class PushInfoAdmin(admin.ModelAdmin):
list_display = ("__str__", "user", "subscription", "group")
Expand All @@ -14,12 +13,11 @@ class PushInfoAdmin(admin.ModelAdmin):
def send_test_message(self, request, queryset):
payload = {"head": "Hey", "body": "Hello World"}
for device in queryset:
try:
_send_notification(device.subscription, json.dumps(payload), 0)
except WebPushException as ex:
device.delete()
self.message_user(request,"Deprecated subscription deleted")
self.message_user(request,"Test sent successfully")
notification = _send_notification(device.subscription, json.dumps(payload), 0)
if notification:
self.message_user(request, "Test sent successfully")
else:
self.message_user(request, "Deprecated subscription deleted")


admin.site.register(PushInformation, PushInfoAdmin)
36 changes: 13 additions & 23 deletions webpush/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,22 @@
def send_notification_to_user(user, payload, ttl=0):
# Get all the push_info of the user

errors = []
push_infos = user.webpush_info.select_related("subscription")
for push_info in push_infos:
try:
_send_notification(push_info.subscription, payload, ttl)

except WebPushException as ex:
errors.append(dict(subscription=push_info.subscription,
exception=ex))

if errors:
raise WebPushException("Push failed.", extra=errors)
_send_notification(push_info.subscription, payload, ttl)


def send_notification_to_group(group_name, payload, ttl=0):
from .models import Group
# Get all the subscription related to the group

errors = []
push_infos = Group.objects.get(name=group_name).webpush_info.select_related("subscription")
for push_info in push_infos:
try:
_send_notification(push_info.subscription, payload, ttl)

except WebPushException as ex:
errors.append(dict(subscription=push_info.subscription,
exception=ex))

if errors:
raise WebPushException("Push failed.", extra=errors)
_send_notification(push_info.subscription, payload, ttl)


def send_to_subscription(subscription, payload, ttl=0):
_send_notification(subscription, payload, ttl)
return _send_notification(subscription, payload, ttl)


def _send_notification(subscription, payload, ttl):
Expand All @@ -60,8 +42,16 @@ def _send_notification(subscription, payload, ttl):
'vapid_claims': {"sub": "mailto:{}".format(vapid_admin_email)}
}

req = webpush(subscription_info=subscription_data, data=payload, ttl=ttl, **vapid_data)
return req
try:
req = webpush(subscription_info=subscription_data, data=payload, ttl=ttl, **vapid_data)
return req
except WebPushException as e:
# If the subscription is expired, delete it.
if e.response.status_code == 410:
subscription.delete()
else:
# Its other type of exception!
raise e


def _process_subscription_info(subscription):
Expand Down

0 comments on commit 09996c0

Please sign in to comment.