Skip to content

Commit e376818

Browse files
authored
Update djagno >=2.0 documentation, styling and remove unused imports (#146)
* Add django >=2.0 documentation * Fix styling and remove unused imports
1 parent e2a4fd9 commit e376818

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

README.md

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
22
Django-Webpush
33
==============
44
[![Say Thanks!](https://img.shields.io/badge/Say%20Thanks-!-1EAEDB.svg)](https://saythanks.io/to/safwanrahman)
@@ -9,7 +9,6 @@ Currently, it Supports Sending Push Notification to **Firefox 46+, Chrome 52+ an
99

1010
----------
1111

12-
1312
Installation and Setup
1413
----------------------
1514

@@ -27,13 +26,15 @@ INSTALLED_APPS = (
2726
```
2827

2928
If you would like to send notification to Google Chrome Users, you need to add a ``WEBPUSH_SETTINGS`` entry with the **Vapid Credentials** Like following:
29+
3030
```python
3131
WEBPUSH_SETTINGS = {
3232
"VAPID_PUBLIC_KEY": "Vapid Public Key",
33-
"VAPID_PRIVATE_KEY":"Vapid Private Key",
33+
"VAPID_PRIVATE_KEY": "Vapid Private Key",
3434
"VAPID_ADMIN_EMAIL": "admin@example.com"
3535
}
3636
```
37+
3738
**Replace ``"Vapid Public Key"`` and ``"Vapid Private Key"`` with your Vapid Keys. Also replace ``admin@example.com`` with your email so that the push server of browser can reach to you if anything goes wrong.**
3839

3940
**Generate a Vapid key pair**
@@ -45,11 +46,20 @@ python manage.py webpush_generate_vapid_keypair
4546
Then include `webpush` in the `urls.py`
4647

4748
```python
49+
# Django >= 2.0
50+
from django.urls import path, include
51+
4852
urlpatterns = [
49-
url(r'^webpush/', include('webpush.urls'))
53+
path('webpush/', include('webpush.urls'))
5054
]
51-
```
5255

56+
# Django < 2.0
57+
from django.conf.urls import url, include
58+
59+
urlpatterns = [
60+
url(r'^webpush/', include('webpush.urls'))
61+
]
62+
```
5363

5464
`django-webpush` is shipped with built in **`jinja`** support.
5565
If you would like to use with jinja backend,
@@ -71,15 +81,13 @@ TEMPLATES = [
7181
]
7282
```
7383

74-
7584
**Then run Migration by ***`python manage.py migrate`*****
7685

77-
78-
7986
Adding Web Push Information in Template
8087
---------------------------------------
8188

8289
So in template, you need to load `webpush_notifications` custom template tag by following:
90+
8391
- If you are using built in templating engine, add `{% load webpush_notifications %}` in the template
8492
- If you are using **jinja** templating engine, you do not need to load anything.
8593

@@ -89,17 +97,21 @@ Next, inside the `<head></head>` tag add `webpush_header` according to your temp
8997
<head>
9098
# For django templating engine
9199
{% webpush_header %}
100+
92101
# For jinja templating engine
93102
{{ webpush_header() }}
94103
</head>
95104
```
105+
96106
Next, inside the `<body></body>` tag, insert `webush_button` where you would like to see the **Subscribe to Push Messaging** Button. Like following
97107

98108
```html
99109
<body>
100110
<p> Hello World! </p>
111+
101112
# For django templating engine
102113
{% webpush_button %}
114+
103115
# For jinja templating engine
104116
{{ webpush_button() }}
105117
</body>
@@ -110,8 +122,10 @@ Or if you want to add custom classes (e.g. bootstrap)
110122
```html
111123
<body>
112124
<p> Hello World! </p>
125+
113126
# For django templating engine
114127
{% webpush_button with_class="btn btn-outline-info" %}
128+
115129
# For jinja templating engine
116130
{{ webpush_button(with_class="btn btn-outline-info") }}
117131
</body>
@@ -126,6 +140,7 @@ Or if you want to add custom classes (e.g. bootstrap)
126140

127141
return render(request, 'template.html', {"webpush":webpush})
128142
```
143+
129144
> **Note:** If you dont pass `group` through the `webpush` context, only logged in users can see the button for subscription and able to get notification.
130145
131146
----------
@@ -138,7 +153,6 @@ So in order to send notification, see below.
138153

139154
- If you would like to send notification to a specific group, do like following:
140155

141-
142156
```python
143157
from webpush import send_group_notification
144158

@@ -152,6 +166,7 @@ So in order to send notification, see below.
152166
```
153167

154168
- If you would like to send Notification to a specific user, do like following
169+
155170
```python
156171
from webpush import send_user_notification
157172

@@ -178,14 +193,14 @@ So in order to send notification, see below.
178193

179194
send_group_notification(group_name="my_group", payload=payload, ttl=1000)
180195
```
196+
181197
**And the subscribers will get a notification like:**
182198

183199
![Web Push Notification icon](http://i.imgur.com/Vr1RMvF.png)
184200

185201
**That will open https://www.example.com if clicked.**
186-
187-
- If you want fine grained control over sending a single push message, do like following
188202

203+
- If you want fine grained control over sending a single push message, do like following
189204

190205
```python
191206
from webpush.utils import send_to_subscription
@@ -198,11 +213,7 @@ So in order to send notification, see below.
198213
send_to_subscription(push_info.subscription, payload)
199214

200215
```
201-
202216

203-
204-
205-
206217
**And the subscribers will get a notification like**
207218
![Web Push Notification](http://i.imgur.com/VA6cxRc.png)
208219

@@ -218,20 +229,20 @@ The package is shipped with built in internationalization support.
218229

219230
If you would like to add more language or update translation, you can run the following command:
220231

221-
```bash
232+
```bash
222233

223-
# Add js translation
224-
django-admin makemessages -d djangojs -l <language_code>
234+
# Add js translation
235+
django-admin makemessages -d djangojs -l <language_code>
225236

226-
# Add python translation
227-
django-admin makemessages -l <language_code>
228-
```
237+
# Add python translation
238+
django-admin makemessages -l <language_code>
239+
```
229240

230241
After that, you can run `django-admin compilemessages` to compile the messages.
231242

232243
License
233244
=======
234-
----
245+
235246
Copyright © 2018 Safwan Rahman
236247

237248
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

webpush/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
from .utils import send_notification_to_group, send_notification_to_user
44

5+
56
def send_group_notification(group_name, payload, ttl=0, exclude_user_id=None):
67
payload = json.dumps(payload)
78
send_notification_to_group(group_name, payload, ttl, exclude_user_id)
89

10+
911
def send_user_notification(user, payload, ttl=0):
1012
payload = json.dumps(payload)
1113
send_notification_to_user(user, payload, ttl)

webpush/forms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ def save_or_delete(self, subscription, user, status_type, group_name):
1818
data["user"] = user
1919

2020
if group_name:
21-
group, created = Group.objects.get_or_create(name=group_name)
21+
group, _ = Group.objects.get_or_create(name=group_name)
2222
data["group"] = group
2323

2424
data["subscription"] = subscription
2525

26-
push_info, created = PushInformation.objects.get_or_create(**data)
26+
push_info, _ = PushInformation.objects.get_or_create(**data)
2727

2828
# If unsubscribe is called, that means need to delete the browser
29-
# and notification info from server.
29+
# and notification info from server.
3030
if status_type == "unsubscribe":
3131
push_info.delete()
3232
subscription.delete()
@@ -39,5 +39,5 @@ class Meta:
3939
fields = ('endpoint', 'auth', 'p256dh', 'browser', 'user_agent')
4040

4141
def get_or_save(self):
42-
subscription, created = SubscriptionInfo.objects.get_or_create(**self.cleaned_data)
42+
subscription, _ = SubscriptionInfo.objects.get_or_create(**self.cleaned_data)
4343
return subscription

webpush/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from django.http import HttpResponse
33
from django.views.decorators.csrf import csrf_exempt
4-
from django.views.decorators.http import require_POST, require_GET
4+
from django.views.decorators.http import require_POST
55
from django.views.generic import TemplateView
66

77
from .forms import WebPushForm, SubscriptionForm

0 commit comments

Comments
 (0)