Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c5536b7

Browse files
committedJul 11, 2023
Update documentation
1 parent 1d597e4 commit c5536b7

File tree

8 files changed

+106
-35
lines changed

8 files changed

+106
-35
lines changed
 

‎README.md

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ Sending notifications in Django has always been a complex subject. Django Magic
2020
- Support files
2121
- Support multiple gateways
2222
- Extensible
23+
- Support MJML
2324

2425

2526
### Installation
26-
> $pip install --upgrade django-magic-notifier
27+
> pip install --upgrade django-magic-notifier
2728
2829
### Usage
2930
##### 1. Configure settings
@@ -48,38 +49,31 @@ NOTIFIER = {
4849
```
4950

5051
##### 2. Create templates
51-
Create a folder named **notifier** in one of app's templates dir. In this folder create another folder named **base** then created your base templates in this folder. Example
52+
Create a folder named **notifier** in one of app's templates dir. In this folder create another folder named **my_template**
53+
then create your base templates in this folder. Example:
5254

53-
*app_name/templates/notifier/base/email.html*
55+
*app_name/templates/notifier/my_template/email.html*
5456
```
55-
{% extends "base_notifier/email.html" %}
57+
{% extends "base_notifier/email.html" %}
58+
{% block content %}
59+
<tr>
60+
<td><p>Hello {{ user.email }}
61+
</td>
62+
</tr>
63+
{% endblock %}
5664
```
5765

58-
*app_name/templates/notifier/base/email.txt*
66+
*app_name/templates/notifier/my_template/email.txt*
5967
```
60-
{% extends "base_notifier/email.txt" %}
68+
{% extends "base_notifier/email.txt" %}
69+
{% block content %}
70+
Hello {{ user.email }}
71+
{% endblock %}
6172
```
6273

63-
*app_name/templates/notifier/hello/email.html*
64-
```
65-
{% extends "notifier/base/email.html" %}
66-
{% block content %}
67-
<tr>
68-
<td><p>Hello {{ user.email }}
69-
</td>
70-
</tr>
71-
{% endblock %}
72-
```
73-
74-
*app_name/templates/notifier/hello/email.txt*
75-
```
76-
{% extends "notifier/hello/email.txt" %}
77-
{% block content %}
78-
>Hello {{ user.email }}
79-
{% endblock %}
80-
```
81-
82-
As you can see, the user to whom the notification goes is automatically added in the template's context. To avoid any clash to don't send the key `'user'` in the context of the **notifiy()** function presented below.
74+
As you can see, the user to whom the notification goes is automatically added
75+
in the template's context. To avoid any clash don't send the key `'user'`
76+
in the context of the **notifiy()** function presented below.
8377

8478
Note that it is DMN (Django Magic Notifier) that has the base_notifier template.
8579

‎docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
author = 'MATTON Jef'
3030

3131
# The full version, including alpha/beta/rc tags
32-
release = '0.1.2'
32+
release = '0.2.2'
3333

3434

3535
# -- General configuration ---------------------------------------------------

‎docs/source/index.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ Welcome to Django Magic Notifier's documentation!
1616
usage
1717
autoapi/index
1818

19-
20-
2119
Indices and tables
2220
==================
2321

‎docs/source/installation.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@ Git::
1313
> cd django-magic-notifier
1414
> python setup.py install
1515

16+
17+
If you intend to use Push notifications, then you need to include DMN
18+
consumers in your django channels routing
19+
20+
Python::
21+
22+
application = ProtocolTypeRouter({
23+
# Django's ASGI application to handle traditional HTTP requests
24+
"http": django_asgi_app,
25+
26+
# WebSocket chat handler
27+
"websocket": URLRouter([
28+
path("ws/notifications/<str:token>/", PushNotifConsumer.as_asgi()),
29+
])
30+
}
31+
)

‎docs/source/settings.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,7 @@ DMN needs a way to get a phone number from a User object. GET USER NUMBER must b
137137
one parameter of type User. Default **`'magic_notifer.utils.get_user_number'`**::
138138

139139
'GET_USER_NUMBER': 'path.to.function'
140+
141+
NOTIFIER PUSH SETTINGS
142+
======================
143+

‎docs/source/templates.rst

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ folder created another folder like that **app_name/templates/notifier/hello**
1313

1414
Now in this folder you have to create some files depending on how you will send your
1515
notifications. If you will send your notification via email then you must create
16-
two files within the hello folder named **email.html** and **email.txt**.
16+
two files within the hello folder named **email.html** and **email.txt** or **email.mjml**
17+
and **email**. Because DMN supports also mjml via 3rd party package.
1718
If you will send notifications via sms then you must create a file named **sms.txt**.
19+
If you want to send push notification then you must create a file **push.json**
1820

1921
It is a common behavior to have a base template, you can do the same by creating a
2022
folder named **base** in the notifier folder and creating the files **email.html**,
@@ -35,6 +37,10 @@ at this example:
3537

3638
{% extends "base_notifier/sms.txt" %}
3739

40+
*app_name/templates/notifier/base/push.json*::
41+
42+
{% extends "base_notifier/push.json" %}
43+
3844

3945
Now in the hello template folder, you do:
4046

@@ -50,15 +56,54 @@ Now in the hello template folder, you do:
5056

5157
*app_name/templates/notifier/hello/email.txt*::
5258

53-
{% extends "notifier/hello/email.txt" %}
59+
{% extends "notifier/base/email.txt" %}
5460
{% block content %}
5561
>Hello {{ user.email }}
5662
{% endblock %}
5763

64+
*app_name/templates/notifier/hello/email.mjml*::
65+
66+
<mjml>
67+
<mj-head>
68+
<mj-attributes>
69+
<mj-text align="center" color="#555" />
70+
</mj-attributes>
71+
</mj-head>
72+
<mj-body background-color="#eee">
73+
<mj-section>
74+
<mj-column>
75+
My Logo
76+
</mj-column>
77+
</mj-section>
78+
<mj-section background-color="#fff">
79+
<mj-column>
80+
<mj-text align="center">
81+
<h2>Welcome</h2>
82+
</mj-text>
83+
<mj-text>
84+
Welcome to our company
85+
</mj-text>
86+
</mj-column>
87+
88+
</mj-section>
89+
<mj-section>
90+
<mj-column>
91+
<mj-text> My Company </mj-text>
92+
</mj-column>
93+
</mj-section>
94+
</mj-body>
95+
</mjml>
96+
97+
5898
*app_name/templates/notifier/hello/sms.txt*::
5999

60-
{% extends "notifier/hello/sms.txt" %}
100+
{% extends "notifier/base/sms.txt" %}
61101
{% block content %}
62102
>Hello {{ user.email }}
63103
{% endblock %}
64104

105+
*app_name/templates/notifier/hello/push.json*::
106+
107+
{% extends "notifier/base/push.json" %}
108+
{% block subject %}Hello {{ user.username }}{% endblock %}
109+

‎docs/source/usage.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Send an email with a direct final string (no template) to a user instance::
99
notify(["email"], subject, [user], final_message="Nice if you get this")
1010

1111

12-
Send an email with a template to a user instance::
12+
Send an email with a template (hello) to a user instance::
1313

1414
user = User(email="testuser@localhost", username="testuser")
1515
subject = "Test magic notifier"
@@ -71,3 +71,9 @@ Send an email and sms with a template to all users excluding staff::
7171
user = User(email="testuser@localhost", username="testuser")
7272
subject = "Test magic notifier"
7373
notify(["email", 'sms'], subject, "all-staff", template='hello')
74+
75+
Send an email, a sms and a push notification with a template to all users excluding staff::
76+
77+
user = User(email="testuser@localhost", username="testuser")
78+
subject = "Test magic notifier"
79+
notify(["email", 'sms', 'push'], subject, "all-staff", template='hello')

‎magic_notifier/pusher.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
try:
88
from asgiref.sync import async_to_sync
99
from channels.layers import get_channel_layer
10-
except:
10+
except Exception:
1111
pass
1212
import json
1313

@@ -22,6 +22,14 @@ class Pusher:
2222
def __init__(
2323
self, subject, receivers: list, template: str, context: dict, **kwargs
2424
):
25+
"""
26+
27+
:param subject:The subject of the notification
28+
:param receivers: The user list of receivers
29+
:param template: The template to use
30+
:param context:The context to pass to the template
31+
:param kwargs:
32+
"""
2533
self.receivers: list = receivers
2634
self.template: str = template
2735
self.context: dict = context
@@ -74,7 +82,7 @@ def _send(self):
7482
)
7583

7684
return res
77-
except:
85+
except Exception as e:
7886
logger.error(traceback.format_exc())
7987

8088

0 commit comments

Comments
 (0)
Please sign in to comment.